Skip to content

Instantly share code, notes, and snippets.

@memememomo
Created November 10, 2010 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memememomo/670085 to your computer and use it in GitHub Desktop.
Save memememomo/670085 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib $FindBin::Bin . '/../lib';
use Test::More;
use Test::mysqld;
use Test::Fixture::DBIxSkinny;
use SQL::SplitStatement;
use My::DB;
### テスト用DBを準備
# mysqld生成
my $mysqld = Test::mysqld->new(
my_cnf => { 'skip-networking' => '' },
) or plan skip_all => $Test::mysqld::errstr;
# DBIx::Skinnyで接続
my $db = My::DB->new({dsn => $mysqld->dsn});
# テーブル生成SQL
my $sql = qq|
use test;
create table book (
id int(11) auto_increment primary key,
title varchar(64),
author_name varchar(32),
detail text,
price int(11),
issued_on date,
created_at datetime
);
|;
# テーブル作成
my $splitter = SQL::SplitStatement->new(
keep_terminator => 1,
keep_comments => 0,
keep_empty_statement => 0,
);
for ( $splitter->split($sql) ) {
$db->do($_);
}
# Test::Fixture::DBIxSkinnyの関数
# データを挿入
construct_fixture(
db => $db,
fixture => 'fixture.yaml',
);
### テスト開始
my $row = $db->search('book', {id => 1})->first;
is($row->title, 'title1');
is($row->author_name, 'author1');
is($row->detail, 'detail1');
is($row->price, 100);
is(ref $row->issued_on, 'DateTime');
is($row->issued_on->ymd, '2010-11-11');
done_testing();
- table: book
name: entry1
data:
title: title1
author_name: author1
detail: detail1
price: 100
issued_on: 2010/11/11
- table: book
name: entry2
data:
title: title2
author_name: author2
detail: detail2
price: 200
issued_on: 2010/11/12
- table: book
name: entry3
data:
title: title3
author_name: author3
detail: detail3
price: 300
issued_on: 2010/11/13
- table: book
name: entry4
data:
title: title4
author_name: author4
detail: detail4
price: 400
issued_on: 2010/11/14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment