Move storage tests to their own dir
[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
10 my $schema = DBICTest->init_schema();
11
12 plan tests => 7;
13
14 ok ( $schema->storage->debug(1), 'debug' );
15 ok ( defined(
16        $schema->storage->debugfh(
17          IO::File->new('t/var/sql.log', 'w')
18        )
19      ),
20      'debugfh'
21    );
22
23 $schema->storage->debugfh->autoflush(1);
24 my $rs = $schema->resultset('CD')->search({});
25 $rs->count();
26
27 my $log = new IO::File('t/var/sql.log', 'r') or die($!);
28 my $line = <$log>;
29 $log->close();
30 ok($line =~ /^SELECT COUNT/, 'Log success');
31
32 $schema->storage->debugfh(undef);
33 $ENV{'DBIC_TRACE'} = '=t/var/foo.log';
34 $rs = $schema->resultset('CD')->search({});
35 $rs->count();
36 $log = new IO::File('t/var/foo.log', 'r') or die($!);
37 $line = <$log>;
38 $log->close();
39 ok($line =~ /^SELECT COUNT/, 'Log success');
40 $schema->storage->debugobj->debugfh(undef);
41 delete($ENV{'DBIC_TRACE'});
42 open(STDERRCOPY, '>&STDERR');
43 stat(STDERRCOPY); # nop to get warnings quiet
44 close(STDERR);
45 eval {
46     $rs = $schema->resultset('CD')->search({});
47     $rs->count();
48 };
49 ok($@, 'Died on closed FH');
50 open(STDERR, '>&STDERRCOPY');
51
52 # test trace output correctness for bind params
53 {
54     my ($sql, @bind);
55     $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
56
57     my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
58     is_same_sql_bind(
59         $sql, \@bind,
60         "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'",
61         [qw/'1' '1' '3'/],
62         'got correct SQL with all bind parameters (debugcb)'
63     );
64
65     @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
66     is_same_sql_bind(
67         $sql, \@bind,
68         "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'"],
69         'got correct SQL with all bind parameters (debugobj)'
70     );
71 }
72
73 1;