Debian Codegraph @

t/70_csv.t

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
•6 use File::Spec;
7
8 BEGIN { use_ok ("DBI"); }
9 do "./t/lib.pl";
10
11 my @tbl_def = (
12 [ "id", "INTEGER", 4, &COL_KEY ],
13 [ "str", "CHAR", 64, &COL_NULLABLE ],
14 [ "name", "CHAR", 64, &COL_NULLABLE ],
15 );
16
17 sub DbFile;
18
19 my $dir = "output$$";
20 my $fqd = File::Spec->rel2abs ($dir);
21 my $abs = Cwd::abs_path ($dir);
22
23 ok (my $dbh = Connect (), "connect");
24
25 ok ($dbh->{f_dir} eq $dir || $dbh->{f_dir} eq $abs ||
26 $dbh->{f_dir} eq $fqd, "default dir");
27 ok ($dbh->{f_dir} = $dir, "set f_dir");
28
29 ok (my $tbl = FindNewTable ($dbh), "find new test table");
30 ok (!-f DbFile ($tbl), "does not exist");
31
32 ok (my $tbl2 = FindNewTable ($dbh), "find new test table");
33 ok (!-f DbFile ($tbl2), "does not exist");
34
35 ok (my $tbl3 = FindNewTable ($dbh), "find new test table");
36 ok (!-f DbFile ($tbl3), "does not exist");
37
38 ok (my $tbl4 = FindNewTable ($dbh), "find new test table");
39 ok (!-f DbFile ($tbl4), "does not exist");
40
41 isnt ($tbl, $tbl2, "different 1 2");
42 isnt ($tbl, $tbl3, "different 1 3");
43 isnt ($tbl, $tbl4, "different 1 4");
44 isnt ($tbl2, $tbl3, "different 2 3");
45 isnt ($tbl2, $tbl4, "different 2 4");
46 isnt ($tbl3, $tbl4, "different 3 4");
47
48 like (my $def = TableDefinition ($tbl, @tbl_def),
49 qr{^create table $tbl}i, "table definition");
50 ok ($dbh->do ($def), "create table 1");
51 ok (-f DbFile ($tbl), "does exists");
52
53 ok ($dbh->do ("drop table $tbl"), "drop table");
54 ok (!-f DbFile ($tbl), "does not exist");
55
56 ok ($dbh->disconnect, "disconnect");
57 undef $dbh;
58
59 my $dsn = "DBI:CSV:f_dir=$dir;csv_eol=\015\012;csv_sep_char=\\;;";
60 ok ($dbh = Connect ($dsn), "connect");
61
62 ok ($dbh->do ($def), "create table");
63 ok (-f DbFile ($tbl), "does exists");
64
65 ok ($dbh->do ("insert into $tbl values (1, 1, ?)", undef, "joe"), "insert 1");
66 ok ($dbh->do ("insert into $tbl values (2, 2, ?)", undef, "Jochen;"), "insert 2");
67
68 ok (my $sth = $dbh->prepare ("select * from $tbl"), "prepare");
69 ok ($sth->execute, "execute");
70 ok (my $row = $sth->fetch, "fetch 1");
71 is_deeply ($row, [ 1, "1", "joe" ], "content");
72 ok ( $row = $sth->fetch, "fetch 2");
73 is_deeply ($row, [ 2, "2", "Jochen;" ], "content");
74 ok ($sth->finish, "finish");
75 undef $sth;
76
77 ok ($dbh->do ("drop table $tbl"), "drop table");
78 ok (!-f DbFile ($tbl), "does not exist");
79
80 ok ($dbh->disconnect, "disconnect");
81 undef $dbh;
82
83 $dsn = "DBI:CSV:";
84 ok ($dbh = Connect ($dsn), "connect");
85
86 # Check, whether the csv_tables->{$tbl}{file} attribute works
87 like (my $def4 = TableDefinition ($tbl4, @tbl_def),
88 qr{^create table $tbl4}i, "table definition");
•89 { my $dbf4 = DbFile ($tbl4);
90 my $dbd4 = DbDir ();
91
92 $dbh->{f_dir_search} = [ $dbd4 ];
93 ok ($dbh->{csv_tables}{$tbl4}{file} = $dbf4, "set table/file");
94 }
95 ok ($dbh->do ($def4), "create table");
96 ok (-f DbFile ($tbl4), "does exists");
97
98 ok ($dbh->do ("drop table $tbl4"), "drop table");
99
•100 if ($DBI::VERSION ge "1.648") {
101 ok (my $tbl5 = FindNewTable ($dbh), "find new test table");
102 my $dir5 = File::Spec->catdir (DbDir (), "not-in-scope");
103 mkdir $dir5;
104 ok (my $dbf5 = File::Spec->catdir ($dir5, $tbl5), "in bad location");
105 ok (!-f $dbf5, "does not exist");
106 ok ($dbh->{csv_tables}{$tbl5}{file} = $dbf5, "set to illegal location");
107 { my @d;
108 local $SIG{__DIE__} = sub { push @d => @_ };
109 local $dbh->{PrintError} = 0;
110 eval { $dbh->do ("create table $tbl5 (x char)"); };
111 like ("@d", qr{is unsafe and not allowed}, "unsafe caught");
112 };
113 rmdir $dir5;
114 }
115
116 ok ($dbh->disconnect, "disconnect");
117 undef $dbh;
118
119 ok ($dbh = DBI->connect ("dbi:CSV:", "", "", {
120 f_dir => DbDir (),
121 f_ext => ".csv",
122 dbd_verbose => 8,
123 csv_sep_char => ";",
124 csv_blank_is_undef => 1,
125 csv_always_quote => 1,
126 }), "connect with attr");
127
128 is ($dbh->{dbd_verbose}, 8, "dbd_verbose set");
129 is ($dbh->{f_ext}, ".csv", "f_ext set");
130 is ($dbh->{csv_sep_char}, ";", "sep_char set");
131 is ($dbh->{csv_blank_is_undef}, 1, "blank_is_undef set");
132
133 ok ($dbh->do ($def), "create table");
134 ok (-f DbFile ($tbl).".csv", "does exists");
135 #is ($sth->{blank_is_undef}, 1, "blank_is_undef");
136 eval {
137 local $SIG{__WARN__} = sub { };
138
139 ok ($sth = $dbh->prepare ("insert into $tbl values (?, ?, ?)"), "prepare");
140 is ($sth->execute (1, ""), undef, "not enough values");
141 like ($dbh->errstr, qr/passed 2 parameters where 3 required/, "error message");
142
143 # Cannot use the same handle twice. SQL::Statement bug
144 ok ($sth = $dbh->prepare ("insert into $tbl values (?, ?, ?)"), "prepare");
145 is ($sth->execute (1, "", 1, ""), undef, "too many values");
146 like ($dbh->errstr, qr/passed 4 parameters where 3 required/, "error message");
147 };
148 ok ($sth->execute ($_, undef, "Code $_"), "insert $_") for 0 .. 9;
149
150 ok ($dbh->do ("drop table $tbl"), "drop table");
151 ok (!-f DbFile ($tbl), "does not exist");
152 ok (!-f DbFile ($tbl).".csv", "does not exist");
153
154 ok ($dbh->disconnect, "disconnect");
155 undef $dbh;
156
157 done_testing ();