More Related Content
Similar to Let s database_testing
Similar to Let s database_testing (20)
Let s database_testing
- 6. DB のテストをすると
この SELECT なんにもマッチしない...
INSERT したデータが文字化けしてるや
ん!
UPDATE や DELETE に WHERE 付け忘れ
てて全件あばばば
TRIGGER が間違ってて、データ壊れ
た。。。
- 16. よくない例
ステージング環境のDBに接続してテストする
本番は MySQL だけど、テストでは SQLite を
使う
自分のローカルに立ち上がっている MySQL を
テストに使う
- 21. Test::Fixture::DBI
table 作ったり、 procedure / function / trigger 作っ
たり、data ぶっこんだりできる
既存の DB から schema 定義をもってきたり、指定の
レコードをとって来たりできる
make_*_yaml.pl で fixture や database 定義を
YAML に吐き出せる
Author: ZIGOROu, (test: xaicron)
- 36. my $row = $model->connector('SLAVE')->txn(fixup => sub {
my $dbh = shift;
my ($stmt, @bind) = $model->sql->select(...);
return $dbh->selectrow_hashref($stmt, undef, @bind);
});
- 40. my $row = try {
$self->connector('SLAVE', $user_id)->run(fixup => sub {
my $dbh = shift;
my ($stmt, @bind) = $self->sql->select(
'user_data',
[qw/id name/],
{ id => $user_id, disabled => 0 },
);
$dbh->selectrow_hashref($stmt, undef, @bind);
});
}
catch {
my $e = $_;
...
};
- 44. try {
my $seq = $self->next_seq('MASTER', 'seq_tweet');
$self->connector('MASTER', $user_id)->run(fixup => sub {
my $dbh = shift;
my ($stmt, @bind) = $self->sql->insert(
'tweet',
{ id => $seq, ... },
);
$dbh->do($stmt, undef, @bind);
$dbh->commit;
});
}
catch {
my $e = $_;
};
- 65. replace_default_make_test(
includes => ['t/lib'],
modules => ['Test::MyApp::DB'],
before_run_codes => [
sub {
$SIG{INT} = sub { CORE::exit 1 };
my $db = Test::MyApp::DB->setup;
$ENV{TEST_MY_SOCKET} = $db->my_cnf->{socket};
},
],
);