Commit | Line | Data |
4c248161 |
1 | use strict; |
2 | use warnings; |
3 | use Test::More; |
4 | |
a2287768 |
5 | plan tests => 12; |
4c248161 |
6 | |
7 | use lib qw(t/lib); |
8 | |
9 | use_ok('DBICTest'); |
c216324a |
10 | my $schema = DBICTest->init_schema(); |
4c248161 |
11 | |
12 | my $cbworks = 0; |
13 | |
c216324a |
14 | $schema->storage->debugcb(sub { $cbworks = 1; }); |
15 | $schema->storage->debug(0); |
16 | my $rs = $schema->resultset('CD')->search({}); |
4c248161 |
17 | $rs->count(); |
18 | ok(!$cbworks, 'Callback not called with debug disabled'); |
19 | |
c216324a |
20 | $schema->storage->debug(1); |
4c248161 |
21 | |
22 | $rs->count(); |
23 | ok($cbworks, 'Debug callback worked.'); |
24 | |
25 | my $prof = new DBIx::Test::Profiler(); |
c216324a |
26 | $schema->storage->debugobj($prof); |
4c248161 |
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 |
c216324a |
38 | $schema->txn_begin(); |
4c248161 |
39 | ok($prof->{'txn_begin'}, 'txn_begin called'); |
40 | |
c216324a |
41 | $rs = $schema->resultset('CD')->search({}); |
4c248161 |
42 | $rs->count(); |
43 | ok($prof->{'query_start'}, 'query_start called'); |
44 | ok($prof->{'query_end'}, 'query_end called'); |
45 | |
c216324a |
46 | $schema->txn_commit(); |
4c248161 |
47 | ok($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 |
56 | ok($prof->{'txn_rollback'}, 'txn_rollback called'); |
57 | |
c216324a |
58 | $schema->storage->debug(0); |
4c248161 |
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; |