1 package # hide from PAUSE
12 DBICTest - Library to be used by DBIx::Class test scripts.
20 my $schema = DBICTest->init_schema();
24 This module provides the basic utilities to write tests against
31 my $schema = DBICTest->init_schema(
34 storage_type=>'::DBI::Replicated',
36 balancer_type=>'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
40 This method removes the test SQLite database in t/var/DBIxClass.db
41 and then creates a new, empty database.
43 This method will call deploy_schema() by default, unless the
44 no_deploy flag is set.
46 Also, by default, this method will call populate_schema() by
47 default, unless the no_deploy or no_populate flags are set.
52 return $ENV{"DBICTEST_DSN"} ? 1:0;
55 sub _sqlite_dbfilename {
56 return "t/var/DBIxClass.db";
62 return $self->_sqlite_dbfilename if $args{sqlite_use_file} or $ENV{"DBICTEST_SQLITE_USE_FILE"};
70 if ($ENV{DBICTEST_DSN}) {
72 (map { $ENV{"DBICTEST_${_}"} || '' } qw/DSN DBUSER DBPASS/),
73 { AutoCommit => 1, %args },
76 my $db_file = $self->_sqlite_dbname(%args);
78 for ($db_file, "${db_file}-journal") {
81 "Unable to unlink existing test database file $_ ($!), creation of fresh database / further tests may fail!"
85 mkdir("t/var") unless -d "t/var";
87 return ("dbi:SQLite:${db_file}", '', '', {
90 # this is executed on every connect, and thus installs a disconnect/DESTROY
91 # guard for every new $dbh
92 on_connect_do => sub {
94 my $dbh = $storage->_get_dbh;
97 $dbh->do ('PRAGMA synchronous = OFF');
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)
102 if (my $guard_cb = __mk_disconnect_guard($db_file)) {
103 $dbh->{Callbacks} = {
104 connect => sub { $guard_cb->('connect') },
105 disconnect => sub { $guard_cb->('disconnect') },
106 DESTROY => sub { $guard_cb->('DESTROY') },
114 sub __mk_disconnect_guard {
115 return if DBIx::Class::_ENV_::PEEPEENESS(); # leaks handles, delaying DESTROY, can't work right
118 return unless -f $db_file;
120 my $orig_inode = (stat($db_file))[1]
123 my $clan_connect_caller = '*UNKNOWN*';
125 while ( my ($pack, $file, $line) = caller(++$i) ) {
126 next if $file eq __FILE__;
127 next if $pack =~ /^DBIx::Class|^Try::Tiny/;
128 $clan_connect_caller = "$file line $line";
135 return if $failed_once;
138 if ($event eq 'connect') {
139 # this is necessary in case we are disconnected and connected again, all within the same $dbh object
143 elsif ($event eq 'disconnect') {
146 elsif ($event eq 'DESTROY' and ! $connected ) {
152 $fail_reason = 'is missing';
155 my $cur_inode = (stat($db_file))[1];
157 if ($orig_inode != $cur_inode) {
158 # pack/unpack to match the unsigned longs returned by `stat`
159 $fail_reason = sprintf 'was recreated (initially inode %s, now %s)', (
160 map { unpack ('L', pack ('l', $_) ) } ($orig_inode, $cur_inode )
168 require Test::Builder;
169 my $t = Test::Builder->new;
170 local $Test::Builder::Level = $Test::Builder::Level + 3;
172 "$db_file originally created at $clan_connect_caller $fail_reason before $event "
173 . 'of DBI handle - a strong indicator that the database file was tampered with while '
174 . 'still being open. This action would fail massively if running under Win32, hence '
175 . 'we make sure it fails on any OS :)'
179 return; # this empty return is a DBI requirement
189 if ($args{compose_connection}) {
190 $schema = DBICTest::Schema->compose_connection(
191 'DBICTest', $self->_database(%args)
194 $schema = DBICTest::Schema->compose_namespace('DBICTest');
197 if( $args{storage_type}) {
198 $schema->storage_type($args{storage_type});
201 if ( !$args{no_connect} ) {
202 $schema = $schema->connect($self->_database(%args));
205 if ( !$args{no_deploy} ) {
206 __PACKAGE__->deploy_schema( $schema, $args{deploy_args} );
207 __PACKAGE__->populate_schema( $schema )
208 if( !$args{no_populate} );
215 DBICTest->deploy_schema( $schema );
217 This method does one of two things to the schema. It can either call
218 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
219 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
220 file and execute the SQL within. Either way you end up with a fresh set
221 of tables for testing.
228 my $args = shift || {};
230 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
231 $schema->deploy($args);
233 open IN, "t/lib/sqlite.sql";
235 { local $/ = undef; $sql = <IN>; }
237 for my $chunk ( split (/;\s*\n+/, $sql) ) {
238 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
239 $schema->storage->dbh_do(sub { $_[1]->do($chunk) }) or print "Error on SQL: $chunk\n";
246 =head2 populate_schema
248 DBICTest->populate_schema( $schema );
250 After you deploy your schema you can use this method to populate
251 the tables with test data.
255 sub populate_schema {
259 $schema->populate('Genre', [
264 $schema->populate('Artist', [
265 [ qw/artistid name/ ],
266 [ 1, 'Caterwauler McCrae' ],
267 [ 2, 'Random Boy Band' ],
268 [ 3, 'We Are Goth' ],
271 $schema->populate('CD', [
272 [ qw/cdid artist title year genreid/ ],
273 [ 1, 1, "Spoonful of bees", 1999, 1 ],
274 [ 2, 1, "Forkful of bees", 2001 ],
275 [ 3, 1, "Caterwaulin' Blues", 1997 ],
276 [ 4, 2, "Generic Manufactured Singles", 2001 ],
277 [ 5, 3, "Come Be Depressed With Us", 1998 ],
280 $schema->populate('LinerNotes', [
281 [ qw/liner_id notes/ ],
282 [ 2, "Buy Whiskey!" ],
284 [ 5, "Kill Yourself!" ],
287 $schema->populate('Tag', [
288 [ qw/tagid cd tag/ ],
300 $schema->populate('TwoKeys', [
307 $schema->populate('FourKeys', [
308 [ qw/foo bar hello goodbye sensors/ ],
309 [ 1, 2, 3, 4, 'online' ],
310 [ 5, 4, 3, 6, 'offline' ],
313 $schema->populate('OneKey', [
314 [ qw/id artist cd/ ],
320 $schema->populate('SelfRef', [
326 $schema->populate('SelfRefAlias', [
327 [ qw/self_ref alias/ ],
331 $schema->populate('ArtistUndirectedMap', [
336 $schema->populate('Producer', [
337 [ qw/producerid name/ ],
338 [ 1, 'Matt S Trout' ],
339 [ 2, 'Bob The Builder' ],
340 [ 3, 'Fred The Phenotype' ],
343 $schema->populate('CD_to_Producer', [
350 $schema->populate('TreeLike', [
351 [ qw/id parent name/ ],
352 [ 1, undef, 'root' ],
361 $schema->populate('Track', [
362 [ qw/trackid cd position title/ ],
363 [ 4, 2, 1, "Stung with Success"],
364 [ 5, 2, 2, "Stripy"],
365 [ 6, 2, 3, "Sticky Honey"],
366 [ 7, 3, 1, "Yowlin"],
367 [ 8, 3, 2, "Howlin"],
368 [ 9, 3, 3, "Fowlin"],
369 [ 10, 4, 1, "Boring Name"],
370 [ 11, 4, 2, "Boring Song"],
371 [ 12, 4, 3, "No More Ideas"],
373 [ 14, 5, 2, "Under The Weather"],
374 [ 15, 5, 3, "Suicidal"],
375 [ 16, 1, 1, "The Bees Knees"],
376 [ 17, 1, 2, "Apiary"],
377 [ 18, 1, 3, "Beehind You"],
380 $schema->populate('Event', [
381 [ qw/id starts_at created_on varchar_date varchar_datetime skip_inflation/ ],
382 [ 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'],
385 $schema->populate('Link', [
386 [ qw/id url title/ ],
390 $schema->populate('Bookmark', [
395 $schema->populate('Collection', [
396 [ qw/collectionid name/ ],
401 $schema->populate('TypedObject', [
402 [ qw/objectid type value/ ],
403 [ 1, "pointy", "Awl" ],
404 [ 2, "round", "Bearing" ],
405 [ 3, "pointy", "Knife" ],
406 [ 4, "pointy", "Tooth" ],
407 [ 5, "round", "Head" ],
409 $schema->populate('CollectionObject', [
410 [ qw/collection object/ ],
418 $schema->populate('Owners', [
424 $schema->populate('BooksInLibrary', [
425 [ qw/id owner title source price/ ],
426 [ 1, 1, "Programming Perl", "Library", 23 ],
427 [ 2, 1, "Dynamical Systems", "Library", 37 ],
428 [ 3, 2, "Best Recipe Cookbook", "Library", 65 ],