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