check database driver dependencies for tests
[dbsrgits/DBIx-Class.git] / t / storage / debug.t
CommitLineData
004d31fb 1use strict;
f54428ab 2use warnings;
64b3598f 3no warnings 'once';
004d31fb 4
5use Test::More;
f410ea47 6use Test::Exception;
004d31fb 7use lib qw(t/lib);
8use DBICTest;
67e1ac6d 9use DBIC::DebugObj;
949172b0 10use DBIC::SqlMakerTest;
c9d29bb2 11use Path::Class qw/file/;
004d31fb 12
13my $schema = DBICTest->init_schema();
14
64b3598f 15my $lfn = file('t/var/sql.log');
16unlink $lfn or die $!
17 if -e $lfn;
18
f410ea47 19# make sure we are testing the vanilla debugger and not ::PrettyPrint
20$schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
004d31fb 21
22ok ( $schema->storage->debug(1), 'debug' );
64b3598f 23$schema->storage->debugfh($lfn->openw);
193195d8 24$schema->storage->debugfh->autoflush(1);
64b3598f 25$schema->resultset('CD')->count;
70f39278 26
64b3598f 27my @loglines = $lfn->slurp;
28is (@loglines, 1, 'one line of log');
29like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
70f39278 30
31$schema->storage->debugfh(undef);
64b3598f 32
33{
34 local $ENV{DBIC_TRACE} = "=$lfn";
35 unlink $lfn;
36
37 $schema->resultset('CD')->count;
38
39 my $schema2 = DBICTest->init_schema(no_deploy => 1);
40 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
41
42 my @loglines = $lfn->slurp;
43 is(@loglines, 2, '2 lines of log');
44 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
45 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
46
47 $schema->storage->debugobj->debugfh(undef)
48}
f410ea47 49
70f39278 50open(STDERRCOPY, '>&STDERR');
70f39278 51close(STDERR);
f410ea47 52dies_ok {
64b3598f 53 $schema->resultset('CD')->search({})->count;
f410ea47 54} 'Died on closed FH';
55
70f39278 56open(STDERR, '>&STDERRCOPY');
57
e5d9ee92 58# test trace output correctness for bind params
59{
af6aac2d 60 my ($sql, @bind);
61 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
e5d9ee92 62
63 my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
9b459129 64 is_same_sql_bind(
af6aac2d 65 $sql, \@bind,
0491b597 66 "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND (cdid BETWEEN ? AND ?) )",
af6aac2d 67 [qw/'1' '1' '3'/],
9b459129 68 'got correct SQL with all bind parameters (debugcb)'
69 );
70
9b459129 71 @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
72 is_same_sql_bind(
73 $sql, \@bind,
42a7de49 74 "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 75 'got correct SQL with all bind parameters (debugobj)'
e5d9ee92 76 );
77}
78
c9d29bb2 79done_testing;