POD fixes
[dbsrgits/DBIx-Class.git] / t / 91debug.t
CommitLineData
004d31fb 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
67e1ac6d 7use DBIC::DebugObj;
949172b0 8use DBIC::SqlMakerTest;
004d31fb 9
10my $schema = DBICTest->init_schema();
11
9b459129 12plan tests => 7;
004d31fb 13
14ok ( $schema->storage->debug(1), 'debug' );
15ok ( defined(
16 $schema->storage->debugfh(
17 IO::File->new('t/var/sql.log', 'w')
18 )
19 ),
20 'debugfh'
21 );
22
193195d8 23$schema->storage->debugfh->autoflush(1);
70f39278 24my $rs = $schema->resultset('CD')->search({});
25$rs->count();
26
27my $log = new IO::File('t/var/sql.log', 'r') or die($!);
28my $line = <$log>;
29$log->close();
30ok($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();
39ok($line =~ /^SELECT COUNT/, 'Log success');
70f39278 40$schema->storage->debugobj->debugfh(undef);
41delete($ENV{'DBIC_TRACE'});
42open(STDERRCOPY, '>&STDERR');
43stat(STDERRCOPY); # nop to get warnings quiet
44close(STDERR);
45eval {
46 $rs = $schema->resultset('CD')->search({});
47 $rs->count();
48};
49ok($@, 'Died on closed FH');
50open(STDERR, '>&STDERRCOPY');
51
e5d9ee92 52# test trace output correctness for bind params
53{
9b459129 54 my ($sql, @bind) = ('');
e5d9ee92 55 $schema->storage->debugcb( sub { $sql = $_[1] } );
56
57 my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
9b459129 58 is_same_sql_bind(
59 $sql, [],
42a7de49 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'", [],
9b459129 61 'got correct SQL with all bind parameters (debugcb)'
62 );
63
64 $schema->storage->debugcb(undef);
67e1ac6d 65 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
9b459129 66 @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
67 is_same_sql_bind(
68 $sql, \@bind,
42a7de49 69 "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 70 'got correct SQL with all bind parameters (debugobj)'
e5d9ee92 71 );
72}
73
004d31fb 741;