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 $schema = DBICTest::Schema->compose_connection('DBICTest' => $dsn, $dbuser, $dbpass);
59 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
60 if ( !$args{no_deploy} ) {
61 __PACKAGE__->deploy_schema( $schema );
62 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
69 DBICTest->deploy_schema( $schema );
71 This method does one of two things to the schema. It can either call
72 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
73 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
74 file and execute the SQL within. Either way you end up with a fresh set
75 of tables for testing.
83 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
84 return $schema->deploy();
86 open IN, "t/lib/sqlite.sql";
88 { local $/ = undef; $sql = <IN>; }
90 $schema->storage->dbh->do($_) for split(/;\n/, $sql);
94 =head2 populate_schema
96 DBICTest->populate_schema( $schema );
98 After you deploy your schema you can use this method to populate
99 the tables with test data.
103 sub populate_schema {
107 $schema->populate('Artist', [
108 [ qw/artistid name/ ],
109 [ 1, 'Caterwauler McCrae' ],
110 [ 2, 'Random Boy Band' ],
111 [ 3, 'We Are Goth' ],
114 $schema->populate('CD', [
115 [ qw/cdid artist title year/ ],
116 [ 1, 1, "Spoonful of bees", 1999 ],
117 [ 2, 1, "Forkful of bees", 2001 ],
118 [ 3, 1, "Caterwaulin' Blues", 1997 ],
119 [ 4, 2, "Generic Manufactured Singles", 2001 ],
120 [ 5, 3, "Come Be Depressed With Us", 1998 ],
123 $schema->populate('LinerNotes', [
124 [ qw/liner_id notes/ ],
125 [ 2, "Buy Whiskey!" ],
127 [ 5, "Kill Yourself!" ],
130 $schema->populate('Tag', [
131 [ qw/tagid cd tag/ ],
143 $schema->populate('TwoKeys', [
150 $schema->populate('FourKeys', [
151 [ qw/foo bar hello goodbye sensors/ ],
152 [ 1, 2, 3, 4, 'online' ],
153 [ 5, 4, 3, 6, 'offline' ],
156 $schema->populate('OneKey', [
157 [ qw/id artist cd/ ],
163 $schema->populate('SelfRef', [
169 $schema->populate('SelfRefAlias', [
170 [ qw/self_ref alias/ ],
174 $schema->populate('ArtistUndirectedMap', [
179 $schema->populate('Producer', [
180 [ qw/producerid name/ ],
181 [ 1, 'Matt S Trout' ],
182 [ 2, 'Bob The Builder' ],
183 [ 3, 'Fred The Phenotype' ],
186 $schema->populate('CD_to_Producer', [
193 $schema->populate('TreeLike', [
194 [ qw/id parent name/ ],
203 $schema->populate('Track', [
204 [ qw/trackid cd position title/ ],
205 [ 4, 2, 1, "Stung with Success"],
206 [ 5, 2, 2, "Stripy"],
207 [ 6, 2, 3, "Sticky Honey"],
208 [ 7, 3, 1, "Yowlin"],
209 [ 8, 3, 2, "Howlin"],
210 [ 9, 3, 3, "Fowlin"],
211 [ 10, 4, 1, "Boring Name"],
212 [ 11, 4, 2, "Boring Song"],
213 [ 12, 4, 3, "No More Ideas"],
215 [ 14, 5, 2, "Under The Weather"],
216 [ 15, 5, 3, "Suicidal"],
217 [ 16, 1, 1, "The Bees Knees"],
218 [ 17, 1, 2, "Apiary"],
219 [ 18, 1, 3, "Beehind You"],
222 $schema->populate('Event', [
223 [ qw/id starts_at created_on/ ],
224 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
227 $schema->populate('Link', [
232 $schema->populate('Bookmark', [