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