Release v0.082821
[dbsrgits/DBIx-Class.git] / t / storage / debug.t
1 use strict;
2 use warnings;
3 no warnings 'once';
4
5 use Test::More;
6 use Test::Exception;
7 use Try::Tiny;
8 use File::Spec;
9 use lib qw(t/lib);
10 use DBICTest;
11 use Path::Class qw/file/;
12
13 BEGIN { delete @ENV{qw(DBIC_TRACE DBIC_TRACE_PROFILE DBICTEST_SQLITE_USE_FILE)} }
14
15 my $schema = DBICTest->init_schema();
16
17 my $lfn = file("t/var/sql-$$.log");
18 unlink $lfn or die $!
19   if -e $lfn;
20
21 # make sure we are testing the vanilla debugger and not ::PrettyPrint
22 require DBIx::Class::Storage::Statistics;
23 $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
24
25 ok ( $schema->storage->debug(1), 'debug' );
26 $schema->storage->debugfh($lfn->openw);
27 $schema->storage->debugfh->autoflush(1);
28 $schema->resultset('CD')->count;
29
30 my @loglines = $lfn->slurp;
31 is (@loglines, 1, 'one line of log');
32 like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
33
34 $schema->storage->debugfh(undef);
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 }
52
53 END {
54   unlink $lfn;
55 }
56
57 open(STDERRCOPY, '>&STDERR');
58
59 my $exception_line_number;
60 # STDERR will be closed, no T::B diag in blocks
61 my $exception = try {
62   close(STDERR);
63   $exception_line_number = __LINE__ + 1;  # important for test, do not reformat
64   $schema->resultset('CD')->search({})->count;
65 } catch {
66   $_
67 } finally {
68   # restore STDERR
69   open(STDERR, '>&STDERRCOPY');
70 };
71
72 ok $exception =~ /
73   \QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)\E
74     .+
75   \Qat @{[__FILE__]} line $exception_line_number\E$
76 /xms
77   or diag "Unexpected exception text:\n\n$exception\n";
78
79 my @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
94 die "How did that fail... $exception"
95   if $exception;
96
97 is_deeply(\@warnings, [], 'No warnings with unicode on STDERR');
98
99 # test debugcb and debugobj protocol
100 {
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 );
138 }
139
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
176 done_testing;