Cleanup/improve the leaktest a bit
[dbsrgits/DBIx-Class-Historic.git] / t / storage / stats.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 plan tests => 12;
6
7 use lib qw(t/lib);
8
9 use_ok('DBICTest');
10 my $schema = DBICTest->init_schema();
11
12 my $cbworks = 0;
13
14 $schema->storage->debugcb(sub { $cbworks = 1; });
15 $schema->storage->debug(0);
16 my $rs = $schema->resultset('CD')->search({});
17 $rs->count();
18 ok(!$cbworks, 'Callback not called with debug disabled');
19
20 $schema->storage->debug(1);
21
22 $rs->count();
23 ok($cbworks, 'Debug callback worked.');
24
25 my $prof = new DBIx::Test::Profiler();
26 $schema->storage->debugobj($prof);
27
28 # Test non-transaction calls.
29 $rs->count();
30 ok($prof->{'query_start'}, 'query_start called');
31 ok($prof->{'query_end'}, 'query_end called');
32 ok(!$prof->{'txn_begin'}, 'txn_begin not called');
33 ok(!$prof->{'txn_commit'}, 'txn_commit not called');
34
35 $prof->reset();
36
37 # Test transaction calls
38 $schema->txn_begin();
39 ok($prof->{'txn_begin'}, 'txn_begin called');
40
41 $rs = $schema->resultset('CD')->search({});
42 $rs->count();
43 ok($prof->{'query_start'}, 'query_start called');
44 ok($prof->{'query_end'}, 'query_end called');
45
46 $schema->txn_commit();
47 ok($prof->{'txn_commit'}, 'txn_commit called');
48
49 $prof->reset();
50
51 # Test a rollback
52 $schema->txn_begin();
53 $rs = $schema->resultset('CD')->search({});
54 $rs->count();
55 $schema->txn_rollback();
56 ok($prof->{'txn_rollback'}, 'txn_rollback called');
57
58 $schema->storage->debug(0);
59
60 package DBIx::Test::Profiler;
61 use strict;
62
63 sub new {
64     my $self = bless({});
65 }
66
67 sub query_start {
68     my $self = shift();
69     $self->{'query_start'} = 1;
70 }
71
72 sub query_end {
73     my $self = shift();
74     $self->{'query_end'} = 1;
75 }
76
77 sub txn_begin {
78     my $self = shift();
79     $self->{'txn_begin'} = 1;
80 }
81
82 sub txn_rollback {
83     my $self = shift();
84     $self->{'txn_rollback'} = 1;
85 }
86
87 sub txn_commit {
88     my $self = shift();
89     $self->{'txn_commit'} = 1;
90 }
91
92 sub reset {
93     my $self = shift();
94
95     $self->{'query_start'} = 0;
96     $self->{'query_end'} = 0;
97     $self->{'txn_begin'} = 0;
98     $self->{'txn_rollback'} = 0;
99     $self->{'txn_end'} = 0;
100 }
101
102 1;