1 package # hide from PAUSE
10 DBICTest - Library to be used by DBIx::Class test scripts.
18 my $schema = DBICTest->init_schema();
22 This module provides the basic utilities to write tests against
29 my $schema = DBICTest->init_schema(
34 This method removes the test SQLite database in t/var/DBIxClass.db
35 and then creates a new, empty database.
37 This method will call deploy_schema() by default, unless the
38 no_deploy flag is set.
40 Also, by default, this method will call populate_schema() by
41 default, unless the no_deploy or no_populate flags are set.
47 my $db_file = "t/var/DBIxClass.db";
49 unlink($db_file) if -e $db_file;
50 unlink($db_file . "-journal") if -e $db_file . "-journal";
51 mkdir("t/var") unless -d "t/var";
53 my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
54 my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
55 my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
57 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
68 if ($args{compose_connection}) {
69 $schema = DBICTest::Schema->compose_connection(
70 'DBICTest', $self->_database
73 $schema = DBICTest::Schema->compose_namespace('DBICTest');
75 if ( !$args{no_connect} ) {
76 $schema = $schema->connect($self->_database);
77 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
79 if ( !$args{no_deploy} ) {
80 __PACKAGE__->deploy_schema( $schema );
81 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
88 DBICTest->deploy_schema( $schema );
90 This method does one of two things to the schema. It can either call
91 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
92 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
93 file and execute the SQL within. Either way you end up with a fresh set
94 of tables for testing.
102 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
103 return $schema->deploy();
105 open IN, "t/lib/sqlite.sql";
107 { local $/ = undef; $sql = <IN>; }
109 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
113 =head2 populate_schema
115 DBICTest->populate_schema( $schema );
117 After you deploy your schema you can use this method to populate
118 the tables with test data.
122 sub populate_schema {
126 $schema->populate('Artist', [
127 [ qw/artistid name/ ],
128 [ 1, 'Caterwauler McCrae' ],
129 [ 2, 'Random Boy Band' ],
130 [ 3, 'We Are Goth' ],
133 $schema->populate('CD', [
134 [ qw/cdid artist title year/ ],
135 [ 1, 1, "Spoonful of bees", 1999 ],
136 [ 2, 1, "Forkful of bees", 2001 ],
137 [ 3, 1, "Caterwaulin' Blues", 1997 ],
138 [ 4, 2, "Generic Manufactured Singles", 2001 ],
139 [ 5, 3, "Come Be Depressed With Us", 1998 ],
142 $schema->populate('LinerNotes', [
143 [ qw/liner_id notes/ ],
144 [ 2, "Buy Whiskey!" ],
146 [ 5, "Kill Yourself!" ],
149 $schema->populate('Tag', [
150 [ qw/tagid cd tag/ ],
162 $schema->populate('TwoKeys', [
169 $schema->populate('FourKeys', [
170 [ qw/foo bar hello goodbye sensors/ ],
171 [ 1, 2, 3, 4, 'online' ],
172 [ 5, 4, 3, 6, 'offline' ],
175 $schema->populate('OneKey', [
176 [ qw/id artist cd/ ],
182 $schema->populate('SelfRef', [
188 $schema->populate('SelfRefAlias', [
189 [ qw/self_ref alias/ ],
193 $schema->populate('ArtistUndirectedMap', [
198 $schema->populate('Producer', [
199 [ qw/producerid name/ ],
200 [ 1, 'Matt S Trout' ],
201 [ 2, 'Bob The Builder' ],
202 [ 3, 'Fred The Phenotype' ],
205 $schema->populate('CD_to_Producer', [
212 $schema->populate('TreeLike', [
213 [ qw/id parent name/ ],
222 $schema->populate('Track', [
223 [ qw/trackid cd position title/ ],
224 [ 4, 2, 1, "Stung with Success"],
225 [ 5, 2, 2, "Stripy"],
226 [ 6, 2, 3, "Sticky Honey"],
227 [ 7, 3, 1, "Yowlin"],
228 [ 8, 3, 2, "Howlin"],
229 [ 9, 3, 3, "Fowlin"],
230 [ 10, 4, 1, "Boring Name"],
231 [ 11, 4, 2, "Boring Song"],
232 [ 12, 4, 3, "No More Ideas"],
234 [ 14, 5, 2, "Under The Weather"],
235 [ 15, 5, 3, "Suicidal"],
236 [ 16, 1, 1, "The Bees Knees"],
237 [ 17, 1, 2, "Apiary"],
238 [ 18, 1, 3, "Beehind You"],
241 $schema->populate('Event', [
242 [ qw/id starts_at created_on/ ],
243 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
246 $schema->populate('Link', [
247 [ qw/id url title/ ],
251 $schema->populate('Bookmark', [
256 $schema->populate('Collection', [
257 [ qw/collectionid name/ ],
262 $schema->populate('CollectionObject', [
263 [ qw/collection object/ ],
271 $schema->populate('TypedObject', [
272 [ qw/objectid type value/ ],
273 [ 1, "pointy", "Awl" ],
274 [ 2, "round", "Bearing" ],
275 [ 3, "pointy", "Knife" ],
276 [ 4, "pointy", "Tooth" ],
277 [ 5, "round", "Head" ],
280 $schema->populate('Owners', [
281 [ qw/ownerid name/ ],
286 $schema->populate('BooksInLibrary', [
287 [ qw/id owner title source/ ],
288 [ 1, 1, "Programming Perl", "Library" ],
289 [ 2, 1, "Dynamical Systems", "Library" ],
290 [ 3, 2, "Best Recipe Cookbook", "Library" ],