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.
48 my $db_file = "t/var/DBIxClass.db";
50 unlink($db_file) if -e $db_file;
51 unlink($db_file . "-journal") if -e $db_file . "-journal";
52 mkdir("t/var") unless -d "t/var";
54 my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
55 my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
56 my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
58 my $compose_method = ($args{compose_connection}
59 ? 'compose_connection'
60 : 'compose_namespace');
62 my $schema = DBICTest::Schema->$compose_method('DBICTest')
63 ->connect($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
64 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
65 if ( !$args{no_deploy} ) {
66 __PACKAGE__->deploy_schema( $schema );
67 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
74 DBICTest->deploy_schema( $schema );
76 This method does one of two things to the schema. It can either call
77 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
78 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
79 file and execute the SQL within. Either way you end up with a fresh set
80 of tables for testing.
88 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
89 return $schema->deploy();
91 open IN, "t/lib/sqlite.sql";
93 { local $/ = undef; $sql = <IN>; }
95 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
99 =head2 populate_schema
101 DBICTest->populate_schema( $schema );
103 After you deploy your schema you can use this method to populate
104 the tables with test data.
108 sub populate_schema {
112 $schema->populate('Artist', [
113 [ qw/artistid name/ ],
114 [ 1, 'Caterwauler McCrae' ],
115 [ 2, 'Random Boy Band' ],
116 [ 3, 'We Are Goth' ],
119 $schema->populate('CD', [
120 [ qw/cdid artist title year/ ],
121 [ 1, 1, "Spoonful of bees", 1999 ],
122 [ 2, 1, "Forkful of bees", 2001 ],
123 [ 3, 1, "Caterwaulin' Blues", 1997 ],
124 [ 4, 2, "Generic Manufactured Singles", 2001 ],
125 [ 5, 3, "Come Be Depressed With Us", 1998 ],
128 $schema->populate('LinerNotes', [
129 [ qw/liner_id notes/ ],
130 [ 2, "Buy Whiskey!" ],
132 [ 5, "Kill Yourself!" ],
135 $schema->populate('Tag', [
136 [ qw/tagid cd tag/ ],
148 $schema->populate('TwoKeys', [
155 $schema->populate('FourKeys', [
156 [ qw/foo bar hello goodbye sensors/ ],
157 [ 1, 2, 3, 4, 'online' ],
158 [ 5, 4, 3, 6, 'offline' ],
161 $schema->populate('OneKey', [
162 [ qw/id artist cd/ ],
168 $schema->populate('SelfRef', [
174 $schema->populate('SelfRefAlias', [
175 [ qw/self_ref alias/ ],
179 $schema->populate('ArtistUndirectedMap', [
184 $schema->populate('Producer', [
185 [ qw/producerid name/ ],
186 [ 1, 'Matt S Trout' ],
187 [ 2, 'Bob The Builder' ],
188 [ 3, 'Fred The Phenotype' ],
191 $schema->populate('CD_to_Producer', [
198 $schema->populate('TreeLike', [
199 [ qw/id parent name/ ],
208 $schema->populate('Track', [
209 [ qw/trackid cd position title/ ],
210 [ 4, 2, 1, "Stung with Success"],
211 [ 5, 2, 2, "Stripy"],
212 [ 6, 2, 3, "Sticky Honey"],
213 [ 7, 3, 1, "Yowlin"],
214 [ 8, 3, 2, "Howlin"],
215 [ 9, 3, 3, "Fowlin"],
216 [ 10, 4, 1, "Boring Name"],
217 [ 11, 4, 2, "Boring Song"],
218 [ 12, 4, 3, "No More Ideas"],
220 [ 14, 5, 2, "Under The Weather"],
221 [ 15, 5, 3, "Suicidal"],
222 [ 16, 1, 1, "The Bees Knees"],
223 [ 17, 1, 2, "Apiary"],
224 [ 18, 1, 3, "Beehind You"],
227 $schema->populate('Event', [
228 [ qw/id starts_at created_on/ ],
229 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
232 $schema->populate('Link', [
233 [ qw/id url title/ ],
237 $schema->populate('Bookmark', [
242 $schema->populate('Collection', [
243 [ qw/collectionid name/ ],
248 $schema->populate('CollectionObject', [
249 [ qw/collection object/ ],
257 $schema->populate('TypedObject', [
258 [ qw/objectid type value/ ],
259 [ 1, "pointy", "Awl" ],
260 [ 2, "round", "Bearing" ],
261 [ 3, "pointy", "Knife" ],
262 [ 4, "pointy", "Tooth" ],
263 [ 5, "round", "Head" ],
266 $schema->populate('Owners', [
267 [ qw/ownerid name/ ],
272 $schema->populate('BooksInLibrary', [
273 [ qw/id owner title source/ ],
274 [ 1, 1, "Programming Perl", "Library" ],
275 [ 2, 1, "Dynamical Systems", "Library" ],
276 [ 3, 2, "Best Recipe Cookbook", "Library" ],