Fix test failure on 5.8.x with DBIC_TRACE=1 (introduced by 68b8ba54)
[dbsrgits/DBIx-Class.git] / t / storage / debug.t
CommitLineData
004d31fb 1use strict;
f54428ab 2use warnings;
64b3598f 3no warnings 'once';
004d31fb 4
8b60b921 5BEGIN { $ENV{DBICTEST_VIA_REPLICATED} = 0 }
6
004d31fb 7use Test::More;
f410ea47 8use Test::Exception;
68367f1f 9use Try::Tiny;
9d522a4e 10use File::Spec;
004d31fb 11use lib qw(t/lib);
12use DBICTest;
c9d29bb2 13use Path::Class qw/file/;
004d31fb 14
09d763c8 15BEGIN { delete @ENV{qw(DBIC_TRACE DBIC_TRACE_PROFILE DBICTEST_SQLITE_USE_FILE)} }
da69d72e 16
004d31fb 17my $schema = DBICTest->init_schema();
18
8d6b1478 19my $lfn = file("t/var/sql-$$.log");
64b3598f 20unlink $lfn or die $!
21 if -e $lfn;
22
f410ea47 23# make sure we are testing the vanilla debugger and not ::PrettyPrint
2cfc22dd 24require DBIx::Class::Storage::Statistics;
f410ea47 25$schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
004d31fb 26
27ok ( $schema->storage->debug(1), 'debug' );
64b3598f 28$schema->storage->debugfh($lfn->openw);
8494142c 29$schema->storage->debugfh->autoflush(1);
64b3598f 30$schema->resultset('CD')->count;
70f39278 31
64b3598f 32my @loglines = $lfn->slurp;
33is (@loglines, 1, 'one line of log');
34like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
70f39278 35
36$schema->storage->debugfh(undef);
64b3598f 37
38{
39 local $ENV{DBIC_TRACE} = "=$lfn";
40 unlink $lfn;
41
42 $schema->resultset('CD')->count;
43
44 my $schema2 = DBICTest->init_schema(no_deploy => 1);
45 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
46
47 my @loglines = $lfn->slurp;
48 is(@loglines, 2, '2 lines of log');
49 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
50 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
51
52 $schema->storage->debugobj->debugfh(undef)
53}
f410ea47 54
8d6b1478 55END {
56 unlink $lfn;
57}
58
70f39278 59open(STDERRCOPY, '>&STDERR');
68367f1f 60
68b8ba54 61my $exception_line_number;
68367f1f 62# STDERR will be closed, no T::B diag in blocks
63my $exception = try {
64 close(STDERR);
68b8ba54 65 $exception_line_number = __LINE__ + 1; # important for test, do not reformat
64b3598f 66 $schema->resultset('CD')->search({})->count;
68367f1f 67} catch {
68 $_
69} finally {
70 # restore STDERR
71 open(STDERR, '>&STDERRCOPY');
72};
73
e93fb362 74ok $exception =~ /
68b8ba54 75 \QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)\E
76 .+
77 \Qat @{[__FILE__]} line $exception_line_number\E$
e93fb362 78/xms
79 or diag "Unexpected exception text:\n\n$exception\n";
68367f1f 80
9d522a4e 81my @warnings;
82$exception = try {
83 local $SIG{__WARN__} = sub { push @warnings, @_ if $_[0] =~ /character/i };
84 close STDERR;
85 open(STDERR, '>', File::Spec->devnull) or die $!;
86 $schema->resultset('CD')->search({ title => "\x{1f4a9}" })->count;
87 '';
88} catch {
89 $_;
90} finally {
91 # restore STDERR
92 close STDERR;
93 open(STDERR, '>&STDERRCOPY');
94};
95
96die "How did that fail... $exception"
97 if $exception;
98
99is_deeply(\@warnings, [], 'No warnings with unicode on STDERR');
f410ea47 100
2cfc22dd 101# test debugcb and debugobj protocol
e5d9ee92 102{
2cfc22dd 103 my $rs = $schema->resultset('CD')->search( {
104 artist => 1,
105 cdid => { -between => [ 1, 3 ] },
106 title => { '!=' => \[ '?', undef ] }
107 });
108
109 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 != ? ) )';
110 my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm>
111
112
113 my @args;
114 $schema->storage->debugcb(sub { push @args, @_ } );
115
116 $rs->all;
117
118 is_deeply( \@args, [
119 "SELECT",
120 sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ),
121 ]);
122
123 {
124 package DBICTest::DebugObj;
125 our @ISA = 'DBIx::Class::Storage::Statistics';
126
127 sub query_start {
128 my $self = shift;
129 ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_;
130 }
131 }
132
133 my $do = $schema->storage->debugobj(DBICTest::DebugObj->new);
134
135 $rs->all;
136
137 is( $do->{_traced_sql}, $sql_trace );
138
139 is_deeply ( $do->{_traced_bind}, \@bind_trace );
e5d9ee92 140}
141
8494142c 142# recreate test as seen in DBIx::Class::QueryLog
143# the rationale is that if someone uses a non-IO::Handle object
144# on CPAN, many are *bound* to use one on darkpan. Thus this
145# test to ensure there is no future silent breakage
146{
147 my $output = "";
148
149 {
150 package DBICTest::_Printable;
151
152 sub print {
153 my ($self, @args) = @_;
154 $output .= join('', @args);
155 }
156 }
157
158 $schema->storage->debugobj(undef);
159 $schema->storage->debug(1);
160 $schema->storage->debugfh( bless {}, "DBICTest::_Printable" );
161 $schema->storage->txn_do( sub { $schema->resultset('Artist')->count } );
162
163 like (
164 $output,
165 qr/
166 \A
167 ^ \QBEGIN WORK\E \s*?
168 ^ \QSELECT COUNT( * ) FROM artist me:\E \s*?
169 ^ \QCOMMIT\E \s*?
170 \z
171 /xm
172 );
173
174 $schema->storage->debug(0);
175 $schema->storage->debugfh(undef);
176}
177
c9d29bb2 178done_testing;