1 package # hide from PAUSE
6 use DBICTest::AuthorCheck;
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!\n"
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 (-e $db_file and my $orig_inode = (stat($db_file))[1] ) {
107 return if $failed_once;
110 if ($event eq 'connect') {
111 # this is necessary in case we are disconnected and connected again, all within the same $dbh object
115 elsif ($event eq 'disconnect') {
118 elsif ($event eq 'DESTROY' and ! $connected ) {
124 $fail_reason = 'is missing';
127 my $cur_inode = (stat($db_file))[1];
129 $fail_reason ||= sprintf 'was recreated (inode %s vs %s)', ($orig_inode, $cur_inode)
130 if $orig_inode != $cur_inode;
136 require Test::Builder;
137 my $t = Test::Builder->new;
138 local $Test::Builder::Level = $Test::Builder::Level + 1;
141 "$db_file $fail_reason before $event of DBI handle - a strong indicator that "
142 . 'the SQLite file was tampered with while still being open. This action would '
143 . 'fail massively if running under Win32, hence DBICTest makes sure it fails '
148 return; # this empty return is a DBI requirement
150 $dbh->{Callbacks} = {
151 connect => sub { $cb->('connect') },
152 disconnect => sub { $cb->('disconnect') },
153 DESTROY => sub { $cb->('DESTROY') },
167 if ($args{compose_connection}) {
168 $schema = DBICTest::Schema->compose_connection(
169 'DBICTest', $self->_database(%args)
172 $schema = DBICTest::Schema->compose_namespace('DBICTest');
175 if( $args{storage_type}) {
176 $schema->storage_type($args{storage_type});
179 if ( !$args{no_connect} ) {
180 $schema = $schema->connect($self->_database(%args));
183 if ( !$args{no_deploy} ) {
184 __PACKAGE__->deploy_schema( $schema, $args{deploy_args} );
185 __PACKAGE__->populate_schema( $schema )
186 if( !$args{no_populate} );
193 DBICTest->deploy_schema( $schema );
195 This method does one of two things to the schema. It can either call
196 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
197 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
198 file and execute the SQL within. Either way you end up with a fresh set
199 of tables for testing.
206 my $args = shift || {};
208 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
209 $schema->deploy($args);
211 open IN, "t/lib/sqlite.sql";
213 { local $/ = undef; $sql = <IN>; }
215 for my $chunk ( split (/;\s*\n+/, $sql) ) {
216 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
217 $schema->storage->dbh_do(sub { $_[1]->do($chunk) }) or print "Error on SQL: $chunk\n";
224 =head2 populate_schema
226 DBICTest->populate_schema( $schema );
228 After you deploy your schema you can use this method to populate
229 the tables with test data.
233 sub populate_schema {
237 $schema->populate('Genre', [
242 $schema->populate('Artist', [
243 [ qw/artistid name/ ],
244 [ 1, 'Caterwauler McCrae' ],
245 [ 2, 'Random Boy Band' ],
246 [ 3, 'We Are Goth' ],
249 $schema->populate('CD', [
250 [ qw/cdid artist title year genreid/ ],
251 [ 1, 1, "Spoonful of bees", 1999, 1 ],
252 [ 2, 1, "Forkful of bees", 2001 ],
253 [ 3, 1, "Caterwaulin' Blues", 1997 ],
254 [ 4, 2, "Generic Manufactured Singles", 2001 ],
255 [ 5, 3, "Come Be Depressed With Us", 1998 ],
258 $schema->populate('LinerNotes', [
259 [ qw/liner_id notes/ ],
260 [ 2, "Buy Whiskey!" ],
262 [ 5, "Kill Yourself!" ],
265 $schema->populate('Tag', [
266 [ qw/tagid cd tag/ ],
278 $schema->populate('TwoKeys', [
285 $schema->populate('FourKeys', [
286 [ qw/foo bar hello goodbye sensors/ ],
287 [ 1, 2, 3, 4, 'online' ],
288 [ 5, 4, 3, 6, 'offline' ],
291 $schema->populate('OneKey', [
292 [ qw/id artist cd/ ],
298 $schema->populate('SelfRef', [
304 $schema->populate('SelfRefAlias', [
305 [ qw/self_ref alias/ ],
309 $schema->populate('ArtistUndirectedMap', [
314 $schema->populate('Producer', [
315 [ qw/producerid name/ ],
316 [ 1, 'Matt S Trout' ],
317 [ 2, 'Bob The Builder' ],
318 [ 3, 'Fred The Phenotype' ],
321 $schema->populate('CD_to_Producer', [
328 $schema->populate('TreeLike', [
329 [ qw/id parent name/ ],
330 [ 1, undef, 'root' ],
339 $schema->populate('Track', [
340 [ qw/trackid cd position title/ ],
341 [ 4, 2, 1, "Stung with Success"],
342 [ 5, 2, 2, "Stripy"],
343 [ 6, 2, 3, "Sticky Honey"],
344 [ 7, 3, 1, "Yowlin"],
345 [ 8, 3, 2, "Howlin"],
346 [ 9, 3, 3, "Fowlin"],
347 [ 10, 4, 1, "Boring Name"],
348 [ 11, 4, 2, "Boring Song"],
349 [ 12, 4, 3, "No More Ideas"],
351 [ 14, 5, 2, "Under The Weather"],
352 [ 15, 5, 3, "Suicidal"],
353 [ 16, 1, 1, "The Bees Knees"],
354 [ 17, 1, 2, "Apiary"],
355 [ 18, 1, 3, "Beehind You"],
358 $schema->populate('Event', [
359 [ qw/id starts_at created_on varchar_date varchar_datetime skip_inflation/ ],
360 [ 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'],
363 $schema->populate('Link', [
364 [ qw/id url title/ ],
368 $schema->populate('Bookmark', [
373 $schema->populate('Collection', [
374 [ qw/collectionid name/ ],
379 $schema->populate('TypedObject', [
380 [ qw/objectid type value/ ],
381 [ 1, "pointy", "Awl" ],
382 [ 2, "round", "Bearing" ],
383 [ 3, "pointy", "Knife" ],
384 [ 4, "pointy", "Tooth" ],
385 [ 5, "round", "Head" ],
387 $schema->populate('CollectionObject', [
388 [ qw/collection object/ ],
396 $schema->populate('Owners', [
402 $schema->populate('BooksInLibrary', [
403 [ qw/id owner title source price/ ],
404 [ 1, 1, "Programming Perl", "Library", 23 ],
405 [ 2, 1, "Dynamical Systems", "Library", 37 ],
406 [ 3, 2, "Best Recipe Cookbook", "Library", 65 ],