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