Stop permanently enabling autoflush on the debug filehandle
[dbsrgits/DBIx-Class.git] / t / storage / debug.t
CommitLineData
004d31fb 1use strict;
f54428ab 2use warnings;
64b3598f 3no warnings 'once';
004d31fb 4
5use Test::More;
f410ea47 6use Test::Exception;
68367f1f 7use Try::Tiny;
004d31fb 8use lib qw(t/lib);
9use DBICTest;
c9d29bb2 10use Path::Class qw/file/;
004d31fb 11
09d763c8 12BEGIN { delete @ENV{qw(DBIC_TRACE DBIC_TRACE_PROFILE DBICTEST_SQLITE_USE_FILE)} }
da69d72e 13
004d31fb 14my $schema = DBICTest->init_schema();
15
8d6b1478 16my $lfn = file("t/var/sql-$$.log");
64b3598f 17unlink $lfn or die $!
18 if -e $lfn;
19
f410ea47 20# make sure we are testing the vanilla debugger and not ::PrettyPrint
2cfc22dd 21require DBIx::Class::Storage::Statistics;
f410ea47 22$schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
004d31fb 23
24ok ( $schema->storage->debug(1), 'debug' );
64b3598f 25$schema->storage->debugfh($lfn->openw);
64b3598f 26$schema->resultset('CD')->count;
70f39278 27
64b3598f 28my @loglines = $lfn->slurp;
29is (@loglines, 1, 'one line of log');
30like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
70f39278 31
32$schema->storage->debugfh(undef);
64b3598f 33
34{
35 local $ENV{DBIC_TRACE} = "=$lfn";
36 unlink $lfn;
37
38 $schema->resultset('CD')->count;
39
40 my $schema2 = DBICTest->init_schema(no_deploy => 1);
41 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
42
43 my @loglines = $lfn->slurp;
44 is(@loglines, 2, '2 lines of log');
45 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
46 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
47
48 $schema->storage->debugobj->debugfh(undef)
49}
f410ea47 50
8d6b1478 51END {
52 unlink $lfn;
53}
54
70f39278 55open(STDERRCOPY, '>&STDERR');
68367f1f 56
57# STDERR will be closed, no T::B diag in blocks
58my $exception = try {
59 close(STDERR);
64b3598f 60 $schema->resultset('CD')->search({})->count;
68367f1f 61} catch {
62 $_
63} finally {
64 # restore STDERR
65 open(STDERR, '>&STDERRCOPY');
66};
67
68like $exception, qr/\QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)/;
69
f410ea47 70
70f39278 71
2cfc22dd 72# test debugcb and debugobj protocol
e5d9ee92 73{
2cfc22dd 74 my $rs = $schema->resultset('CD')->search( {
75 artist => 1,
76 cdid => { -between => [ 1, 3 ] },
77 title => { '!=' => \[ '?', undef ] }
78 });
79
80 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 != ? ) )';
81 my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm>
82
83
84 my @args;
85 $schema->storage->debugcb(sub { push @args, @_ } );
86
87 $rs->all;
88
89 is_deeply( \@args, [
90 "SELECT",
91 sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ),
92 ]);
93
94 {
95 package DBICTest::DebugObj;
96 our @ISA = 'DBIx::Class::Storage::Statistics';
97
98 sub query_start {
99 my $self = shift;
100 ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_;
101 }
102 }
103
104 my $do = $schema->storage->debugobj(DBICTest::DebugObj->new);
105
106 $rs->all;
107
108 is( $do->{_traced_sql}, $sql_trace );
109
110 is_deeply ( $do->{_traced_bind}, \@bind_trace );
e5d9ee92 111}
112
c9d29bb2 113done_testing;