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(
34 This method removes the test SQLite database in t/var/DBIxClass.db
35 and then creates a new, empty database.
37 This method will call deploy_schema() by default, unless the
38 no_deploy flag is set.
40 Also, by default, this method will call populate_schema() by
41 default, unless the no_deploy or no_populate flags are set.
49 my $db_file = "t/var/DBIxClass.db";
51 unlink($db_file) if -e $db_file;
52 unlink($db_file . "-journal") if -e $db_file . "-journal";
53 mkdir("t/var") unless -d "t/var";
55 my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}";
56 my $dbuser = $args{"user"} || '';
57 my $dbpass = $args{"pass"} || '';
61 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
63 if ($args{compose_connection}) {
64 $schema = RestTest::Schema->compose_connection(
65 'DBICTest', @connect_info
68 $schema = RestTest::Schema->compose_namespace('DBICTest')
69 ->connect(@connect_info);
72 if ( !$args{no_deploy} ) {
73 __PACKAGE__->deploy_schema( $schema );
74 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
84 return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql';
89 DBICTest->deploy_schema( $schema );
97 my $file = shift || $self->get_ddl_file($schema);
100 { local $/ = undef; $sql = <IN>; }
102 ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
108 DBICTest->clear_schema( $schema );
116 foreach my $class ($schema->sources) {
117 $schema->resultset($class)->delete;
122 =head2 populate_schema
124 DBICTest->populate_schema( $schema );
126 After you deploy your schema you can use this method to populate
127 the tables with test data.
131 sub populate_schema {
135 $schema->populate('Artist', [
136 [ qw/artistid name/ ],
137 [ 1, 'Caterwauler McCrae' ],
138 [ 2, 'Random Boy Band' ],
139 [ 3, 'We Are Goth' ],
142 $schema->populate('CD', [
143 [ qw/cdid artist title year/ ],
144 [ 1, 1, "Spoonful of bees", 1999 ],
145 [ 2, 1, "Forkful of bees", 2001 ],
146 [ 3, 1, "Caterwaulin' Blues", 1997 ],
147 [ 4, 2, "Generic Manufactured Singles", 2001 ],
148 [ 5, 2, "We like girls and stuff", 2003 ],
149 [ 6, 3, "Come Be Depressed With Us", 1998 ],
152 $schema->populate('Tag', [
153 [ qw/tagid cd tag/ ],
165 $schema->populate('Producer', [
166 [ qw/producerid name/ ],
167 [ 1, 'Matt S Trout' ],
168 [ 2, 'Bob The Builder' ],
169 [ 3, 'Fred The Phenotype' ],
172 $schema->populate('CD_to_Producer', [
179 $schema->populate('Track', [
180 [ qw/trackid cd position title last_updated_on/ ],
181 [ 4, 2, 1, "Stung with Success"],
182 [ 5, 2, 2, "Stripy"],
183 [ 6, 2, 3, "Sticky Honey"],
184 [ 7, 3, 1, "Yowlin"],
185 [ 8, 3, 2, "Howlin"],
186 [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'],
187 [ 10, 4, 1, "Boring Name"],
188 [ 11, 4, 2, "Boring Song"],
189 [ 12, 4, 3, "No More Ideas"],
191 [ 14, 5, 2, "Under The Weather"],
192 [ 15, 5, 3, "Suicidal"],
193 [ 16, 1, 1, "The Bees Knees"],
194 [ 17, 1, 2, "Apiary"],
195 [ 18, 1, 3, "Beehind You"],