9 DBICTEST_SQLITE_USE_FILE
10 DBICTEST_VIA_REPLICATED
20 use Path::Class qw/file/;
22 my $schema = DBICTest->init_schema();
24 my $lfn = file("t/var/sql-$$.log");
28 # make sure we are testing the vanilla debugger and not ::PrettyPrint
29 require DBIx::Class::Storage::Statistics;
30 $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
32 ok ( $schema->storage->debug(1), 'debug' );
33 $schema->storage->debugfh($lfn->openw);
34 $schema->storage->debugfh->autoflush(1);
35 $schema->resultset('CD')->count;
37 my @loglines = $lfn->slurp;
38 is (@loglines, 1, 'one line of log');
39 like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
41 $schema->storage->debugfh(undef);
44 local $ENV{DBIC_TRACE} = "=$lfn";
47 $schema->resultset('CD')->count;
49 my $schema2 = DBICTest->init_schema(no_deploy => 1);
50 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
52 my @loglines = $lfn->slurp;
53 is(@loglines, 2, '2 lines of log');
54 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
55 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
57 $schema->storage->debugobj->debugfh(undef)
64 open(STDERRCOPY, '>&STDERR');
66 my $exception_line_number;
67 # STDERR will be closed, no T::B diag in blocks
70 $exception_line_number = __LINE__ + 1; # important for test, do not reformat
71 $schema->resultset('CD')->search({})->count;
76 open(STDERR, '>&STDERRCOPY');
80 \QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)\E
82 \Qat @{[__FILE__]} line $exception_line_number\E$
84 or diag "Unexpected exception text:\n\n$exception\n";
88 local $SIG{__WARN__} = sub { push @warnings, @_ if $_[0] =~ /character/i };
90 open(STDERR, '>', File::Spec->devnull) or die $!;
91 $schema->resultset('CD')->search({ title => "\x{1f4a9}" })->count;
98 open(STDERR, '>&STDERRCOPY');
101 die "How did that fail... $exception"
104 is_deeply(\@warnings, [], 'No warnings with unicode on STDERR');
106 # test debugcb and debugobj protocol
108 my $rs = $schema->resultset('CD')->search( {
110 cdid => { -between => [ 1, 3 ] },
111 title => { '!=' => \[ '?', undef ] }
114 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 != ? ) )';
115 my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm>
119 $schema->storage->debugcb(sub { push @args, @_ } );
125 sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ),
129 package DBICTest::DebugObj;
130 our @ISA = 'DBIx::Class::Storage::Statistics';
134 ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_;
138 my $do = $schema->storage->debugobj(DBICTest::DebugObj->new);
142 is( $do->{_traced_sql}, $sql_trace );
144 is_deeply ( $do->{_traced_bind}, \@bind_trace );
147 # recreate test as seen in DBIx::Class::QueryLog
148 # the rationale is that if someone uses a non-IO::Handle object
149 # on CPAN, many are *bound* to use one on darkpan. Thus this
150 # test to ensure there is no future silent breakage
155 package DBICTest::_Printable;
158 my ($self, @args) = @_;
159 $output .= join('', @args);
163 $schema->storage->debugobj(undef);
164 $schema->storage->debug(1);
165 $schema->storage->debugfh( bless {}, "DBICTest::_Printable" );
166 $schema->storage->txn_do( sub { $schema->resultset('Artist')->count } );
172 ^ \QBEGIN WORK\E \s*?
173 ^ \QSELECT COUNT( * ) FROM artist me:\E \s*?
179 $schema->storage->debug(0);
180 $schema->storage->debugfh(undef);