Commit | Line | Data |
c6d74d3e |
1 | package # hide from PAUSE |
2 | DBICTest; |
1c339d71 |
3 | |
4 | use strict; |
5 | use warnings; |
ab340f7f |
6 | use DBICTest::AuthorCheck; |
1c339d71 |
7 | use DBICTest::Schema; |
a258bfc7 |
8 | use Carp; |
17e7d7f0 |
9 | |
10 | =head1 NAME |
11 | |
12 | DBICTest - Library to be used by DBIx::Class test scripts. |
13 | |
14 | =head1 SYNOPSIS |
15 | |
16 | use lib qw(t/lib); |
17 | use DBICTest; |
18 | use Test::More; |
19 | |
20 | my $schema = DBICTest->init_schema(); |
21 | |
22 | =head1 DESCRIPTION |
23 | |
24 | This module provides the basic utilities to write tests against |
25 | DBIx::Class. |
26 | |
27 | =head1 METHODS |
28 | |
29 | =head2 init_schema |
30 | |
31 | my $schema = DBICTest->init_schema( |
32 | no_deploy=>1, |
33 | no_populate=>1, |
2bf79155 |
34 | storage_type=>'::DBI::Replicated', |
cb6ec758 |
35 | storage_type_args=>{ |
d7a58a29 |
36 | balancer_type=>'DBIx::Class::Storage::DBI::Replicated::Balancer::Random' |
cb6ec758 |
37 | }, |
17e7d7f0 |
38 | ); |
39 | |
40 | This method removes the test SQLite database in t/var/DBIxClass.db |
41 | and then creates a new, empty database. |
42 | |
43 | This method will call deploy_schema() by default, unless the |
44 | no_deploy flag is set. |
45 | |
46 | Also, by default, this method will call populate_schema() by |
47 | default, unless the no_deploy or no_populate flags are set. |
48 | |
49 | =cut |
1c339d71 |
50 | |
857d66ca |
51 | sub has_custom_dsn { |
d7a58a29 |
52 | return $ENV{"DBICTEST_DSN"} ? 1:0; |
857d66ca |
53 | } |
54 | |
55 | sub _sqlite_dbfilename { |
bcb3e850 |
56 | return "t/var/DBIxClass.db"; |
57 | } |
58 | |
59 | sub _sqlite_dbname { |
481df957 |
60 | my $self = shift; |
61 | my %args = @_; |
bcb3e850 |
62 | return $self->_sqlite_dbfilename if $args{sqlite_use_file} or $ENV{"DBICTEST_SQLITE_USE_FILE"}; |
d7a58a29 |
63 | return ":memory:"; |
857d66ca |
64 | } |
65 | |
579ca3f7 |
66 | sub _database { |
17e7d7f0 |
67 | my $self = shift; |
481df957 |
68 | my %args = @_; |
17e7d7f0 |
69 | |
a258bfc7 |
70 | if ($ENV{DBICTEST_DSN}) { |
71 | return ( |
72 | (map { $ENV{"DBICTEST_${_}"} || '' } qw/DSN DBUSER DBPASS/), |
73 | { AutoCommit => 1, %args }, |
74 | ); |
75 | } |
76 | my $db_file = $self->_sqlite_dbname(%args); |
17e7d7f0 |
77 | |
a258bfc7 |
78 | for ($db_file, "${db_file}-journal") { |
79 | next unless -e $_; |
80 | unlink ($_) or carp ( |
81 | "Unable to unlink existing test database file $_ ($!), creation of fresh database / further tests may fail!\n" |
82 | ); |
83 | } |
17e7d7f0 |
84 | |
a258bfc7 |
85 | mkdir("t/var") unless -d "t/var"; |
3943fd63 |
86 | |
a258bfc7 |
87 | return ("dbi:SQLite:${db_file}", '', '', { |
88 | AutoCommit => 1, |
89 | |
90 | # this is executed on every connect, and thus installs a disconnect/DESTROY |
91 | # guard for every new $dbh |
92 | on_connect_do => sub { |
93 | my $storage = shift; |
94 | my $dbh = $storage->_get_dbh; |
95 | |
96 | # no fsync on commit |
97 | $dbh->do ('PRAGMA synchronous = OFF'); |
98 | |
99 | # set a *DBI* disconnect callback, to make sure the physical SQLite |
100 | # file is still there (i.e. the test does not attempt to delete |
101 | # an open database, which fails on Win32) |
d9c17594 |
102 | if (my $guard_cb = __mk_disconnect_guard($db_file)) { |
a258bfc7 |
103 | $dbh->{Callbacks} = { |
d9c17594 |
104 | connect => sub { $guard_cb->('connect') }, |
105 | disconnect => sub { $guard_cb->('disconnect') }, |
106 | DESTROY => sub { $guard_cb->('DESTROY') }, |
a258bfc7 |
107 | }; |
108 | } |
109 | }, |
110 | %args, |
111 | }); |
579ca3f7 |
112 | } |
113 | |
d9c17594 |
114 | sub __mk_disconnect_guard { |
115 | my $db_file = shift; |
116 | return unless -f $db_file; |
117 | |
118 | my $orig_inode = (stat($db_file))[1] |
119 | or return; |
120 | |
121 | my $clan_connect_caller = '*UNKNOWN*'; |
122 | my $i; |
123 | while ( my ($pack, $file, $line) = caller(++$i) ) { |
124 | next if $file eq __FILE__; |
125 | next if $pack =~ /^DBIx::Class|^Try::Tiny/; |
126 | $clan_connect_caller = "$file line $line"; |
127 | } |
128 | |
129 | my $failed_once = 0; |
130 | my $connected = 1; |
131 | |
132 | return sub { |
133 | return if $failed_once; |
134 | |
135 | my $event = shift; |
136 | if ($event eq 'connect') { |
137 | # this is necessary in case we are disconnected and connected again, all within the same $dbh object |
138 | $connected = 1; |
139 | return; |
140 | } |
141 | elsif ($event eq 'disconnect') { |
142 | $connected = 0; |
143 | } |
144 | elsif ($event eq 'DESTROY' and ! $connected ) { |
145 | return; |
146 | } |
147 | |
148 | my $fail_reason; |
149 | if (! -e $db_file) { |
150 | $fail_reason = 'is missing'; |
151 | } |
152 | else { |
153 | my $cur_inode = (stat($db_file))[1]; |
154 | |
155 | if ($orig_inode != $cur_inode) { |
156 | # pack/unpack to match the unsigned longs returned by `stat` |
157 | $fail_reason = sprintf 'was recreated (initially inode %s, now %s)', ( |
158 | map { unpack ('L', pack ('l', $_) ) } ($orig_inode, $cur_inode ) |
159 | ); |
160 | } |
161 | } |
162 | |
163 | if ($fail_reason) { |
164 | $failed_once++; |
165 | |
166 | require Test::Builder; |
167 | my $t = Test::Builder->new; |
168 | local $Test::Builder::Level = $Test::Builder::Level + 3; |
169 | $t->ok (0, |
170 | "$db_file originally created at $clan_connect_caller $fail_reason before $event " |
171 | . 'of DBI handle - a strong indicator that the database file was tampered with while ' |
172 | . 'still being open. This action would fail massively if running under Win32, hence ' |
173 | . 'we make sure it fails on any OS :)' |
174 | ); |
175 | } |
176 | |
177 | return; # this empty return is a DBI requirement |
178 | }; |
179 | } |
180 | |
579ca3f7 |
181 | sub init_schema { |
182 | my $self = shift; |
183 | my %args = @_; |
184 | |
185 | my $schema; |
d7a58a29 |
186 | |
640ac94c |
187 | if ($args{compose_connection}) { |
188 | $schema = DBICTest::Schema->compose_connection( |
481df957 |
189 | 'DBICTest', $self->_database(%args) |
640ac94c |
190 | ); |
191 | } else { |
192 | $schema = DBICTest::Schema->compose_namespace('DBICTest'); |
193 | } |
a258bfc7 |
194 | |
2bf79155 |
195 | if( $args{storage_type}) { |
d7a58a29 |
196 | $schema->storage_type($args{storage_type}); |
197 | } |
a258bfc7 |
198 | |
579ca3f7 |
199 | if ( !$args{no_connect} ) { |
481df957 |
200 | $schema = $schema->connect($self->_database(%args)); |
3943fd63 |
201 | } |
a258bfc7 |
202 | |
17e7d7f0 |
203 | if ( !$args{no_deploy} ) { |
89cf6a70 |
204 | __PACKAGE__->deploy_schema( $schema, $args{deploy_args} ); |
205 | __PACKAGE__->populate_schema( $schema ) |
206 | if( !$args{no_populate} ); |
17e7d7f0 |
207 | } |
208 | return $schema; |
209 | } |
210 | |
211 | =head2 deploy_schema |
212 | |
213 | DBICTest->deploy_schema( $schema ); |
214 | |
215 | This method does one of two things to the schema. It can either call |
216 | the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment |
217 | variable is set, otherwise the default is to read in the t/lib/sqlite.sql |
218 | file and execute the SQL within. Either way you end up with a fresh set |
219 | of tables for testing. |
220 | |
221 | =cut |
222 | |
223 | sub deploy_schema { |
224 | my $self = shift; |
89cf6a70 |
225 | my $schema = shift; |
226 | my $args = shift || {}; |
17e7d7f0 |
227 | |
106d5f3b |
228 | if ($ENV{"DBICTEST_SQLT_DEPLOY"}) { |
06e15b8e |
229 | $schema->deploy($args); |
17e7d7f0 |
230 | } else { |
231 | open IN, "t/lib/sqlite.sql"; |
232 | my $sql; |
233 | { local $/ = undef; $sql = <IN>; } |
234 | close IN; |
ebf846e8 |
235 | for my $chunk ( split (/;\s*\n+/, $sql) ) { |
236 | if ( $chunk =~ / ^ (?! --\s* ) \S /xm ) { # there is some real sql in the chunk - a non-space at the start of the string which is not a comment |
856e2d17 |
237 | $schema->storage->dbh_do(sub { $_[1]->do($chunk) }) or print "Error on SQL: $chunk\n"; |
ebf846e8 |
238 | } |
239 | } |
17e7d7f0 |
240 | } |
89cf6a70 |
241 | return; |
17e7d7f0 |
242 | } |
243 | |
244 | =head2 populate_schema |
245 | |
246 | DBICTest->populate_schema( $schema ); |
247 | |
248 | After you deploy your schema you can use this method to populate |
249 | the tables with test data. |
250 | |
251 | =cut |
252 | |
253 | sub populate_schema { |
254 | my $self = shift; |
255 | my $schema = shift; |
256 | |
972015a7 |
257 | $schema->populate('Genre', [ |
258 | [qw/genreid name/], |
259 | [qw/1 emo /], |
260 | ]); |
261 | |
17e7d7f0 |
262 | $schema->populate('Artist', [ |
77211009 |
263 | [ qw/artistid name/ ], |
264 | [ 1, 'Caterwauler McCrae' ], |
265 | [ 2, 'Random Boy Band' ], |
266 | [ 3, 'We Are Goth' ], |
17e7d7f0 |
267 | ]); |
268 | |
269 | $schema->populate('CD', [ |
972015a7 |
270 | [ qw/cdid artist title year genreid/ ], |
271 | [ 1, 1, "Spoonful of bees", 1999, 1 ], |
17e7d7f0 |
272 | [ 2, 1, "Forkful of bees", 2001 ], |
273 | [ 3, 1, "Caterwaulin' Blues", 1997 ], |
274 | [ 4, 2, "Generic Manufactured Singles", 2001 ], |
275 | [ 5, 3, "Come Be Depressed With Us", 1998 ], |
276 | ]); |
277 | |
278 | $schema->populate('LinerNotes', [ |
279 | [ qw/liner_id notes/ ], |
280 | [ 2, "Buy Whiskey!" ], |
281 | [ 4, "Buy Merch!" ], |
282 | [ 5, "Kill Yourself!" ], |
283 | ]); |
284 | |
285 | $schema->populate('Tag', [ |
286 | [ qw/tagid cd tag/ ], |
287 | [ 1, 1, "Blue" ], |
288 | [ 2, 2, "Blue" ], |
289 | [ 3, 3, "Blue" ], |
290 | [ 4, 5, "Blue" ], |
291 | [ 5, 2, "Cheesy" ], |
292 | [ 6, 4, "Cheesy" ], |
293 | [ 7, 5, "Cheesy" ], |
294 | [ 8, 2, "Shiny" ], |
295 | [ 9, 4, "Shiny" ], |
296 | ]); |
297 | |
298 | $schema->populate('TwoKeys', [ |
299 | [ qw/artist cd/ ], |
300 | [ 1, 1 ], |
301 | [ 1, 2 ], |
302 | [ 2, 2 ], |
303 | ]); |
304 | |
305 | $schema->populate('FourKeys', [ |
3bd6e3e0 |
306 | [ qw/foo bar hello goodbye sensors/ ], |
307 | [ 1, 2, 3, 4, 'online' ], |
308 | [ 5, 4, 3, 6, 'offline' ], |
17e7d7f0 |
309 | ]); |
310 | |
311 | $schema->populate('OneKey', [ |
312 | [ qw/id artist cd/ ], |
313 | [ 1, 1, 1 ], |
314 | [ 2, 1, 2 ], |
315 | [ 3, 2, 2 ], |
316 | ]); |
317 | |
318 | $schema->populate('SelfRef', [ |
319 | [ qw/id name/ ], |
320 | [ 1, 'First' ], |
321 | [ 2, 'Second' ], |
322 | ]); |
323 | |
324 | $schema->populate('SelfRefAlias', [ |
325 | [ qw/self_ref alias/ ], |
326 | [ 1, 2 ] |
327 | ]); |
328 | |
329 | $schema->populate('ArtistUndirectedMap', [ |
330 | [ qw/id1 id2/ ], |
331 | [ 1, 2 ] |
332 | ]); |
333 | |
334 | $schema->populate('Producer', [ |
335 | [ qw/producerid name/ ], |
336 | [ 1, 'Matt S Trout' ], |
337 | [ 2, 'Bob The Builder' ], |
338 | [ 3, 'Fred The Phenotype' ], |
339 | ]); |
340 | |
341 | $schema->populate('CD_to_Producer', [ |
342 | [ qw/cd producer/ ], |
343 | [ 1, 1 ], |
344 | [ 1, 2 ], |
345 | [ 1, 3 ], |
346 | ]); |
8871d4ad |
347 | |
348 | $schema->populate('TreeLike', [ |
61177e44 |
349 | [ qw/id parent name/ ], |
972015a7 |
350 | [ 1, undef, 'root' ], |
8871d4ad |
351 | [ 2, 1, 'foo' ], |
352 | [ 3, 2, 'bar' ], |
353 | [ 6, 2, 'blop' ], |
354 | [ 4, 3, 'baz' ], |
355 | [ 5, 4, 'quux' ], |
356 | [ 7, 3, 'fong' ], |
357 | ]); |
4b8dcc58 |
358 | |
17e7d7f0 |
359 | $schema->populate('Track', [ |
360 | [ qw/trackid cd position title/ ], |
361 | [ 4, 2, 1, "Stung with Success"], |
362 | [ 5, 2, 2, "Stripy"], |
363 | [ 6, 2, 3, "Sticky Honey"], |
364 | [ 7, 3, 1, "Yowlin"], |
365 | [ 8, 3, 2, "Howlin"], |
366 | [ 9, 3, 3, "Fowlin"], |
367 | [ 10, 4, 1, "Boring Name"], |
368 | [ 11, 4, 2, "Boring Song"], |
369 | [ 12, 4, 3, "No More Ideas"], |
370 | [ 13, 5, 1, "Sad"], |
371 | [ 14, 5, 2, "Under The Weather"], |
372 | [ 15, 5, 3, "Suicidal"], |
373 | [ 16, 1, 1, "The Bees Knees"], |
374 | [ 17, 1, 2, "Apiary"], |
375 | [ 18, 1, 3, "Beehind You"], |
376 | ]); |
4b8dcc58 |
377 | |
ae515736 |
378 | $schema->populate('Event', [ |
ff8a6e3b |
379 | [ qw/id starts_at created_on varchar_date varchar_datetime skip_inflation/ ], |
380 | [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05', '2006-07-23', '2006-05-22 19:05:07', '2006-04-21 18:04:06'], |
ae515736 |
381 | ]); |
382 | |
17e7d7f0 |
383 | $schema->populate('Link', [ |
54e0bd06 |
384 | [ qw/id url title/ ], |
385 | [ 1, '', 'aaa' ] |
17e7d7f0 |
386 | ]); |
e673f011 |
387 | |
17e7d7f0 |
388 | $schema->populate('Bookmark', [ |
389 | [ qw/id link/ ], |
390 | [ 1, 1 ] |
391 | ]); |
78060df8 |
392 | |
393 | $schema->populate('Collection', [ |
394 | [ qw/collectionid name/ ], |
395 | [ 1, "Tools" ], |
396 | [ 2, "Body Parts" ], |
397 | ]); |
89cf6a70 |
398 | |
78060df8 |
399 | $schema->populate('TypedObject', [ |
400 | [ qw/objectid type value/ ], |
401 | [ 1, "pointy", "Awl" ], |
402 | [ 2, "round", "Bearing" ], |
403 | [ 3, "pointy", "Knife" ], |
404 | [ 4, "pointy", "Tooth" ], |
405 | [ 5, "round", "Head" ], |
406 | ]); |
89cf6a70 |
407 | $schema->populate('CollectionObject', [ |
408 | [ qw/collection object/ ], |
409 | [ 1, 1 ], |
410 | [ 1, 2 ], |
411 | [ 1, 3 ], |
412 | [ 2, 4 ], |
413 | [ 2, 5 ], |
414 | ]); |
78060df8 |
415 | |
416 | $schema->populate('Owners', [ |
bed3a173 |
417 | [ qw/id name/ ], |
78060df8 |
418 | [ 1, "Newton" ], |
419 | [ 2, "Waltham" ], |
420 | ]); |
421 | |
422 | $schema->populate('BooksInLibrary', [ |
cda5e082 |
423 | [ qw/id owner title source price/ ], |
424 | [ 1, 1, "Programming Perl", "Library", 23 ], |
425 | [ 2, 1, "Dynamical Systems", "Library", 37 ], |
426 | [ 3, 2, "Best Recipe Cookbook", "Library", 65 ], |
78060df8 |
427 | ]); |
1c339d71 |
428 | } |
4b8dcc58 |
429 | |
510ca912 |
430 | 1; |