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