Commit | Line | Data |
004d31fb |
1 | use strict; |
f54428ab |
2 | use warnings; |
64b3598f |
3 | no warnings 'once'; |
004d31fb |
4 | |
8b60b921 |
5 | BEGIN { $ENV{DBICTEST_VIA_REPLICATED} = 0 } |
6 | |
004d31fb |
7 | use Test::More; |
f410ea47 |
8 | use Test::Exception; |
68367f1f |
9 | use Try::Tiny; |
9d522a4e |
10 | use File::Spec; |
004d31fb |
11 | use lib qw(t/lib); |
12 | use DBICTest; |
c9d29bb2 |
13 | use Path::Class qw/file/; |
004d31fb |
14 | |
09d763c8 |
15 | BEGIN { delete @ENV{qw(DBIC_TRACE DBIC_TRACE_PROFILE DBICTEST_SQLITE_USE_FILE)} } |
da69d72e |
16 | |
004d31fb |
17 | my $schema = DBICTest->init_schema(); |
18 | |
8d6b1478 |
19 | my $lfn = file("t/var/sql-$$.log"); |
64b3598f |
20 | unlink $lfn or die $! |
21 | if -e $lfn; |
22 | |
f410ea47 |
23 | # make sure we are testing the vanilla debugger and not ::PrettyPrint |
2cfc22dd |
24 | require DBIx::Class::Storage::Statistics; |
f410ea47 |
25 | $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new); |
004d31fb |
26 | |
27 | ok ( $schema->storage->debug(1), 'debug' ); |
64b3598f |
28 | $schema->storage->debugfh($lfn->openw); |
8494142c |
29 | $schema->storage->debugfh->autoflush(1); |
64b3598f |
30 | $schema->resultset('CD')->count; |
70f39278 |
31 | |
64b3598f |
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'); |
70f39278 |
35 | |
36 | $schema->storage->debugfh(undef); |
64b3598f |
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 | } |
f410ea47 |
54 | |
8d6b1478 |
55 | END { |
56 | unlink $lfn; |
57 | } |
58 | |
70f39278 |
59 | open(STDERRCOPY, '>&STDERR'); |
68367f1f |
60 | |
68b8ba54 |
61 | my $exception_line_number; |
68367f1f |
62 | # STDERR will be closed, no T::B diag in blocks |
63 | my $exception = try { |
64 | close(STDERR); |
68b8ba54 |
65 | $exception_line_number = __LINE__ + 1; # important for test, do not reformat |
64b3598f |
66 | $schema->resultset('CD')->search({})->count; |
68367f1f |
67 | } catch { |
68 | $_ |
69 | } finally { |
70 | # restore STDERR |
71 | open(STDERR, '>&STDERRCOPY'); |
72 | }; |
73 | |
e93fb362 |
74 | ok $exception =~ / |
68b8ba54 |
75 | \QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)\E |
76 | .+ |
77 | \Qat @{[__FILE__]} line $exception_line_number\E$ |
e93fb362 |
78 | /xms |
79 | or diag "Unexpected exception text:\n\n$exception\n"; |
68367f1f |
80 | |
9d522a4e |
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'); |
f410ea47 |
100 | |
2cfc22dd |
101 | # test debugcb and debugobj protocol |
e5d9ee92 |
102 | { |
2cfc22dd |
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 ); |
e5d9ee92 |
140 | } |
141 | |
8494142c |
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 | |
c9d29bb2 |
178 | done_testing; |