Commit | Line | Data |
004d31fb |
1 | use strict; |
f54428ab |
2 | use warnings; |
004d31fb |
3 | |
4 | use Test::More; |
f410ea47 |
5 | use Test::Exception; |
004d31fb |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
67e1ac6d |
8 | use DBIC::DebugObj; |
949172b0 |
9 | use DBIC::SqlMakerTest; |
c9d29bb2 |
10 | use Path::Class qw/file/; |
004d31fb |
11 | |
12 | my $schema = DBICTest->init_schema(); |
13 | |
f410ea47 |
14 | # make sure we are testing the vanilla debugger and not ::PrettyPrint |
15 | $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new); |
004d31fb |
16 | |
17 | ok ( $schema->storage->debug(1), 'debug' ); |
c9d29bb2 |
18 | $schema->storage->debugfh(file('t/var/sql.log')->openw); |
004d31fb |
19 | |
193195d8 |
20 | $schema->storage->debugfh->autoflush(1); |
70f39278 |
21 | my $rs = $schema->resultset('CD')->search({}); |
22 | $rs->count(); |
23 | |
c9d29bb2 |
24 | my $log = file('t/var/sql.log')->openr; |
70f39278 |
25 | my $line = <$log>; |
26 | $log->close(); |
f410ea47 |
27 | like($line, qr/^SELECT COUNT/, 'Log success'); |
70f39278 |
28 | |
29 | $schema->storage->debugfh(undef); |
30 | $ENV{'DBIC_TRACE'} = '=t/var/foo.log'; |
31 | $rs = $schema->resultset('CD')->search({}); |
32 | $rs->count(); |
c9d29bb2 |
33 | $log = file('t/var/foo.log')->openr; |
70f39278 |
34 | $line = <$log>; |
35 | $log->close(); |
f410ea47 |
36 | like($line, qr/^SELECT COUNT/, 'Log success'); |
70f39278 |
37 | $schema->storage->debugobj->debugfh(undef); |
38 | delete($ENV{'DBIC_TRACE'}); |
f410ea47 |
39 | |
70f39278 |
40 | open(STDERRCOPY, '>&STDERR'); |
41 | stat(STDERRCOPY); # nop to get warnings quiet |
42 | close(STDERR); |
f410ea47 |
43 | dies_ok { |
70f39278 |
44 | $rs = $schema->resultset('CD')->search({}); |
45 | $rs->count(); |
f410ea47 |
46 | } 'Died on closed FH'; |
47 | |
70f39278 |
48 | open(STDERR, '>&STDERRCOPY'); |
49 | |
e5d9ee92 |
50 | # test trace output correctness for bind params |
51 | { |
af6aac2d |
52 | my ($sql, @bind); |
53 | $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)); |
e5d9ee92 |
54 | |
55 | my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } ); |
9b459129 |
56 | is_same_sql_bind( |
af6aac2d |
57 | $sql, \@bind, |
0491b597 |
58 | "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND (cdid BETWEEN ? AND ?) )", |
af6aac2d |
59 | [qw/'1' '1' '3'/], |
9b459129 |
60 | 'got correct SQL with all bind parameters (debugcb)' |
61 | ); |
62 | |
9b459129 |
63 | @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } ); |
64 | is_same_sql_bind( |
65 | $sql, \@bind, |
42a7de49 |
66 | "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND (cdid BETWEEN ? AND ?) )", ["'1'", "'1'", "'3'"], |
9b459129 |
67 | 'got correct SQL with all bind parameters (debugobj)' |
e5d9ee92 |
68 | ); |
69 | } |
70 | |
c9d29bb2 |
71 | done_testing; |