Commit | Line | Data |
004d31fb |
1 | use strict; |
f54428ab |
2 | use warnings; |
64b3598f |
3 | no warnings 'once'; |
004d31fb |
4 | |
5 | use Test::More; |
f410ea47 |
6 | use Test::Exception; |
004d31fb |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
c9d29bb2 |
9 | use Path::Class qw/file/; |
004d31fb |
10 | |
09d763c8 |
11 | BEGIN { delete @ENV{qw(DBIC_TRACE DBIC_TRACE_PROFILE DBICTEST_SQLITE_USE_FILE)} } |
da69d72e |
12 | |
004d31fb |
13 | my $schema = DBICTest->init_schema(); |
14 | |
8d6b1478 |
15 | my $lfn = file("t/var/sql-$$.log"); |
64b3598f |
16 | unlink $lfn or die $! |
17 | if -e $lfn; |
18 | |
f410ea47 |
19 | # make sure we are testing the vanilla debugger and not ::PrettyPrint |
2cfc22dd |
20 | require DBIx::Class::Storage::Statistics; |
f410ea47 |
21 | $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new); |
004d31fb |
22 | |
23 | ok ( $schema->storage->debug(1), 'debug' ); |
64b3598f |
24 | $schema->storage->debugfh($lfn->openw); |
193195d8 |
25 | $schema->storage->debugfh->autoflush(1); |
64b3598f |
26 | $schema->resultset('CD')->count; |
70f39278 |
27 | |
64b3598f |
28 | my @loglines = $lfn->slurp; |
29 | is (@loglines, 1, 'one line of log'); |
30 | like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success'); |
70f39278 |
31 | |
32 | $schema->storage->debugfh(undef); |
64b3598f |
33 | |
34 | { |
35 | local $ENV{DBIC_TRACE} = "=$lfn"; |
36 | unlink $lfn; |
37 | |
38 | $schema->resultset('CD')->count; |
39 | |
40 | my $schema2 = DBICTest->init_schema(no_deploy => 1); |
41 | $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms |
42 | |
43 | my @loglines = $lfn->slurp; |
44 | is(@loglines, 2, '2 lines of log'); |
45 | like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success'); |
46 | like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success'); |
47 | |
48 | $schema->storage->debugobj->debugfh(undef) |
49 | } |
f410ea47 |
50 | |
8d6b1478 |
51 | END { |
52 | unlink $lfn; |
53 | } |
54 | |
70f39278 |
55 | open(STDERRCOPY, '>&STDERR'); |
70f39278 |
56 | close(STDERR); |
f410ea47 |
57 | dies_ok { |
64b3598f |
58 | $schema->resultset('CD')->search({})->count; |
f410ea47 |
59 | } 'Died on closed FH'; |
60 | |
70f39278 |
61 | open(STDERR, '>&STDERRCOPY'); |
62 | |
2cfc22dd |
63 | # test debugcb and debugobj protocol |
e5d9ee92 |
64 | { |
2cfc22dd |
65 | my $rs = $schema->resultset('CD')->search( { |
66 | artist => 1, |
67 | cdid => { -between => [ 1, 3 ] }, |
68 | title => { '!=' => \[ '?', undef ] } |
69 | }); |
70 | |
71 | my $sql_trace = 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( ( artist = ? AND ( cdid BETWEEN ? AND ? ) AND title != ? ) )'; |
72 | my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm> |
73 | |
74 | |
75 | my @args; |
76 | $schema->storage->debugcb(sub { push @args, @_ } ); |
77 | |
78 | $rs->all; |
79 | |
80 | is_deeply( \@args, [ |
81 | "SELECT", |
82 | sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ), |
83 | ]); |
84 | |
85 | { |
86 | package DBICTest::DebugObj; |
87 | our @ISA = 'DBIx::Class::Storage::Statistics'; |
88 | |
89 | sub query_start { |
90 | my $self = shift; |
91 | ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_; |
92 | } |
93 | } |
94 | |
95 | my $do = $schema->storage->debugobj(DBICTest::DebugObj->new); |
96 | |
97 | $rs->all; |
98 | |
99 | is( $do->{_traced_sql}, $sql_trace ); |
100 | |
101 | is_deeply ( $do->{_traced_bind}, \@bind_trace ); |
e5d9ee92 |
102 | } |
103 | |
c9d29bb2 |
104 | done_testing; |