Merge 'replication_dedux' into 'trunk'
[dbsrgits/DBIx-Class.git] / t / 91debug.t
CommitLineData
004d31fb 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
8my $schema = DBICTest->init_schema();
9
e5d9ee92 10plan tests => 6;
004d31fb 11
12ok ( $schema->storage->debug(1), 'debug' );
13ok ( defined(
14 $schema->storage->debugfh(
15 IO::File->new('t/var/sql.log', 'w')
16 )
17 ),
18 'debugfh'
19 );
20
193195d8 21$schema->storage->debugfh->autoflush(1);
70f39278 22my $rs = $schema->resultset('CD')->search({});
23$rs->count();
24
25my $log = new IO::File('t/var/sql.log', 'r') or die($!);
26my $line = <$log>;
27$log->close();
28ok($line =~ /^SELECT COUNT/, 'Log success');
29
30$schema->storage->debugfh(undef);
31$ENV{'DBIC_TRACE'} = '=t/var/foo.log';
32$rs = $schema->resultset('CD')->search({});
33$rs->count();
34$log = new IO::File('t/var/foo.log', 'r') or die($!);
35$line = <$log>;
36$log->close();
37ok($line =~ /^SELECT COUNT/, 'Log success');
70f39278 38$schema->storage->debugobj->debugfh(undef);
39delete($ENV{'DBIC_TRACE'});
40open(STDERRCOPY, '>&STDERR');
41stat(STDERRCOPY); # nop to get warnings quiet
42close(STDERR);
43eval {
44 $rs = $schema->resultset('CD')->search({});
45 $rs->count();
46};
47ok($@, 'Died on closed FH');
48open(STDERR, '>&STDERRCOPY');
49
e5d9ee92 50# test trace output correctness for bind params
51{
52 my $sql = '';
53 $schema->storage->debugcb( sub { $sql = $_[1] } );
54
55 my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
56 like(
57 $sql,
58 qr/\QSELECT me.cdid, me.artist, me.title, me.year FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'\E/,
59 'got correct SQL with all bind parameters'
60 );
61}
62
004d31fb 631;