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