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