Debian Codegraph @

t/018_mock_statement_track.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9 use_ok('DBD::Mock');
10 }
11
12 { # check the available methods
13
14 can_ok("DBD::Mock::StatementTrack", 'new');
15 my $st_track = DBD::Mock::StatementTrack->new();
16 isa_ok($st_track, 'DBD::Mock::StatementTrack');
17
18 can_ok($st_track, 'num_fields');
19 can_ok($st_track, 'num_params');
20 can_ok($st_track, 'bound_param');
21 can_ok($st_track, 'bound_param_trailing');
22 can_ok($st_track, 'is_active');
23 can_ok($st_track, 'is_finished');
24 can_ok($st_track, 'mark_executed');
25 can_ok($st_track, 'next_record');
26 can_ok($st_track, 'is_depleted');
27 can_ok($st_track, 'to_string');
28 can_ok($st_track, 'is_executed');
29 can_ok($st_track, 'statement');
30 can_ok($st_track, 'current_record_num');
31 can_ok($st_track, 'return_data');
32 can_ok($st_track, 'fields');
33 can_ok($st_track, 'bound_params');
34 }
35
36 { # check the default state
37
38 my $st_track = DBD::Mock::StatementTrack->new();
39 isa_ok($st_track, 'DBD::Mock::StatementTrack');
40
41 is($st_track->num_fields(), 0, '... we have no fields in the default');
42 is_deeply($st_track->fields(), [], '... we have no fields in the default');
43
44 is($st_track->num_params(), 0, '... we have no bound params in the default');
45 is_deeply($st_track->bound_params(), [], '... we have no bound params in the default');
46
47 is_deeply($st_track->return_data(), [], '... we have no return data in the default');
48
49 is($st_track->current_record_num(), 0, '... our current record num is 0 in the default');
50
51 is($st_track->statement(), '', '... our statement is a blank string in the default');
52 is($st_track->is_executed(), 'no', '... our statement is not executed in the default');
53
54 ok($st_track->is_depleted(), '... the default state is depleted');
55 ok(!defined($st_track->next_record()), '... the default state has no next record since it is depleted');
56
57 is($st_track->is_finished(), 'no', '... our statement is not finished in the default');
58
59 is($st_track->is_active(), 0, '... the default state is not active');
60 }
61
62 { # check a pre-defined state
63
64 my %params = (
65 return_data => [ [1, 1, 1], [2, 2, 2], [3, 3, 3] ],
66 fields => [ 'foo', 'bar', 'baz' ],
67 bound_params => [ 'baz' ],
68 statement => 'SELECT foo FROM bar WHERE baz = ?'
69 );
70
71 my $st_track = DBD::Mock::StatementTrack->new(%params);
72 isa_ok($st_track, 'DBD::Mock::StatementTrack');
73
74 is($st_track->num_fields(), 3, '... we have the expected num of fields');
75 is_deeply($st_track->fields(), $params{fields}, '... we have the expected fields');
76
77 is($st_track->num_params(), 1, '... we have the expected num of bound params');
78 is_deeply($st_track->bound_params(), $params{bound_params}, '... we have the expected bound params');
79
80 is_deeply($st_track->return_data(), $params{return_data}, '... we have the expected return data');
81
82 is($st_track->current_record_num(), 0, '... our current record num is 0 in the default');
83
84 is($st_track->statement(), $params{statement}, '... our statement as expected ');
85 is($st_track->is_executed(), 'no', '... our statement is not executed');
86
87 ok(!$st_track->is_depleted(), '... the state is not depleted');
88
89 is($st_track->is_finished(), 'no', '... our statement is not finished');
90
91 is($st_track->is_active(), 0, '... the default state is active');
92
93 # now lets alter that state
94 # and make sure changes reflect
95
96 is_deeply(
97 $st_track->bound_param(2, 'foo'),
98 [ 'baz', 'foo' ],
99 '... we have the expected bound params');
100
101 $st_track->bound_param_trailing('bar', 'foobar');
102 is_deeply(
103 $st_track->bound_params(),
104 [ 'baz', 'foo', 'bar', 'foobar' ],
105 '... we have the expected bound params');
106
107 is($st_track->num_params(), 4, '... we have the expected num of bound params');
108
109 {
110 my $old_SQL = $st_track->statement();
111
112 my $SQL = 'INSERT INTO foo (foo, bar, baz) VALUE(1, 2, 3)';
113 $st_track->statement($SQL);
114
115 is($st_track->statement(), $SQL, '... our statement as expected ');
116 is($st_track->is_active(), 0, '... with an INSERT we are not considered active');
117
118 $st_track->statement($old_SQL);
119
120 is($st_track->statement(), $old_SQL, '... restore our statement');
121 }
122
123 $st_track->mark_executed();
124 is($st_track->is_executed(), 'yes', '... our statement is now executed');
125 is($st_track->current_record_num(), 0, '... our current record num is 0');
126
127 is($st_track->is_active(), 1, '... we are active now that we are executed');
128
129 for (1 .. 3) {
130 ok(!$st_track->is_depleted(), '... the state is not depleted');
131 is_deeply(
132 $st_track->next_record(),
133 [ $_, $_, $_ ],
134 '... got the next record as expected');
135 is($st_track->current_record_num(), $_, '... our current record num is now ' . $_);
136 }
137
138 ok(!defined($st_track->next_record()), '... no more records');
139 ok($st_track->is_depleted(), '... we are now depleted');
140
141 is($st_track->is_active(), 0, '... we are no longer active now that we are depleted');
142
143 is($st_track->is_finished(), 'no', '... passing in nothing just returns the value');
144
145 $st_track->is_finished('yes');
146 is($st_track->is_finished(), 'yes', '... our statement is now finished');
147
148 $st_track->is_finished('nothing');
149 is($st_track->is_finished(), 'no', '... our statement is no longer finished');
150
151 }
152
153 done_testing();