1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
11 DBICTEST_SQLITE_USE_FILE
12 DBICTEST_VIA_REPLICATED
22 use Path::Class qw/file/;
24 my $schema = DBICTest->init_schema();
26 my $lfn = file("t/var/sql-$$.log");
30 # make sure we are testing the vanilla debugger and not ::PrettyPrint
31 require DBIx::Class::Storage::Statistics;
32 $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
34 ok ( $schema->storage->debug(1), 'debug' );
35 $schema->storage->debugfh($lfn->openw);
36 $schema->storage->debugfh->autoflush(1);
37 $schema->resultset('CD')->count;
39 my @loglines = $lfn->slurp;
40 is (@loglines, 1, 'one line of log');
41 like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
43 $schema->storage->debugfh(undef);
46 local $ENV{DBIC_TRACE} = "=$lfn";
49 $schema->resultset('CD')->count;
51 my $schema2 = DBICTest->init_schema(no_deploy => 1);
52 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
54 my @loglines = $lfn->slurp;
55 is(@loglines, 2, '2 lines of log');
56 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
57 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
59 $schema->storage->debugobj->debugfh(undef)
66 open(STDERRCOPY, '>&STDERR');
68 my $exception_line_number;
69 # STDERR will be closed, no T::B diag in blocks
72 $exception_line_number = __LINE__ + 1; # important for test, do not reformat
73 $schema->resultset('CD')->search({})->count;
78 open(STDERR, '>&STDERRCOPY');
82 \QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)\E
84 \Qat @{[__FILE__]} line $exception_line_number\E$
86 or diag "Unexpected exception text:\n\n$exception\n";
90 local $SIG{__WARN__} = sub { push @warnings, @_ if $_[0] =~ /character/i };
92 open(STDERR, '>', File::Spec->devnull) or die $!;
93 $schema->resultset('CD')->search({ title => "\x{1f4a9}" })->count;
100 open(STDERR, '>&STDERRCOPY');
103 die "How did that fail... $exception"
106 is_deeply(\@warnings, [], 'No warnings with unicode on STDERR');
108 # test debugcb and debugobj protocol
110 my $rs = $schema->resultset('CD')->search( {
112 cdid => { -between => [ 1, 3 ] },
113 title => { '!=' => \[ '?', undef ] }
116 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 != ? ) )';
117 my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm>
121 $schema->storage->debugcb(sub { push @args, @_ } );
127 sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ),
131 package DBICTest::DebugObj;
132 our @ISA = 'DBIx::Class::Storage::Statistics';
136 ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_;
140 my $do = $schema->storage->debugobj(DBICTest::DebugObj->new);
144 is( $do->{_traced_sql}, $sql_trace );
146 is_deeply ( $do->{_traced_bind}, \@bind_trace );
149 # recreate test as seen in DBIx::Class::QueryLog
150 # the rationale is that if someone uses a non-IO::Handle object
151 # on CPAN, many are *bound* to use one on darkpan. Thus this
152 # test to ensure there is no future silent breakage
157 package DBICTest::_Printable;
160 my ($self, @args) = @_;
161 $output .= join('', @args);
165 $schema->storage->debugobj(undef);
166 $schema->storage->debug(1);
167 $schema->storage->debugfh( bless {}, "DBICTest::_Printable" );
168 $schema->storage->txn_do( sub { $schema->resultset('Artist')->count } );
174 ^ \QBEGIN WORK\E \s*?
175 ^ \QSELECT COUNT( * ) FROM artist me:\E \s*?
181 $schema->storage->debug(0);
182 $schema->storage->debugfh(undef);