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