fix and regression test for RT #62642
[dbsrgits/DBIx-Class.git] / t / storage / debug.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use DBIC::DebugObj;
9 use DBIC::SqlMakerTest;
10 use Path::Class qw/file/;
11
12 my $schema = DBICTest->init_schema();
13
14 # make sure we are testing the vanilla debugger and not ::PrettyPrint
15 $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
16
17 ok ( $schema->storage->debug(1), 'debug' );
18 $schema->storage->debugfh(file('t/var/sql.log')->openw);
19
20 $schema->storage->debugfh->autoflush(1);
21 my $rs = $schema->resultset('CD')->search({});
22 $rs->count();
23
24 my $log = file('t/var/sql.log')->openr;
25 my $line = <$log>;
26 $log->close();
27 like($line, qr/^SELECT COUNT/, 'Log success');
28
29 $schema->storage->debugfh(undef);
30 $ENV{'DBIC_TRACE'} = '=t/var/foo.log';
31 $rs = $schema->resultset('CD')->search({});
32 $rs->count();
33 $log = file('t/var/foo.log')->openr;
34 $line = <$log>;
35 $log->close();
36 like($line, qr/^SELECT COUNT/, 'Log success');
37 $schema->storage->debugobj->debugfh(undef);
38 delete($ENV{'DBIC_TRACE'});
39
40 open(STDERRCOPY, '>&STDERR');
41 stat(STDERRCOPY); # nop to get warnings quiet
42 close(STDERR);
43 dies_ok {
44     $rs = $schema->resultset('CD')->search({});
45     $rs->count();
46 } 'Died on closed FH';
47
48 open(STDERR, '>&STDERRCOPY');
49
50 # test trace output correctness for bind params
51 {
52     my ($sql, @bind);
53     $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
54
55     my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
56     is_same_sql_bind(
57         $sql, \@bind,
58         "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND (cdid BETWEEN ? AND ?) )",
59         [qw/'1' '1' '3'/],
60         'got correct SQL with all bind parameters (debugcb)'
61     );
62
63     @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
64     is_same_sql_bind(
65         $sql, \@bind,
66         "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND (cdid BETWEEN ? AND ?) )", ["'1'", "'1'", "'3'"],
67         'got correct SQL with all bind parameters (debugobj)'
68     );
69 }
70
71 done_testing;