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