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