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); |
64b3598f |
27 | $schema->resultset('CD')->count; |
70f39278 |
28 | |
64b3598f |
29 | my @loglines = $lfn->slurp; |
30 | is (@loglines, 1, 'one line of log'); |
31 | like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success'); |
70f39278 |
32 | |
33 | $schema->storage->debugfh(undef); |
64b3598f |
34 | |
35 | { |
36 | local $ENV{DBIC_TRACE} = "=$lfn"; |
37 | unlink $lfn; |
38 | |
39 | $schema->resultset('CD')->count; |
40 | |
41 | my $schema2 = DBICTest->init_schema(no_deploy => 1); |
42 | $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms |
43 | |
44 | my @loglines = $lfn->slurp; |
45 | is(@loglines, 2, '2 lines of log'); |
46 | like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success'); |
47 | like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success'); |
48 | |
49 | $schema->storage->debugobj->debugfh(undef) |
50 | } |
f410ea47 |
51 | |
8d6b1478 |
52 | END { |
53 | unlink $lfn; |
54 | } |
55 | |
70f39278 |
56 | open(STDERRCOPY, '>&STDERR'); |
68367f1f |
57 | |
68b8ba54 |
58 | my $exception_line_number; |
68367f1f |
59 | # STDERR will be closed, no T::B diag in blocks |
60 | my $exception = try { |
61 | close(STDERR); |
68b8ba54 |
62 | $exception_line_number = __LINE__ + 1; # important for test, do not reformat |
64b3598f |
63 | $schema->resultset('CD')->search({})->count; |
68367f1f |
64 | } catch { |
65 | $_ |
66 | } finally { |
67 | # restore STDERR |
68 | open(STDERR, '>&STDERRCOPY'); |
69 | }; |
70 | |
68b8ba54 |
71 | like $exception, qr/ |
72 | \QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)\E |
73 | .+ |
74 | \Qat @{[__FILE__]} line $exception_line_number\E$ |
75 | /xms; |
68367f1f |
76 | |
9d522a4e |
77 | my @warnings; |
78 | $exception = try { |
79 | local $SIG{__WARN__} = sub { push @warnings, @_ if $_[0] =~ /character/i }; |
80 | close STDERR; |
81 | open(STDERR, '>', File::Spec->devnull) or die $!; |
82 | $schema->resultset('CD')->search({ title => "\x{1f4a9}" })->count; |
83 | ''; |
84 | } catch { |
85 | $_; |
86 | } finally { |
87 | # restore STDERR |
88 | close STDERR; |
89 | open(STDERR, '>&STDERRCOPY'); |
90 | }; |
91 | |
92 | die "How did that fail... $exception" |
93 | if $exception; |
94 | |
95 | is_deeply(\@warnings, [], 'No warnings with unicode on STDERR'); |
f410ea47 |
96 | |
70f39278 |
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 | |
c9d29bb2 |
139 | done_testing; |