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"} || '';
60 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
62 if ($args{compose_connection}) {
63 $schema = DBICTest::Schema->compose_connection(
64 'DBICTest', @connect_info
67 $schema = DBICTest::Schema->compose_namespace('DBICTest')
68 ->connect(@connect_info);
70 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
71 if ( !$args{no_deploy} ) {
72 __PACKAGE__->deploy_schema( $schema );
73 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
80 DBICTest->deploy_schema( $schema );
82 This method does one of two things to the schema. It can either call
83 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
84 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
85 file and execute the SQL within. Either way you end up with a fresh set
86 of tables for testing.
94 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
95 return $schema->deploy();
97 open IN, "t/lib/sqlite.sql";
99 { local $/ = undef; $sql = <IN>; }
101 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
105 =head2 populate_schema
107 DBICTest->populate_schema( $schema );
109 After you deploy your schema you can use this method to populate
110 the tables with test data.
114 sub populate_schema {
118 $schema->populate('Artist', [
119 [ qw/artistid name/ ],
120 [ 1, 'Caterwauler McCrae' ],
121 [ 2, 'Random Boy Band' ],
122 [ 3, 'We Are Goth' ],
125 $schema->populate('CD', [
126 [ qw/cdid artist title year/ ],
127 [ 1, 1, "Spoonful of bees", 1999 ],
128 [ 2, 1, "Forkful of bees", 2001 ],
129 [ 3, 1, "Caterwaulin' Blues", 1997 ],
130 [ 4, 2, "Generic Manufactured Singles", 2001 ],
131 [ 5, 3, "Come Be Depressed With Us", 1998 ],
134 $schema->populate('LinerNotes', [
135 [ qw/liner_id notes/ ],
136 [ 2, "Buy Whiskey!" ],
138 [ 5, "Kill Yourself!" ],
141 $schema->populate('Tag', [
142 [ qw/tagid cd tag/ ],
154 $schema->populate('TwoKeys', [
161 $schema->populate('FourKeys', [
162 [ qw/foo bar hello goodbye sensors/ ],
163 [ 1, 2, 3, 4, 'online' ],
164 [ 5, 4, 3, 6, 'offline' ],
167 $schema->populate('OneKey', [
168 [ qw/id artist cd/ ],
174 $schema->populate('SelfRef', [
180 $schema->populate('SelfRefAlias', [
181 [ qw/self_ref alias/ ],
185 $schema->populate('ArtistUndirectedMap', [
190 $schema->populate('Producer', [
191 [ qw/producerid name/ ],
192 [ 1, 'Matt S Trout' ],
193 [ 2, 'Bob The Builder' ],
194 [ 3, 'Fred The Phenotype' ],
197 $schema->populate('CD_to_Producer', [
204 $schema->populate('TreeLike', [
205 [ qw/id parent name/ ],
214 $schema->populate('Track', [
215 [ qw/trackid cd position title/ ],
216 [ 4, 2, 1, "Stung with Success"],
217 [ 5, 2, 2, "Stripy"],
218 [ 6, 2, 3, "Sticky Honey"],
219 [ 7, 3, 1, "Yowlin"],
220 [ 8, 3, 2, "Howlin"],
221 [ 9, 3, 3, "Fowlin"],
222 [ 10, 4, 1, "Boring Name"],
223 [ 11, 4, 2, "Boring Song"],
224 [ 12, 4, 3, "No More Ideas"],
226 [ 14, 5, 2, "Under The Weather"],
227 [ 15, 5, 3, "Suicidal"],
228 [ 16, 1, 1, "The Bees Knees"],
229 [ 17, 1, 2, "Apiary"],
230 [ 18, 1, 3, "Beehind You"],
233 $schema->populate('Event', [
234 [ qw/id starts_at created_on/ ],
235 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
238 $schema->populate('Link', [
239 [ qw/id url title/ ],
243 $schema->populate('Bookmark', [
248 $schema->populate('Collection', [
249 [ qw/collectionid name/ ],
254 $schema->populate('CollectionObject', [
255 [ qw/collection object/ ],
263 $schema->populate('TypedObject', [
264 [ qw/objectid type value/ ],
265 [ 1, "pointy", "Awl" ],
266 [ 2, "round", "Bearing" ],
267 [ 3, "pointy", "Knife" ],
268 [ 4, "pointy", "Tooth" ],
269 [ 5, "round", "Head" ],
272 $schema->populate('Owners', [
273 [ qw/ownerid name/ ],
278 $schema->populate('BooksInLibrary', [
279 [ qw/id owner title source/ ],
280 [ 1, 1, "Programming Perl", "Library" ],
281 [ 2, 1, "Dynamical Systems", "Library" ],
282 [ 3, 2, "Best Recipe Cookbook", "Library" ],