Debian Codegraph @

t/60_misc.t

raw
1 #!/usr/bin/perl
2
3 # Misc tests
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 is ($dbh->quote ("tast1"), "'tast1'", "quote");
26
27 ok (my $sth = $dbh->prepare ("select * from $tbl where id = 1"), "prepare");
28 { local $dbh->{PrintError} = 0;
29 my @warn;
30 local $SIG{__WARN__} = sub { push @warn, @_ };
31 eval { is ($sth->fetch, undef, "fetch w/o execute"); };
32 is (scalar @warn, 1, "one error");
33 like ($warn[0],
34 qr/fetch row without a precee?ding execute/, "error message");
35 }
36 ok ($sth->execute, "execute");
37 is ($sth->fetch, undef, "fetch no rows");
38 ok ($sth->finish, "finish");
39 undef $sth;
40
41 ok ($sth = $dbh->prepare ("insert into $tbl values (?, ?)"), "prepare ins");
42 ok ($sth->execute ($_, "Code $_"), "insert $_") for 1 .. 9;
43 ok ($sth->finish, "finish");
44 undef $sth;
45
46 ok ($sth = $dbh->prepare ("select * from $tbl order by id"), "prepare sel");
47 # Test what happens with two consequetive execute ()'s
48 ok ($sth->execute, "execute 1");
49 ok ($sth->execute, "execute 2");
50
51 # Test all fetch methods
52 ok (my @row = $sth->fetchrow_array, "fetchrow_array");
53 is_deeply (\@row, [ 1, "Code 1" ], "content");
54 ok (my $row = $sth->fetchrow_arrayref, "fetchrow_arrayref");
55 is_deeply ( $row, [ 2, "Code 2" ], "content");
56 ok ( $row = $sth->fetchrow_hashref, "fetchrow_hashref");
57 is_deeply ( $row, { id => 3, name => "Code 3" }, "content");
58 ok (my $all = $sth->fetchall_hashref ("id"), "fetchall_hashref");
59 is_deeply ($all,
60 { map { ( $_ => { id => $_, name => "Code $_" } ) } 4 .. 9 }, "content");
61
62 ok ($sth->execute, "execute");
63 ok ( $all = $sth->fetchall_arrayref, "fetchall_arrayref");
64 is_deeply ($all, [ map { [ $_, "Code $_" ] } 1 .. 9 ], "content");
65
66 ok ($dbh->do ("drop table $tbl"), "drop table");
67 ok ($dbh->disconnect, "disconnect");
68
69 done_testing ();