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