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',
34 balancer_type=>'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
38 This method removes the test SQLite database in t/var/DBIxClass.db
39 and then creates a new, empty database.
41 This method will call deploy_schema() by default, unless the
42 no_deploy flag is set.
44 Also, by default, this method will call populate_schema() by
45 default, unless the no_deploy or no_populate flags are set.
50 return $ENV{"DBICTEST_DSN"} ? 1:0;
53 sub _sqlite_dbfilename {
54 return "t/var/DBIxClass.db";
59 my $db_file = $self->_sqlite_dbfilename;
61 unlink($db_file) if -e $db_file;
62 unlink($db_file . "-journal") if -e $db_file . "-journal";
63 mkdir("t/var") unless -d "t/var";
65 my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
66 my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
67 my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
69 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
80 if ($args{compose_connection}) {
81 $schema = DBICTest::Schema->compose_connection(
82 'DBICTest', $self->_database
85 $schema = DBICTest::Schema->compose_namespace('DBICTest');
87 if( $args{storage_type}) {
88 $schema->storage_type($args{storage_type});
90 if ( !$args{no_connect} ) {
91 $schema = $schema->connect($self->_database);
92 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
94 if ( !$args{no_deploy} ) {
95 __PACKAGE__->deploy_schema( $schema );
96 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
103 DBICTest->deploy_schema( $schema );
105 This method does one of two things to the schema. It can either call
106 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
107 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
108 file and execute the SQL within. Either way you end up with a fresh set
109 of tables for testing.
117 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
118 return $schema->deploy();
120 open IN, "t/lib/sqlite.sql";
122 { local $/ = undef; $sql = <IN>; }
124 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
128 =head2 populate_schema
130 DBICTest->populate_schema( $schema );
132 After you deploy your schema you can use this method to populate
133 the tables with test data.
137 sub populate_schema {
141 $schema->populate('Artist', [
142 [ qw/artistid name/ ],
143 [ 1, 'Caterwauler McCrae' ],
144 [ 2, 'Random Boy Band' ],
145 [ 3, 'We Are Goth' ],
148 $schema->populate('CD', [
149 [ qw/cdid artist title year/ ],
150 [ 1, 1, "Spoonful of bees", 1999 ],
151 [ 2, 1, "Forkful of bees", 2001 ],
152 [ 3, 1, "Caterwaulin' Blues", 1997 ],
153 [ 4, 2, "Generic Manufactured Singles", 2001 ],
154 [ 5, 3, "Come Be Depressed With Us", 1998 ],
157 $schema->populate('LinerNotes', [
158 [ qw/liner_id notes/ ],
159 [ 2, "Buy Whiskey!" ],
161 [ 5, "Kill Yourself!" ],
164 $schema->populate('Tag', [
165 [ qw/tagid cd tag/ ],
177 $schema->populate('TwoKeys', [
184 $schema->populate('FourKeys', [
185 [ qw/foo bar hello goodbye sensors/ ],
186 [ 1, 2, 3, 4, 'online' ],
187 [ 5, 4, 3, 6, 'offline' ],
190 $schema->populate('OneKey', [
191 [ qw/id artist cd/ ],
197 $schema->populate('SelfRef', [
203 $schema->populate('SelfRefAlias', [
204 [ qw/self_ref alias/ ],
208 $schema->populate('ArtistUndirectedMap', [
213 $schema->populate('Producer', [
214 [ qw/producerid name/ ],
215 [ 1, 'Matt S Trout' ],
216 [ 2, 'Bob The Builder' ],
217 [ 3, 'Fred The Phenotype' ],
220 $schema->populate('CD_to_Producer', [
227 $schema->populate('TreeLike', [
228 [ qw/id parent name/ ],
237 $schema->populate('Track', [
238 [ qw/trackid cd position title/ ],
239 [ 4, 2, 1, "Stung with Success"],
240 [ 5, 2, 2, "Stripy"],
241 [ 6, 2, 3, "Sticky Honey"],
242 [ 7, 3, 1, "Yowlin"],
243 [ 8, 3, 2, "Howlin"],
244 [ 9, 3, 3, "Fowlin"],
245 [ 10, 4, 1, "Boring Name"],
246 [ 11, 4, 2, "Boring Song"],
247 [ 12, 4, 3, "No More Ideas"],
249 [ 14, 5, 2, "Under The Weather"],
250 [ 15, 5, 3, "Suicidal"],
251 [ 16, 1, 1, "The Bees Knees"],
252 [ 17, 1, 2, "Apiary"],
253 [ 18, 1, 3, "Beehind You"],
256 $schema->populate('Event', [
257 [ qw/id starts_at created_on/ ],
258 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
261 $schema->populate('Link', [
262 [ qw/id url title/ ],
266 $schema->populate('Bookmark', [
271 $schema->populate('Collection', [
272 [ qw/collectionid name/ ],
277 $schema->populate('CollectionObject', [
278 [ qw/collection object/ ],
286 $schema->populate('TypedObject', [
287 [ qw/objectid type value/ ],
288 [ 1, "pointy", "Awl" ],
289 [ 2, "round", "Bearing" ],
290 [ 3, "pointy", "Knife" ],
291 [ 4, "pointy", "Tooth" ],
292 [ 5, "round", "Head" ],
295 $schema->populate('Owners', [
296 [ qw/ownerid name/ ],
301 $schema->populate('BooksInLibrary', [
302 [ qw/id owner title source/ ],
303 [ 1, 1, "Programming Perl", "Library" ],
304 [ 2, 1, "Dynamical Systems", "Library" ],
305 [ 3, 2, "Best Recipe Cookbook", "Library" ],