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