1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#!/usr/bin/perl
# test if update returns expected values / keeps file sizes sane
use strict;
use warnings;
use Test::More;
BEGIN { use_ok ("DBI"); }
do "./t/lib.pl";
my @tbl_def = (
[ "id", "INTEGER", 4, &COL_NULLABLE ],
[ "name", "CHAR", 64, &COL_NULLABLE ],
);
ok (my $dbh = Connect (), "connect");
ok (my $tbl = FindNewTable ($dbh), "find new test table");
like (my $def = TableDefinition ($tbl, @tbl_def),
qr{^create table $tbl}i, "table definition");
ok ($dbh->do ($def), "create table");
my $sz = 0;
my $tbl_file = DbFile ($tbl);
ok ($sz = -s $tbl_file, "file exists");
ok ($dbh->do ("insert into $tbl (id) values ($_)"), "insert $_") for 1 .. 10;
ok ($sz < -s $tbl_file, "file grew");
$sz = -s $tbl_file;
{ local $dbh->{PrintWarn} = 0;
local $dbh->{PrintError} = 0;
is ($dbh->do ("update wxyz set name = 'ick' where id = 99"), undef, "update in non-existing tbl");
}
my $zero_ret = $dbh->do ("update $tbl set name = 'ack' where id = 99");
ok ($zero_ret, "true non-existing update RV (via do)");
cmp_ok ($zero_ret, "==", 0, "update non-existing row (via do)");
cmp_ok ($sz, "==", -s $tbl_file, "file size did not change on noop updates");
is ($dbh->do ("update $tbl set name = 'multis' where id > 7"), 3, "update several (count) (via do)");
cmp_ok ($sz, "<", -s $tbl_file, "file size grew on update");
$sz = -s $tbl_file;
is ($dbh->do ("update $tbl set name = 'single' where id = 9"), 1, "update single (count) (via do)");
cmp_ok ($sz, "==", -s $tbl_file, "file size did not change on same-size update");
$zero_ret = $dbh->prepare ("update $tbl set name = 'ack' where id = 88")->execute;
ok ($zero_ret, "true non-existing update RV (via prepare/execute)");
cmp_ok ($zero_ret, "==", 0, "update non-existing row (via prepare/execute)");
cmp_ok ($sz, "==", -s $tbl_file, "file size did not change on noop update");
$sz = -s $tbl_file;
is ($dbh->prepare ("update $tbl set name = 'multis' where id < 4")->execute, 3, "update several (count) (via prepare/execute)");
cmp_ok ($sz, "<", -s $tbl_file, "file size grew on update");
$sz = -s $tbl_file;
is ($dbh->prepare ("update $tbl set name = 'single' where id = 2")->execute, 1, "update single (count) (via prepare/execute)");
cmp_ok ($sz, "==", -s $tbl_file, "file size did not change on same-size update");
ok ($dbh->do ("drop table $tbl"), "drop table");
ok ($dbh->disconnect, "disconnect");
ok (!-f $tbl_file, "file removed");
done_testing ();
|