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'])
93 unless $self->has_custom_dsn;
95 if ( !$args{no_deploy} ) {
96 __PACKAGE__->deploy_schema( $schema, $args{deploy_args} );
97 __PACKAGE__->populate_schema( $schema )
98 if( !$args{no_populate} );
105 DBICTest->deploy_schema( $schema );
107 This method does one of two things to the schema. It can either call
108 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment
109 variable is set, otherwise the default is to read in the t/lib/sqlite.sql
110 file and execute the SQL within. Either way you end up with a fresh set
111 of tables for testing.
118 my $args = shift || {};
120 if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
121 $schema->deploy($args);
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);
132 =head2 populate_schema
134 DBICTest->populate_schema( $schema );
136 After you deploy your schema you can use this method to populate
137 the tables with test data.
141 sub populate_schema {
145 $schema->populate('Artist', [
146 [ qw/artistid name/ ],
147 [ 1, 'Caterwauler McCrae' ],
148 [ 2, 'Random Boy Band' ],
149 [ 3, 'We Are Goth' ],
152 $schema->populate('CD', [
153 [ qw/cdid artist title year/ ],
154 [ 1, 1, "Spoonful of bees", 1999 ],
155 [ 2, 1, "Forkful of bees", 2001 ],
156 [ 3, 1, "Caterwaulin' Blues", 1997 ],
157 [ 4, 2, "Generic Manufactured Singles", 2001 ],
158 [ 5, 3, "Come Be Depressed With Us", 1998 ],
161 $schema->populate('LinerNotes', [
162 [ qw/liner_id notes/ ],
163 [ 2, "Buy Whiskey!" ],
165 [ 5, "Kill Yourself!" ],
168 $schema->populate('Tag', [
169 [ qw/tagid cd tag/ ],
181 $schema->populate('TwoKeys', [
188 $schema->populate('FourKeys', [
189 [ qw/foo bar hello goodbye sensors/ ],
190 [ 1, 2, 3, 4, 'online' ],
191 [ 5, 4, 3, 6, 'offline' ],
194 $schema->populate('OneKey', [
195 [ qw/id artist cd/ ],
201 $schema->populate('SelfRef', [
207 $schema->populate('SelfRefAlias', [
208 [ qw/self_ref alias/ ],
212 $schema->populate('ArtistUndirectedMap', [
217 $schema->populate('Producer', [
218 [ qw/producerid name/ ],
219 [ 1, 'Matt S Trout' ],
220 [ 2, 'Bob The Builder' ],
221 [ 3, 'Fred The Phenotype' ],
224 $schema->populate('CD_to_Producer', [
231 $schema->populate('TreeLike', [
232 [ qw/id parent name/ ],
233 [ 1, undef, 'root' ],
242 $schema->populate('Track', [
243 [ qw/trackid cd position title/ ],
244 [ 4, 2, 1, "Stung with Success"],
245 [ 5, 2, 2, "Stripy"],
246 [ 6, 2, 3, "Sticky Honey"],
247 [ 7, 3, 1, "Yowlin"],
248 [ 8, 3, 2, "Howlin"],
249 [ 9, 3, 3, "Fowlin"],
250 [ 10, 4, 1, "Boring Name"],
251 [ 11, 4, 2, "Boring Song"],
252 [ 12, 4, 3, "No More Ideas"],
254 [ 14, 5, 2, "Under The Weather"],
255 [ 15, 5, 3, "Suicidal"],
256 [ 16, 1, 1, "The Bees Knees"],
257 [ 17, 1, 2, "Apiary"],
258 [ 18, 1, 3, "Beehind You"],
261 $schema->populate('Event', [
262 [ qw/id starts_at created_on/ ],
263 [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
266 $schema->populate('Link', [
267 [ qw/id url title/ ],
271 $schema->populate('Bookmark', [
276 $schema->populate('Collection', [
277 [ qw/collectionid name/ ],
282 $schema->populate('TypedObject', [
283 [ qw/objectid type value/ ],
284 [ 1, "pointy", "Awl" ],
285 [ 2, "round", "Bearing" ],
286 [ 3, "pointy", "Knife" ],
287 [ 4, "pointy", "Tooth" ],
288 [ 5, "round", "Head" ],
290 $schema->populate('CollectionObject', [
291 [ qw/collection object/ ],
299 $schema->populate('Owners', [
300 [ qw/ownerid name/ ],
305 $schema->populate('BooksInLibrary', [
306 [ qw/id owner title source/ ],
307 [ 1, 1, "Programming Perl", "Library" ],
308 [ 2, 1, "Dynamical Systems", "Library" ],
309 [ 3, 2, "Best Recipe Cookbook", "Library" ],