1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
11 DBICTEST_SQLITE_USE_FILE
12 DBICTEST_VIA_REPLICATED
22 use DBICTest::Util 'slurp_bytes';
24 my $schema = DBICTest->init_schema();
26 my $log_fn = "t/var/sql-$$.log";
27 unlink $log_fn or die $! if -e $log_fn;
29 # make sure we are testing the vanilla debugger and not ::PrettyPrint
30 require DBIx::Class::Storage::Statistics;
31 $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
33 ok ( $schema->storage->debug(1), 'debug' );
35 open my $dbgfh, '>', $log_fn or die $!;
36 $schema->storage->debugfh($dbgfh);
37 $schema->storage->debugfh->autoflush(1);
38 $schema->resultset('CD')->count;
39 $schema->storage->debugfh(undef);
42 my @loglines = slurp_bytes $log_fn;
43 is (@loglines, 1, 'one line of log');
44 like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
48 local $ENV{DBIC_TRACE} = "=$log_fn";
51 $schema->resultset('CD')->count;
53 my $schema2 = DBICTest->init_schema(no_deploy => 1);
54 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
56 my @loglines = slurp_bytes $log_fn;
57 is(@loglines, 2, '2 lines of log');
58 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
59 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
61 $schema->storage->debugobj->debugfh(undef)
65 unlink $log_fn if $log_fn;
68 open(STDERRCOPY, '>&STDERR');
70 my $exception_line_number;
71 # STDERR will be closed, no T::B diag in blocks
74 $exception_line_number = __LINE__ + 1; # important for test, do not reformat
75 $schema->resultset('CD')->search({})->count;
80 open(STDERR, '>&STDERRCOPY');
84 \QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)\E
86 \Qat @{[__FILE__]} line $exception_line_number\E$
88 or diag "Unexpected exception text:\n\n$exception\n";
92 local $SIG{__WARN__} = sub { push @warnings, @_ if $_[0] =~ /character/i };
94 open(STDERR, '>', File::Spec->devnull) or die $!;
95 $schema->resultset('CD')->search({ title => "\x{1f4a9}" })->count;
102 open(STDERR, '>&STDERRCOPY');
105 die "How did that fail... $exception"
108 is_deeply(\@warnings, [], 'No warnings with unicode on STDERR');
110 # test debugcb and debugobj protocol
112 my $rs = $schema->resultset('CD')->search( {
114 cdid => { -between => [ 1, 3 ] },
115 title => { '!=' => \[ '?', undef ] }
118 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 != ? ) )';
119 my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm>
123 $schema->storage->debugcb(sub { push @args, @_ } );
129 sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ),
133 package DBICTest::DebugObj;
134 our @ISA = 'DBIx::Class::Storage::Statistics';
138 ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_;
142 my $do = $schema->storage->debugobj(DBICTest::DebugObj->new);
146 is( $do->{_traced_sql}, $sql_trace );
148 is_deeply ( $do->{_traced_bind}, \@bind_trace );
151 # recreate test as seen in DBIx::Class::QueryLog
152 # the rationale is that if someone uses a non-IO::Handle object
153 # on CPAN, many are *bound* to use one on darkpan. Thus this
154 # test to ensure there is no future silent breakage
159 package DBICTest::_Printable;
162 my ($self, @args) = @_;
163 $output .= join('', @args);
167 $schema->storage->debugobj(undef);
168 $schema->storage->debug(1);
169 $schema->storage->debugfh( bless {}, "DBICTest::_Printable" );
170 $schema->storage->txn_do( sub { $schema->resultset('Artist')->count } );
176 ^ \QBEGIN WORK\E \s*?
177 ^ \QSELECT COUNT( * ) FROM artist me:\E \s*?
183 $schema->storage->debug(0);
184 $schema->storage->debugfh(undef);