1 package # hide from PAUSE
6 use DBICTest::AuthorCheck;
11 DBICTest - Library to be used by DBIx::Class test scripts.
19 my $schema = DBICTest->init_schema();
23 This module provides the basic utilities to write tests against
30 my $schema = DBICTest->init_schema(
33 storage_type=>'::DBI::Replicated',
35 balancer_type=>'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
39 This method removes the test SQLite database in t/var/DBIxClass.db
40 and then creates a new, empty database.
42 This method will call deploy_schema() by default, unless the
43 no_deploy flag is set.
45 Also, by default, this method will call populate_schema() by
46 default, unless the no_deploy or no_populate flags are set.
51 return $ENV{"DBICTEST_DSN"} ? 1:0;
54 sub _sqlite_dbfilename {
55 return "t/var/DBIxClass.db";
61 return $self->_sqlite_dbfilename if $args{sqlite_use_file} or $ENV{"DBICTEST_SQLITE_USE_FILE"};
68 my $db_file = $self->_sqlite_dbname(%args);
70 unlink($db_file) if -e $db_file;
71 unlink($db_file . "-journal") if -e $db_file . "-journal";
72 mkdir("t/var") unless -d "t/var";
74 my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
75 my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
76 my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
78 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1, %args });
89 if ($args{compose_connection}) {
90 $schema = DBICTest::Schema->compose_connection(
91 'DBICTest', $self->_database(%args)
94 $schema = DBICTest::Schema->compose_namespace('DBICTest');
96 if( $args{storage_type}) {
97 $schema->storage_type($args{storage_type});
99 if ( !$args{no_connect} ) {
100 $schema = $schema->connect($self->_database(%args));
101 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF'])
102 unless $self->has_custom_dsn;
104 if ( !$args{no_deploy} ) {
105 __PACKAGE__->deploy_schema( $schema, $args{deploy_args} );
106 __PACKAGE__->populate_schema( $schema )
107 if( !$args{no_populate} );
114 DBICTest->deploy_schema( $schema );
116 This method does one of two things to the schema. It can either call
117 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
118 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
119 file and execute the SQL within. Either way you end up with a fresh set
120 of tables for testing.
127 my $args = shift || {};
129 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
130 $schema->deploy($args);
132 open IN, "t/lib/sqlite.sql";
134 { local $/ = undef; $sql = <IN>; }
136 for my $chunk ( split (/;\s*\n+/, $sql) ) {
137 if ( $chunk =~ / ^ (?! --\s* ) \S /xm ) { # there is some real sql in the chunk - a non-space at the start of the string which is not a comment
138 $schema->storage->dbh->do($chunk) or print "Error on SQL: $chunk\n";
145 =head2 populate_schema
147 DBICTest->populate_schema( $schema );
149 After you deploy your schema you can use this method to populate
150 the tables with test data.
154 sub populate_schema {
158 $schema->populate('Artist', [
159 [ qw/artistid name/ ],
160 [ 1, 'Caterwauler McCrae' ],
161 [ 2, 'Random Boy Band' ],
162 [ 3, 'We Are Goth' ],
165 $schema->populate('CD', [
166 [ qw/cdid artist title year/ ],
167 [ 1, 1, "Spoonful of bees", 1999 ],
168 [ 2, 1, "Forkful of bees", 2001 ],
169 [ 3, 1, "Caterwaulin' Blues", 1997 ],
170 [ 4, 2, "Generic Manufactured Singles", 2001 ],
171 [ 5, 3, "Come Be Depressed With Us", 1998 ],
174 $schema->populate('LinerNotes', [
175 [ qw/liner_id notes/ ],
176 [ 2, "Buy Whiskey!" ],
178 [ 5, "Kill Yourself!" ],
181 $schema->populate('Tag', [
182 [ qw/tagid cd tag/ ],
194 $schema->populate('TwoKeys', [
201 $schema->populate('FourKeys', [
202 [ qw/foo bar hello goodbye sensors/ ],
203 [ 1, 2, 3, 4, 'online' ],
204 [ 5, 4, 3, 6, 'offline' ],
207 $schema->populate('OneKey', [
208 [ qw/id artist cd/ ],
214 $schema->populate('SelfRef', [
220 $schema->populate('SelfRefAlias', [
221 [ qw/self_ref alias/ ],
225 $schema->populate('ArtistUndirectedMap', [
230 $schema->populate('Producer', [
231 [ qw/producerid name/ ],
232 [ 1, 'Matt S Trout' ],
233 [ 2, 'Bob The Builder' ],
234 [ 3, 'Fred The Phenotype' ],
237 $schema->populate('CD_to_Producer', [
244 $schema->populate('TreeLike', [
245 [ qw/id parent name/ ],
246 [ 1, undef, 'root' ],
255 $schema->populate('Track', [
256 [ qw/trackid cd position title/ ],
257 [ 4, 2, 1, "Stung with Success"],
258 [ 5, 2, 2, "Stripy"],
259 [ 6, 2, 3, "Sticky Honey"],
260 [ 7, 3, 1, "Yowlin"],
261 [ 8, 3, 2, "Howlin"],
262 [ 9, 3, 3, "Fowlin"],
263 [ 10, 4, 1, "Boring Name"],
264 [ 11, 4, 2, "Boring Song"],
265 [ 12, 4, 3, "No More Ideas"],
267 [ 14, 5, 2, "Under The Weather"],
268 [ 15, 5, 3, "Suicidal"],
269 [ 16, 1, 1, "The Bees Knees"],
270 [ 17, 1, 2, "Apiary"],
271 [ 18, 1, 3, "Beehind You"],
274 $schema->populate('Event', [
275 [ qw/id starts_at created_on varchar_date varchar_datetime skip_inflation/ ],
276 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05', '2006-07-23', '2006-05-22 19:05:07', '2006-04-21 18:04:06'],
279 $schema->populate('Link', [
280 [ qw/id url title/ ],
284 $schema->populate('Bookmark', [
289 $schema->populate('Collection', [
290 [ qw/collectionid name/ ],
295 $schema->populate('TypedObject', [
296 [ qw/objectid type value/ ],
297 [ 1, "pointy", "Awl" ],
298 [ 2, "round", "Bearing" ],
299 [ 3, "pointy", "Knife" ],
300 [ 4, "pointy", "Tooth" ],
301 [ 5, "round", "Head" ],
303 $schema->populate('CollectionObject', [
304 [ qw/collection object/ ],
312 $schema->populate('Owners', [
318 $schema->populate('BooksInLibrary', [
319 [ qw/id owner title source price/ ],
320 [ 1, 1, "Programming Perl", "Library", 23 ],
321 [ 2, 1, "Dynamical Systems", "Library", 37 ],
322 [ 3, 2, "Best Recipe Cookbook", "Library", 65 ],