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_args}) {
88 $schema->storage_type_args($args{storage_type_args});
90 if( $args{storage_type}) {
91 $schema->storage_type($args{storage_type});
93 if ( !$args{no_connect} ) {
94 $schema = $schema->connect($self->_database);
95 $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
97 if ( !$args{no_deploy} ) {
98 __PACKAGE__->deploy_schema( $schema );
99 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
106 DBICTest->deploy_schema( $schema );
108 This method does one of two things to the schema. It can either call
109 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
110 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
111 file and execute the SQL within. Either way you end up with a fresh set
112 of tables for testing.
120 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
121 return $schema->deploy();
123 open IN, "t/lib/sqlite.sql";
125 { local $/ = undef; $sql = <IN>; }
127 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
131 =head2 populate_schema
133 DBICTest->populate_schema( $schema );
135 After you deploy your schema you can use this method to populate
136 the tables with test data.
140 sub populate_schema {
144 $schema->populate('Artist', [
145 [ qw/artistid name/ ],
146 [ 1, 'Caterwauler McCrae' ],
147 [ 2, 'Random Boy Band' ],
148 [ 3, 'We Are Goth' ],
151 $schema->populate('CD', [
152 [ qw/cdid artist title year/ ],
153 [ 1, 1, "Spoonful of bees", 1999 ],
154 [ 2, 1, "Forkful of bees", 2001 ],
155 [ 3, 1, "Caterwaulin' Blues", 1997 ],
156 [ 4, 2, "Generic Manufactured Singles", 2001 ],
157 [ 5, 3, "Come Be Depressed With Us", 1998 ],
160 $schema->populate('LinerNotes', [
161 [ qw/liner_id notes/ ],
162 [ 2, "Buy Whiskey!" ],
164 [ 5, "Kill Yourself!" ],
167 $schema->populate('Tag', [
168 [ qw/tagid cd tag/ ],
180 $schema->populate('TwoKeys', [
187 $schema->populate('FourKeys', [
188 [ qw/foo bar hello goodbye sensors/ ],
189 [ 1, 2, 3, 4, 'online' ],
190 [ 5, 4, 3, 6, 'offline' ],
193 $schema->populate('OneKey', [
194 [ qw/id artist cd/ ],
200 $schema->populate('SelfRef', [
206 $schema->populate('SelfRefAlias', [
207 [ qw/self_ref alias/ ],
211 $schema->populate('ArtistUndirectedMap', [
216 $schema->populate('Producer', [
217 [ qw/producerid name/ ],
218 [ 1, 'Matt S Trout' ],
219 [ 2, 'Bob The Builder' ],
220 [ 3, 'Fred The Phenotype' ],
223 $schema->populate('CD_to_Producer', [
230 $schema->populate('TreeLike', [
231 [ qw/id parent name/ ],
240 $schema->populate('Track', [
241 [ qw/trackid cd position title/ ],
242 [ 4, 2, 1, "Stung with Success"],
243 [ 5, 2, 2, "Stripy"],
244 [ 6, 2, 3, "Sticky Honey"],
245 [ 7, 3, 1, "Yowlin"],
246 [ 8, 3, 2, "Howlin"],
247 [ 9, 3, 3, "Fowlin"],
248 [ 10, 4, 1, "Boring Name"],
249 [ 11, 4, 2, "Boring Song"],
250 [ 12, 4, 3, "No More Ideas"],
252 [ 14, 5, 2, "Under The Weather"],
253 [ 15, 5, 3, "Suicidal"],
254 [ 16, 1, 1, "The Bees Knees"],
255 [ 17, 1, 2, "Apiary"],
256 [ 18, 1, 3, "Beehind You"],
259 $schema->populate('Event', [
260 [ qw/id starts_at created_on/ ],
261 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
264 $schema->populate('Link', [
265 [ qw/id url title/ ],
269 $schema->populate('Bookmark', [
274 $schema->populate('Collection', [
275 [ qw/collectionid name/ ],
280 $schema->populate('CollectionObject', [
281 [ qw/collection object/ ],
289 $schema->populate('TypedObject', [
290 [ qw/objectid type value/ ],
291 [ 1, "pointy", "Awl" ],
292 [ 2, "round", "Bearing" ],
293 [ 3, "pointy", "Knife" ],
294 [ 4, "pointy", "Tooth" ],
295 [ 5, "round", "Head" ],
298 $schema->populate('Owners', [
299 [ qw/ownerid name/ ],
304 $schema->populate('BooksInLibrary', [
305 [ qw/id owner title source/ ],
306 [ 1, 1, "Programming Perl", "Library" ],
307 [ 2, 1, "Dynamical Systems", "Library" ],
308 [ 3, 2, "Best Recipe Cookbook", "Library" ],