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