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 mkdir("t/var") unless -d "t/var";
52 if ( !$args{no_deploy} ) {
53 unlink($db_file) if -e $db_file;
54 unlink($db_file . "-journal") if -e $db_file . "-journal";
57 my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}";
58 my $dbuser = $args{"user"} || '';
59 my $dbpass = $args{"pass"} || '';
63 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
65 if ($args{compose_connection}) {
66 $schema = DBICTest::Schema->compose_connection(
67 'DBICTest', @connect_info
70 $schema = DBICTest::Schema->compose_namespace('DBICTest')
71 ->connect(@connect_info);
74 if ( !$args{no_deploy} ) {
75 __PACKAGE__->deploy_schema( $schema );
76 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
86 return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql';
91 DBICTest->deploy_schema( $schema );
99 my $file = shift || $self->get_ddl_file($schema);
100 open( my $fh, "<",$file ) or die "couldnt open $file, $!";
102 { local $/ = undef; $sql = <$fh>; }
104 foreach my $line (split(/;\n/, $sql)) {
107 next if($line =~ /^--/);
108 next if($line =~ /^BEGIN TRANSACTION/m);
109 next if($line =~ /^COMMIT/m);
110 next if $line =~ /^\s+$/; # skip whitespace only
112 $schema->storage->dbh->do($line) || print "Error on SQL: $line\n";
119 DBICTest->clear_schema( $schema );
127 foreach my $class ($schema->sources) {
128 $schema->resultset($class)->delete;
133 =head2 populate_schema
135 DBICTest->populate_schema( $schema );
137 After you deploy your schema you can use this method to populate
138 the tables with test data.
142 sub populate_schema {
146 $schema->populate('Artist', [
147 [ qw/artistid name/ ],
148 [ 1, 'Caterwauler McCrae' ],
149 [ 2, 'Random Boy Band' ],
150 [ 3, 'We Are Goth' ],
151 [ 4, '' ], # Test overridden new will default name to "Test Name" using use_create => 1.
155 $schema->populate('Artist::WashedUp', [
160 $schema->populate('CD', [
161 [ qw/cdid artist title year/ ],
162 [ 1, 1, "Spoonful of bees", 1999 ],
163 [ 2, 1, "Forkful of bees", 2001 ],
164 [ 3, 1, "Caterwaulin' Blues", 1997 ],
165 [ 4, 2, "Generic Manufactured Singles", 2001 ],
166 [ 5, 2, "We like girls and stuff", 2003 ],
167 [ 6, 3, "Come Be Depressed With Us", 1998 ],
170 $schema->populate('Tag', [
171 [ qw/tagid cd tag/ ],
183 $schema->populate('Producer', [
184 [ qw/producerid name/ ],
185 [ 1, 'Matt S Trout' ],
186 [ 2, 'Bob The Builder' ],
187 [ 3, 'Fred The Phenotype' ],
190 $schema->populate('CD_to_Producer', [
200 $schema->populate('Track', [
201 [ qw/trackid cd position title last_updated_on/ ],
202 [ 4, 2, 1, "Stung with Success"],
203 [ 5, 2, 2, "Stripy"],
204 [ 6, 2, 3, "Sticky Honey"],
205 [ 7, 3, 1, "Yowlin"],
206 [ 8, 3, 2, "Howlin"],
207 [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'],
208 [ 10, 4, 1, "Boring Name"],
209 [ 11, 4, 2, "Boring Song"],
210 [ 12, 4, 3, "No More Ideas"],
212 [ 14, 5, 2, "Under The Weather"],
213 [ 15, 5, 3, "Suicidal"],
214 [ 16, 1, 1, "The Bees Knees"],
215 [ 17, 1, 2, "Apiary"],
216 [ 18, 1, 3, "Beehind You"],