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 {
56 return "t/var/DBIxClass.db" if $args{sqlite_use_file} or $ENV{"DBICTEST_SQLITE_USE_FILE"};
63 my $db_file = $self->_sqlite_dbfilename(%args);
65 unlink($db_file) if -e $db_file;
66 unlink($db_file . "-journal") if -e $db_file . "-journal";
67 mkdir("t/var") unless -d "t/var";
69 my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
70 my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
71 my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
73 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
84 if ($args{compose_connection}) {
85 $schema = DBICTest::Schema->compose_connection(
86 'DBICTest', $self->_database(%args)
89 $schema = DBICTest::Schema->compose_namespace('DBICTest');
91 if( $args{storage_type}) {
92 $schema->storage_type($args{storage_type});
94 if ( !$args{no_connect} ) {
95 $schema = $schema->connect($self->_database(%args));
96 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF'])
97 unless $self->has_custom_dsn;
99 if ( !$args{no_deploy} ) {
100 __PACKAGE__->deploy_schema( $schema, $args{deploy_args} );
101 __PACKAGE__->populate_schema( $schema )
102 if( !$args{no_populate} );
109 DBICTest->deploy_schema( $schema );
111 This method does one of two things to the schema. It can either call
112 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
113 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
114 file and execute the SQL within. Either way you end up with a fresh set
115 of tables for testing.
122 my $args = shift || {};
124 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
125 $schema->deploy($args);
127 open IN, "t/lib/sqlite.sql";
129 { local $/ = undef; $sql = <IN>; }
131 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
136 =head2 populate_schema
138 DBICTest->populate_schema( $schema );
140 After you deploy your schema you can use this method to populate
141 the tables with test data.
145 sub populate_schema {
149 $schema->populate('Artist', [
150 [ qw/artistid name/ ],
151 [ 1, 'Caterwauler McCrae' ],
152 [ 2, 'Random Boy Band' ],
153 [ 3, 'We Are Goth' ],
156 $schema->populate('CD', [
157 [ qw/cdid artist title year/ ],
158 [ 1, 1, "Spoonful of bees", 1999 ],
159 [ 2, 1, "Forkful of bees", 2001 ],
160 [ 3, 1, "Caterwaulin' Blues", 1997 ],
161 [ 4, 2, "Generic Manufactured Singles", 2001 ],
162 [ 5, 3, "Come Be Depressed With Us", 1998 ],
165 $schema->populate('LinerNotes', [
166 [ qw/liner_id notes/ ],
167 [ 2, "Buy Whiskey!" ],
169 [ 5, "Kill Yourself!" ],
172 $schema->populate('Tag', [
173 [ qw/tagid cd tag/ ],
185 $schema->populate('TwoKeys', [
192 $schema->populate('FourKeys', [
193 [ qw/foo bar hello goodbye sensors/ ],
194 [ 1, 2, 3, 4, 'online' ],
195 [ 5, 4, 3, 6, 'offline' ],
198 $schema->populate('OneKey', [
199 [ qw/id artist cd/ ],
205 $schema->populate('SelfRef', [
211 $schema->populate('SelfRefAlias', [
212 [ qw/self_ref alias/ ],
216 $schema->populate('ArtistUndirectedMap', [
221 $schema->populate('Producer', [
222 [ qw/producerid name/ ],
223 [ 1, 'Matt S Trout' ],
224 [ 2, 'Bob The Builder' ],
225 [ 3, 'Fred The Phenotype' ],
228 $schema->populate('CD_to_Producer', [
235 $schema->populate('TreeLike', [
236 [ qw/id parent name/ ],
237 [ 1, undef, 'root' ],
246 $schema->populate('Track', [
247 [ qw/trackid cd position title/ ],
248 [ 4, 2, 1, "Stung with Success"],
249 [ 5, 2, 2, "Stripy"],
250 [ 6, 2, 3, "Sticky Honey"],
251 [ 7, 3, 1, "Yowlin"],
252 [ 8, 3, 2, "Howlin"],
253 [ 9, 3, 3, "Fowlin"],
254 [ 10, 4, 1, "Boring Name"],
255 [ 11, 4, 2, "Boring Song"],
256 [ 12, 4, 3, "No More Ideas"],
258 [ 14, 5, 2, "Under The Weather"],
259 [ 15, 5, 3, "Suicidal"],
260 [ 16, 1, 1, "The Bees Knees"],
261 [ 17, 1, 2, "Apiary"],
262 [ 18, 1, 3, "Beehind You"],
265 $schema->populate('Event', [
266 [ qw/id starts_at created_on/ ],
267 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
270 $schema->populate('Link', [
271 [ qw/id url title/ ],
275 $schema->populate('Bookmark', [
280 $schema->populate('Collection', [
281 [ qw/collectionid name/ ],
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" ],
294 $schema->populate('CollectionObject', [
295 [ qw/collection object/ ],
303 $schema->populate('Owners', [
304 [ qw/ownerid name/ ],
309 $schema->populate('BooksInLibrary', [
310 [ qw/id owner title source/ ],
311 [ 1, 1, "Programming Perl", "Library" ],
312 [ 2, 1, "Dynamical Systems", "Library" ],
313 [ 3, 2, "Best Recipe Cookbook", "Library" ],