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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#!/usr/local/bin/perl
# Test row insertion and retrieval
use strict;
use warnings;
use Test::More;
BEGIN { use_ok ("DBI") }
do "t/lib.pl";
my @tbl_def = (
[ "id", "INTEGER", 4, 0 ],
[ "name", "CHAR", 64, 0 ],
[ "val", "INTEGER", 4, 0 ],
[ "txt", "CHAR", 64, 0 ],
);
ok (my $dbh = Connect (), "connect");
ok (my $tbl = FindNewTable ($dbh), "find new test table");
$tbl ||= "tmp99";
eval {
local $SIG{__WARN__} = sub {};
$dbh->do ("drop table $tbl");
};
like (my $def = TableDefinition ($tbl, @tbl_def),
qr{^create table $tbl}i, "table definition");
my $sz = 0;
ok ($dbh->do ($def), "create table");
my $tbl_file = DbFile ($tbl);
ok ($sz = -s $tbl_file, "file exists");
ok ($dbh->do ("insert into $tbl values ".
"(1, 'Alligator Descartes', 1111, 'Some Text')"), "insert");
ok ($sz < -s $tbl_file, "file grew");
$sz = -s $tbl_file;
ok ($dbh->do ("insert into $tbl (id, name, val, txt) values ".
"(2, 'Crocodile Dundee', 2222, 'Down Under')"), "insert with field names");
ok ($sz < -s $tbl_file, "file grew");
ok (my $sth = $dbh->prepare ("select * from $tbl where id = 1"), "prepare");
is (ref $sth, "DBI::st", "handle type");
ok ($sth->execute, "execute");
ok (my $row = $sth->fetch, "fetch");
is (ref $row, "ARRAY", "returned a list");
is ($sth->errstr, undef, "no error");
is_deeply ($row, [ 1, "Alligator Descartes", 1111, "Some Text" ], "content");
ok ($sth->finish, "finish");
undef $sth;
# Try some other capitilization
ok ($dbh->do ("DELETE FROM $tbl WHERE id = 1"), "delete");
# Now, try SELECT'ing the row out. This should fail.
ok ($sth = $dbh->prepare ("select * from $tbl where id = 1"), "prepare");
is (ref $sth, "DBI::st", "handle type");
ok ($sth->execute, "execute");
is ($sth->fetch, undef, "fetch");
is ($sth->errstr, undef, "error"); # ???
ok ($sth->finish, "finish");
undef $sth;
ok ($sth = $dbh->prepare ("insert into $tbl values (?, ?, ?, ?)"), "prepare insert");
ok ($sth->execute (3, "Babar", 3333, "Elephant"), "insert prepared");
ok ($sth->finish, "finish");
undef $sth;
ok ($sth = $dbh->prepare ("insert into $tbl (id, name, val, txt) values (?, ?, ?, ?)"), "prepare insert with field names");
ok ($sth->execute (4, "Vischje", 33, "in het riet"), "insert prepared");
ok ($sth->finish, "finish");
undef $sth;
ok ($dbh->do ("delete from $tbl"), "delete all");
ok ($dbh->do ("insert into $tbl (id) values (0)"), "insert just one field");
{ local (@ARGV) = DbFile ($tbl);
my @csv = <>;
s/\r?\n\Z// for @csv;
is (scalar @csv, 2, "Just two lines");
is ($csv[0], "id,name,val,txt", "header");
is ($csv[1], "0,,,", "data");
}
ok ($dbh->do ("drop table $tbl"), "drop");
ok ($dbh->disconnect, "disconnect");
done_testing ();
|