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 $schema = DBICTest::Schema->compose_namespace('DBICTest');
69 if ( !$args{no_connect} ) {
70 $schema = $schema->connect($self->_database);
71 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
73 if ( !$args{no_deploy} ) {
74 __PACKAGE__->deploy_schema( $schema );
75 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
82 DBICTest->deploy_schema( $schema );
84 This method does one of two things to the schema. It can either call
85 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
86 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
87 file and execute the SQL within. Either way you end up with a fresh set
88 of tables for testing.
96 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
97 return $schema->deploy();
99 open IN, "t/lib/sqlite.sql";
101 { local $/ = undef; $sql = <IN>; }
103 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
107 =head2 populate_schema
109 DBICTest->populate_schema( $schema );
111 After you deploy your schema you can use this method to populate
112 the tables with test data.
116 sub populate_schema {
120 $schema->populate('Artist', [
121 [ qw/artistid name/ ],
122 [ 1, 'Caterwauler McCrae' ],
123 [ 2, 'Random Boy Band' ],
124 [ 3, 'We Are Goth' ],
127 $schema->populate('CD', [
128 [ qw/cdid artist title year/ ],
129 [ 1, 1, "Spoonful of bees", 1999 ],
130 [ 2, 1, "Forkful of bees", 2001 ],
131 [ 3, 1, "Caterwaulin' Blues", 1997 ],
132 [ 4, 2, "Generic Manufactured Singles", 2001 ],
133 [ 5, 3, "Come Be Depressed With Us", 1998 ],
136 $schema->populate('LinerNotes', [
137 [ qw/liner_id notes/ ],
138 [ 2, "Buy Whiskey!" ],
140 [ 5, "Kill Yourself!" ],
143 $schema->populate('Tag', [
144 [ qw/tagid cd tag/ ],
156 $schema->populate('TwoKeys', [
163 $schema->populate('FourKeys', [
164 [ qw/foo bar hello goodbye sensors/ ],
165 [ 1, 2, 3, 4, 'online' ],
166 [ 5, 4, 3, 6, 'offline' ],
169 $schema->populate('OneKey', [
170 [ qw/id artist cd/ ],
176 $schema->populate('SelfRef', [
182 $schema->populate('SelfRefAlias', [
183 [ qw/self_ref alias/ ],
187 $schema->populate('ArtistUndirectedMap', [
192 $schema->populate('Producer', [
193 [ qw/producerid name/ ],
194 [ 1, 'Matt S Trout' ],
195 [ 2, 'Bob The Builder' ],
196 [ 3, 'Fred The Phenotype' ],
199 $schema->populate('CD_to_Producer', [
206 $schema->populate('TreeLike', [
207 [ qw/id parent name/ ],
216 $schema->populate('Track', [
217 [ qw/trackid cd position title/ ],
218 [ 4, 2, 1, "Stung with Success"],
219 [ 5, 2, 2, "Stripy"],
220 [ 6, 2, 3, "Sticky Honey"],
221 [ 7, 3, 1, "Yowlin"],
222 [ 8, 3, 2, "Howlin"],
223 [ 9, 3, 3, "Fowlin"],
224 [ 10, 4, 1, "Boring Name"],
225 [ 11, 4, 2, "Boring Song"],
226 [ 12, 4, 3, "No More Ideas"],
228 [ 14, 5, 2, "Under The Weather"],
229 [ 15, 5, 3, "Suicidal"],
230 [ 16, 1, 1, "The Bees Knees"],
231 [ 17, 1, 2, "Apiary"],
232 [ 18, 1, 3, "Beehind You"],
235 $schema->populate('Event', [
236 [ qw/id starts_at created_on/ ],
237 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
240 $schema->populate('Link', [
241 [ qw/id url title/ ],
245 $schema->populate('Bookmark', [
250 $schema->populate('Collection', [
251 [ qw/collectionid name/ ],
256 $schema->populate('CollectionObject', [
257 [ qw/collection object/ ],
265 $schema->populate('TypedObject', [
266 [ qw/objectid type value/ ],
267 [ 1, "pointy", "Awl" ],
268 [ 2, "round", "Bearing" ],
269 [ 3, "pointy", "Knife" ],
270 [ 4, "pointy", "Tooth" ],
271 [ 5, "round", "Head" ],
274 $schema->populate('Owners', [
275 [ qw/ownerid name/ ],
280 $schema->populate('BooksInLibrary', [
281 [ qw/id owner title source/ ],
282 [ 1, 1, "Programming Perl", "Library" ],
283 [ 2, 1, "Dynamical Systems", "Library" ],
284 [ 3, 2, "Best Recipe Cookbook", "Library" ],