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";
49 my $db_audit = 't/var/Audit.db';
51 unlink($db_file) if -e $db_file;
52 unlink($db_file . "-journal") if -e $db_file . "-journal";
53 unlink($db_audit) if -e $db_audit;
54 unlink($db_audit . "-journal") if -e $db_audit . "-journal";
55 mkdir("t/var") unless -d "t/var";
57 my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
58 my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
59 my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
62 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
64 if ($args{compose_connection}) {
65 $schema = DBICTest::Schema->compose_connection(
66 'DBICTest', @connect_info
69 $schema = DBICTest::Schema->compose_namespace('DBICTest')
70 ->connect(@connect_info);
71 print STDERR "Created Schema: ", ref($schema), "\n";
73 # my $schema = DBICTest::Schema->compose_connection('DBICTest' => $dsn, $dbuser, $dbpass);
74 # $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
75 if ( !$args{no_deploy} ) {
76 __PACKAGE__->deploy_schema( $schema );
77 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
84 DBICTest->deploy_schema( $schema );
86 This method does one of two things to the schema. It can either call
87 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
88 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
89 file and execute the SQL within. Either way you end up with a fresh set
90 of tables for testing.
98 # if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
99 return $schema->deploy();
101 # open IN, "t/lib/sqlite.sql";
103 # { local $/ = undef; $sql = <IN>; }
105 # ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
109 =head2 populate_schema
111 DBICTest->populate_schema( $schema );
113 After you deploy your schema you can use this method to populate
114 the tables with test data.
118 sub populate_schema {
122 $schema->populate('Artist', [
123 [ qw/artistid name/ ],
124 [ 1, 'Caterwauler McCrae' ],
125 [ 2, 'Random Boy Band' ],
126 [ 3, 'We Are Goth' ],
129 $schema->populate('CD', [
130 [ qw/cdid artist title year/ ],
131 [ 1, 1, "Spoonful of bees", 1999 ],
132 [ 2, 1, "Forkful of bees", 2001 ],
133 [ 3, 1, "Caterwaulin' Blues", 1997 ],
134 [ 4, 2, "Generic Manufactured Singles", 2001 ],
135 [ 5, 3, "Come Be Depressed With Us", 1998 ],
138 $schema->populate('LinerNotes', [
139 [ qw/liner_id notes/ ],
140 [ 2, "Buy Whiskey!" ],
142 [ 5, "Kill Yourself!" ],
145 $schema->populate('Tag', [
146 [ qw/tagid cd tag/ ],
158 $schema->populate('TwoKeys', [
165 $schema->populate('FourKeys', [
166 [ qw/foo bar hello goodbye sensors/ ],
167 [ 1, 2, 3, 4, 'online' ],
168 [ 5, 4, 3, 6, 'offline' ],
171 $schema->populate('OneKey', [
172 [ qw/id artist cd/ ],
178 $schema->populate('SelfRef', [
184 $schema->populate('SelfRefAlias', [
185 [ qw/self_ref alias/ ],
189 $schema->populate('ArtistUndirectedMap', [
194 $schema->populate('Producer', [
195 [ qw/producerid name/ ],
196 [ 1, 'Matt S Trout' ],
197 [ 2, 'Bob The Builder' ],
198 [ 3, 'Fred The Phenotype' ],
201 $schema->populate('CD_to_Producer', [
208 $schema->populate('TreeLike', [
209 [ qw/id parent name/ ],
218 $schema->populate('Track', [
219 [ qw/trackid cd position title/ ],
220 [ 4, 2, 1, "Stung with Success"],
221 [ 5, 2, 2, "Stripy"],
222 [ 6, 2, 3, "Sticky Honey"],
223 [ 7, 3, 1, "Yowlin"],
224 [ 8, 3, 2, "Howlin"],
225 [ 9, 3, 3, "Fowlin"],
226 [ 10, 4, 1, "Boring Name"],
227 [ 11, 4, 2, "Boring Song"],
228 [ 12, 4, 3, "No More Ideas"],
230 [ 14, 5, 2, "Under The Weather"],
231 [ 15, 5, 3, "Suicidal"],
232 [ 16, 1, 1, "The Bees Knees"],
233 [ 17, 1, 2, "Apiary"],
234 [ 18, 1, 3, "Beehind You"],
237 $schema->populate('Event', [
238 [ qw/id starts_at created_on/ ],
239 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
242 $schema->populate('Link', [
247 $schema->populate('Bookmark', [
252 $schema->populate('Collection', [
253 [ qw/collectionid name/ ],
258 $schema->populate('CollectionObject', [
259 [ qw/collection object/ ],
267 $schema->populate('TypedObject', [
268 [ qw/objectid type value/ ],
269 [ 1, "pointy", "Awl" ],
270 [ 2, "round", "Bearing" ],
271 [ 3, "pointy", "Knife" ],
272 [ 4, "pointy", "Tooth" ],
273 [ 5, "round", "Head" ],
276 $schema->populate('Owners', [
277 [ qw/ownerid name/ ],
282 $schema->populate('BooksInLibrary', [
283 [ qw/id owner title source/ ],
284 [ 1, 1, "Programming Perl", "Library" ],
285 [ 2, 1, "Dynamical Systems", "Library" ],
286 [ 3, 2, "Best Recipe Cookbook", "Library" ],