Better (and much more precise) explanation of Moose/Moo subclassing
[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;
c9d29bb2 9use Path::Class qw/file/;
004d31fb 10
09d763c8 11BEGIN { delete @ENV{qw(DBIC_TRACE DBIC_TRACE_PROFILE DBICTEST_SQLITE_USE_FILE)} }
da69d72e 12
004d31fb 13my $schema = DBICTest->init_schema();
14
8d6b1478 15my $lfn = file("t/var/sql-$$.log");
64b3598f 16unlink $lfn or die $!
17 if -e $lfn;
18
f410ea47 19# make sure we are testing the vanilla debugger and not ::PrettyPrint
2cfc22dd 20require DBIx::Class::Storage::Statistics;
f410ea47 21$schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
004d31fb 22
23ok ( $schema->storage->debug(1), 'debug' );
64b3598f 24$schema->storage->debugfh($lfn->openw);
193195d8 25$schema->storage->debugfh->autoflush(1);
64b3598f 26$schema->resultset('CD')->count;
70f39278 27
64b3598f 28my @loglines = $lfn->slurp;
29is (@loglines, 1, 'one line of log');
30like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
70f39278 31
32$schema->storage->debugfh(undef);
64b3598f 33
34{
35 local $ENV{DBIC_TRACE} = "=$lfn";
36 unlink $lfn;
37
38 $schema->resultset('CD')->count;
39
40 my $schema2 = DBICTest->init_schema(no_deploy => 1);
41 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
42
43 my @loglines = $lfn->slurp;
44 is(@loglines, 2, '2 lines of log');
45 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
46 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
47
48 $schema->storage->debugobj->debugfh(undef)
49}
f410ea47 50
8d6b1478 51END {
52 unlink $lfn;
53}
54
70f39278 55open(STDERRCOPY, '>&STDERR');
70f39278 56close(STDERR);
f410ea47 57dies_ok {
64b3598f 58 $schema->resultset('CD')->search({})->count;
f410ea47 59} 'Died on closed FH';
60
70f39278 61open(STDERR, '>&STDERRCOPY');
62
2cfc22dd 63# test debugcb and debugobj protocol
e5d9ee92 64{
2cfc22dd 65 my $rs = $schema->resultset('CD')->search( {
66 artist => 1,
67 cdid => { -between => [ 1, 3 ] },
68 title => { '!=' => \[ '?', undef ] }
69 });
70
71 my $sql_trace = 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( ( artist = ? AND ( cdid BETWEEN ? AND ? ) AND title != ? ) )';
72 my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm>
73
74
75 my @args;
76 $schema->storage->debugcb(sub { push @args, @_ } );
77
78 $rs->all;
79
80 is_deeply( \@args, [
81 "SELECT",
82 sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ),
83 ]);
84
85 {
86 package DBICTest::DebugObj;
87 our @ISA = 'DBIx::Class::Storage::Statistics';
88
89 sub query_start {
90 my $self = shift;
91 ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_;
92 }
93 }
94
95 my $do = $schema->storage->debugobj(DBICTest::DebugObj->new);
96
97 $rs->all;
98
99 is( $do->{_traced_sql}, $sql_trace );
100
101 is_deeply ( $do->{_traced_bind}, \@bind_trace );
e5d9ee92 102}
103
c9d29bb2 104done_testing;