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