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