ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-DBI/t/02_sql_lite.t
Revision: 1.3
Committed: Tue Apr 2 02:13:14 2013 UTC (11 years, 2 months ago) by root
Content type: application/x-troff
Branch: MAIN
CVS Tags: rel-2_3
Changes since 1.2: +9 -10 lines
Log Message:
2.3

File Contents

# Content
1 #!/usr/bin/perl
2
3 BEGIN {
4 unless ($ENV{PERL_ANYEVENT_DBI_TESTS}) {
5 print "1..0 # SKIP env var PERL_ANYEVENT_DBI_TESTS not set\n"; exit;
6 }
7 eval {
8 require DBD::SQLite;
9 };
10 if ($@) {
11 print "1..0 # SKIP this test requires Test::More and DBD::SQLite\n"; exit;
12 }
13 require Test::More;
14 import Test::More tests => 43;
15 }
16
17 use strict;
18 use warnings;
19 use AnyEvent;
20 use AnyEvent::DBI;
21 use File::Temp qw(tempfile);
22
23 # we are going to watch what the sub-processes send to stderr
24 close STDERR;
25 my($tfh_err,$tfn_err) = tempfile;
26 close $tfh_err;
27 open(STDERR,">>$tfn_err");
28
29 my ($cv,$dbh,$tfh,$tfn,$error,$result,$rv);
30
31 ($tfh,$tfn) = tempfile;
32 close $tfh;
33
34 # connect with exec
35 $cv = AnyEvent->condvar;
36 $dbh = new AnyEvent::DBI(
37 "dbi:SQLite:dbname=$tfn",'','',
38 AutoCommit => 1,
39 PrintError => 0,
40 timeout => 2,
41 exec_server => 1,
42 on_error => sub { },
43 on_connect => sub {return $cv->send($@) unless $_[1]; $cv->send()},
44 );
45 $error = $cv->recv();
46 is($error,undef,'on_connect() called without error, sqlite server is connected');
47
48 # lets have an error
49 $cv = AnyEvent->condvar;
50 $dbh->exec('select bogus_column from no_such_table',sub {return $cv->send($@) unless $_[1];$cv->send(undef,$_[1])});
51 ($error,$result) = $cv->recv();
52 like ($error,qr{no such table}i,'Select from non existant table results in error');
53 # ensure we got no stderr output
54 ok(-z $tfn_err,'Error does not result in output on STDERR');
55
56 # check the error behavior
57 $cv = AnyEvent->condvar;
58 $dbh->attr('PrintError',sub {return $cv->send($@) unless $_[1]; $cv->send(undef,$_[1])});
59 ($error,$result)= $cv->recv();
60 ok(!$error,'No errors occur while checking attribute');
61 ok(!$result,'Accessor without set (PrintError) returns false');
62
63 # change the error behavior
64 $cv = AnyEvent->condvar;
65 $dbh->attr(PrintError=>1,sub {return $cv->send($@) unless $_[1]; $cv->send(undef,$_[1])});
66 ($error,$result)= $cv->recv();
67 ok(!$error,'No error occurs while setting PrintError => 1');
68 ok($result,'Accessor with set (PrintError) returns true');
69
70 # check the error behavior
71 $cv = AnyEvent->condvar;
72 $dbh->attr('PrintError',sub {return $cv->send($@) unless $_[1]; $cv->send(undef,$_[1])});
73 ($error,$result)= $cv->recv();
74 ok(!$error,'No errors occur while checking attribute');
75 ok($result,'PrintError was true');
76
77 # lets have an error
78 $cv = AnyEvent->condvar;
79 $dbh->exec('select bogus_column from no_such_table',sub {return $cv->send($@) unless $_[1];$cv->send(undef,$_[1])});
80 ($error,$result) = $cv->recv();
81 like ($error,qr{no such table}i,'Select from non existant column makes an error');
82 # ensure we did get STDERR output
83 ok(-s $tfn_err,'Error message has appeared on STDERR');
84
85 # create a table
86 $cv = AnyEvent->condvar;
87 $dbh->exec('create table a_table (a_column text)',sub {return $cv->send($@) unless $_[1];$cv->send(undef,$_[1])});
88 ($error,$result) = $cv->recv();
89 ok(!$error,'No errors creating a table');
90
91 # add some data
92 $cv = AnyEvent->condvar;
93 $dbh->exec('insert into a_table (a_column) values(?)','test',sub {return $cv->send($@) unless $#_;$cv->send(undef,@_[1,2])});
94 ($error,$result,$rv) = $cv->recv();
95 ok(!$error,'No errors inserting into table');
96 is($rv,1,"One row affected");
97
98 # check for the data
99 $cv = AnyEvent->condvar;
100 $dbh->exec('select a_column from a_table',sub {return $cv->send($@) unless $#_;$cv->send(undef,@_[1,2])});
101 ($error,$result,$rv) = $cv->recv();
102 ok(!$error,'No errors inserting into table');
103 ok($rv,'select succeeded');
104 is($result->[0]->[0],'test','found correct data');
105
106 # check the autocommit behavior
107 $cv = AnyEvent->condvar;
108 $dbh->attr('AutoCommit',sub {return $cv->send($@) unless $_[1]; $cv->send(undef,$_[1])});
109 ($error,$result)= $cv->recv();
110 ok(!$error,'No errors occur while checking attribute');
111 ok($result,'AutoCommit was true');
112
113 # turn off autocommit
114 $cv = AnyEvent->condvar;
115 $dbh->attr(AutoCommit=>0,sub {return $cv->send($@) unless $_[1]; $cv->send(undef,$_[1])});
116 ($error,$result)= $cv->recv();
117 ok(!$error,'No error setting attr');
118 ok(!$result,'AutoCommit was false');
119
120 # add some data
121 $cv = AnyEvent->condvar;
122 $dbh->exec('insert into a_table (a_column) values(?)','moredata',sub {return $cv->send($@) unless $#_;$cv->send(undef,@_[1,2])});
123 ($error,$result,$rv) = $cv->recv;
124 ok(!$error,'No errors inserting into table');
125 is($rv,1,"One row affected");
126
127 # crash the handle
128 unlink $dbh;
129
130 # connect without exec or autocommit
131 $cv = AnyEvent->condvar;
132 $dbh = new AnyEvent::DBI(
133 "dbi:SQLite:dbname=$tfn",'','',
134 AutoCommit => 0,
135 PrintError => 0,
136 timeout => 2,
137 exec_server => 0,
138 on_error => sub { },
139 on_connect => sub {return $cv->send($@) unless $_[1]; $cv->send()},
140 );
141 $error = $cv->recv();
142 is($error,undef,'on_connect() called without error, sqlite server is connected');
143
144 # check for the data and that the aborted transaction did not make it to the database
145 $cv = AnyEvent->condvar;
146 $dbh->exec('select a_column from a_table',sub {return $cv->send($@) unless $_[1];$cv->send(undef,@_[1,2])});
147 ($error,$result,$rv) = $cv->recv();
148 ok(!$error,'No errors selecting from table');
149 ok($rv,'select succeeded');
150 is(scalar @$result,1,'found only one row');
151 is($result->[0]->[0],'test','found correct data in that row');
152
153 # add some data
154 $cv = AnyEvent->condvar;
155 $dbh->exec('insert into a_table (a_column) values(?)','moredata',sub {return $cv->send($@) unless $#_;$cv->send(undef,@_[1,2])});
156 ($error,$result,$rv) = $cv->recv();
157 ok(!$error,'No errors inserting into table');
158 is($rv,1,'One row affected');
159
160 # commit to db
161 $cv = AnyEvent->condvar;
162 $dbh->commit(sub {return $cv->send($@) unless $_[1];$cv->send(undef,$_[1])});
163 ($error,$result) = $cv->recv();
164 ok(!$error,'No errors commiting');
165
166 # check for the data and that the aborted transaction did not make it to the database
167 $cv = AnyEvent->condvar;
168 $dbh->exec('select a_column from a_table',sub {return $cv->send($@) unless $_[1];$cv->send(undef,@_[1,2])});
169 ($error,$result,$rv) = $cv->recv();
170 ok(!$error,'No errors inserting into table');
171 ok($rv,'select succeeded');
172 is(scalar @$result,2,'found two rows');
173 is($result->[0]->[0],'test','found correct data in row one');
174 is($result->[1]->[0],'moredata','found correct data in row two');
175
176 # change the autocommit behavior
177 $cv = AnyEvent->condvar;
178 $dbh->attr(AutoCommit=>1,sub {return $cv->send($@) unless $_[1]; $cv->send(undef,$_[1])});
179 ($error,$result)= $cv->recv();
180 ok(!$error,'No error occurs while setting AutoCommit => 1');
181 ok($result,'Accessor with set (AutoCommit) returns true');
182
183 # using bad function returns error
184 $cv = AnyEvent->condvar;
185 #$dbh->exec('select a_column from a_table where instr(a_column,?)','re',sub {return $cv->send($@) unless $_[0];$cv->send(undef,@_[1,2]);});
186 $dbh->exec('select a_column from a_table where instr(a_column,?)','re',
187 sub {return $cv->send($@,@_[0,1,2]);});
188 my $hdl;
189 ($error,$hdl,$result,$rv) = $cv->recv();
190 like($error,qr{function}i,'Using an unknown function results in error');
191
192 # create the function
193 $cv = AnyEvent->condvar;
194
195 $dbh->func(
196 q{
197 'instr',
198 2,
199 sub {
200 my ($string, $search) = @_;
201 return index $string, $search;
202 },
203 },
204 'create_function',
205 sub {return $cv->send($@) unless $_[1];$cv->send(undef,$_[1])}
206 );
207 $cv->recv(); # ignore result from this particular private fn.
208
209 # using new function
210 $cv = AnyEvent->condvar;
211 $dbh->exec('select a_column from a_table where instr(a_column,?) >= 0','re',sub {return $cv->send($@) unless $_[1];$cv->send(undef,@_[1,2])});
212 ($error,$result,$rv) = $cv->recv();
213 ok(!$error,'Our new function works fine');
214 ok($rv,'select succeeded');
215 is(scalar @$result,1,'found only one row');
216 is($result->[0]->[0],'moredata','found correct data');
217
218 END {
219 unlink $tfn if $tfn;
220 # system ("cat $tfn_err");
221 unlink $tfn_err if $tfn_err;
222 }
223