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(
32 storage_type=>'::DBI::Replicated',
35 This method removes the test SQLite database in t/var/DBIxClass.db
36 and then creates a new, empty database.
38 This method will call deploy_schema() by default, unless the
39 no_deploy flag is set.
41 Also, by default, this method will call populate_schema() by
42 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 @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
69 if ($args{compose_connection}) {
70 $schema = DBICTest::Schema->compose_connection(
71 'DBICTest', $self->_database
74 $schema = DBICTest::Schema->compose_namespace('DBICTest');
76 if( $args{storage_type}) {
77 $schema->storage_type($args{storage_type});
79 if ( !$args{no_connect} ) {
80 $schema = $schema->connect($self->_database);
81 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
83 if ( !$args{no_deploy} ) {
84 __PACKAGE__->deploy_schema( $schema );
85 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
92 DBICTest->deploy_schema( $schema );
94 This method does one of two things to the schema. It can either call
95 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
96 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
97 file and execute the SQL within. Either way you end up with a fresh set
98 of tables for testing.
106 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
107 return $schema->deploy();
109 open IN, "t/lib/sqlite.sql";
111 { local $/ = undef; $sql = <IN>; }
113 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
117 =head2 populate_schema
119 DBICTest->populate_schema( $schema );
121 After you deploy your schema you can use this method to populate
122 the tables with test data.
126 sub populate_schema {
130 $schema->populate('Artist', [
131 [ qw/artistid name/ ],
132 [ 1, 'Caterwauler McCrae' ],
133 [ 2, 'Random Boy Band' ],
134 [ 3, 'We Are Goth' ],
137 $schema->populate('CD', [
138 [ qw/cdid artist title year/ ],
139 [ 1, 1, "Spoonful of bees", 1999 ],
140 [ 2, 1, "Forkful of bees", 2001 ],
141 [ 3, 1, "Caterwaulin' Blues", 1997 ],
142 [ 4, 2, "Generic Manufactured Singles", 2001 ],
143 [ 5, 3, "Come Be Depressed With Us", 1998 ],
146 $schema->populate('LinerNotes', [
147 [ qw/liner_id notes/ ],
148 [ 2, "Buy Whiskey!" ],
150 [ 5, "Kill Yourself!" ],
153 $schema->populate('Tag', [
154 [ qw/tagid cd tag/ ],
166 $schema->populate('TwoKeys', [
173 $schema->populate('FourKeys', [
174 [ qw/foo bar hello goodbye sensors/ ],
175 [ 1, 2, 3, 4, 'online' ],
176 [ 5, 4, 3, 6, 'offline' ],
179 $schema->populate('OneKey', [
180 [ qw/id artist cd/ ],
186 $schema->populate('SelfRef', [
192 $schema->populate('SelfRefAlias', [
193 [ qw/self_ref alias/ ],
197 $schema->populate('ArtistUndirectedMap', [
202 $schema->populate('Producer', [
203 [ qw/producerid name/ ],
204 [ 1, 'Matt S Trout' ],
205 [ 2, 'Bob The Builder' ],
206 [ 3, 'Fred The Phenotype' ],
209 $schema->populate('CD_to_Producer', [
216 $schema->populate('TreeLike', [
217 [ qw/id parent name/ ],
226 $schema->populate('Track', [
227 [ qw/trackid cd position title/ ],
228 [ 4, 2, 1, "Stung with Success"],
229 [ 5, 2, 2, "Stripy"],
230 [ 6, 2, 3, "Sticky Honey"],
231 [ 7, 3, 1, "Yowlin"],
232 [ 8, 3, 2, "Howlin"],
233 [ 9, 3, 3, "Fowlin"],
234 [ 10, 4, 1, "Boring Name"],
235 [ 11, 4, 2, "Boring Song"],
236 [ 12, 4, 3, "No More Ideas"],
238 [ 14, 5, 2, "Under The Weather"],
239 [ 15, 5, 3, "Suicidal"],
240 [ 16, 1, 1, "The Bees Knees"],
241 [ 17, 1, 2, "Apiary"],
242 [ 18, 1, 3, "Beehind You"],
245 $schema->populate('Event', [
246 [ qw/id starts_at created_on/ ],
247 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
250 $schema->populate('Link', [
251 [ qw/id url title/ ],
255 $schema->populate('Bookmark', [
260 $schema->populate('Collection', [
261 [ qw/collectionid name/ ],
266 $schema->populate('CollectionObject', [
267 [ qw/collection object/ ],
275 $schema->populate('TypedObject', [
276 [ qw/objectid type value/ ],
277 [ 1, "pointy", "Awl" ],
278 [ 2, "round", "Bearing" ],
279 [ 3, "pointy", "Knife" ],
280 [ 4, "pointy", "Tooth" ],
281 [ 5, "round", "Head" ],
284 $schema->populate('Owners', [
285 [ qw/ownerid name/ ],
290 $schema->populate('BooksInLibrary', [
291 [ qw/id owner title source/ ],
292 [ 1, 1, "Programming Perl", "Library" ],
293 [ 2, 1, "Dynamical Systems", "Library" ],
294 [ 3, 2, "Best Recipe Cookbook", "Library" ],