1 package # hide from PAUSE
12 DBICTest - Library to be used by DBIx::Class test scripts.
20 my $schema = DBICTest->init_schema();
24 This module provides the basic utilities to write tests against
31 my $schema = DBICTest->init_schema(
36 This method removes the test SQLite database in t/var/DBIxClass.db
37 and then creates a new, empty database.
39 This method will call deploy_schema() by default, unless the
40 no_deploy flag is set.
42 Also, by default, this method will call populate_schema() by
43 default, unless the no_deploy or no_populate flags are set.
53 ? "$args{db_dir}/DBIxClass.db"
54 : "t/var/DBIxClass.db"
57 mkdir("t/var") unless -d "t/var";
58 if ( !$args{no_deploy} ) {
59 unlink($db_file) if -e $db_file;
60 unlink($db_file . "-journal") if -e $db_file . "-journal";
63 my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}";
64 my $dbuser = $args{"user"} || '';
65 my $dbpass = $args{"pass"} || '';
69 my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1, sqlite_unicode => 1 });
71 if ($args{compose_connection}) {
72 $schema = DBICTest::Schema->compose_connection(
73 'DBICTest', @connect_info
76 $schema = DBICTest::Schema->compose_namespace('DBICTest')
77 ->connect(@connect_info);
80 if ( !$args{no_deploy} ) {
81 __PACKAGE__->deploy_schema( $schema );
82 __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
92 return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql';
97 DBICTest->deploy_schema( $schema );
105 my $file = shift || $self->get_ddl_file($schema);
106 open( my $fh, "<",$file ) or die "couldnt open $file, $!";
108 { local $/ = undef; $sql = <$fh>; }
110 foreach my $line (split(/;\n/, $sql)) {
113 next if($line =~ /^--/);
114 next if($line =~ /^BEGIN TRANSACTION/m);
115 next if($line =~ /^COMMIT/m);
116 next if $line =~ /^\s+$/; # skip whitespace only
118 $schema->storage->dbh->do($line) || print "Error on SQL: $line\n";
125 DBICTest->clear_schema( $schema );
133 foreach my $class ($schema->sources) {
134 $schema->resultset($class)->delete;
139 =head2 populate_schema
141 DBICTest->populate_schema( $schema );
143 After you deploy your schema you can use this method to populate
144 the tables with test data.
148 sub populate_schema {
152 $schema->populate('Artist', [
153 [ qw/artistid name/ ],
154 [ 1, 'Caterwauler McCrae' ],
155 [ 2, 'Random Boy Band' ],
156 [ 3, 'We Are Goth' ],
157 [ 4, '' ], # Test overridden new will default name to "Test Name" using use_create => 1.
161 $schema->populate('Artist::WashedUp', [
166 $schema->populate('CD', [
167 [ qw/cdid artist title year/ ],
168 [ 1, 1, "Spoonful of bees", 1999 ],
169 [ 2, 1, "Forkful of bees", 2001 ],
170 [ 3, 1, "Caterwaulin' Blues", 1997 ],
171 [ 4, 2, "Generic Manufactured Singles", 2001 ],
172 [ 5, 2, "Unicode Chars ™ © • † ∑ α β « » → …", 2015 ],
173 [ 6, 3, "Übertreibung älterer Umlaute", 1998 ],
176 $schema->populate('Tag', [
177 [ qw/tagid cd tag/ ],
189 $schema->populate('Producer', [
190 [ qw/producerid name/ ],
191 [ 1, 'Matt S Trout' ],
192 [ 2, 'Bob The Builder' ],
193 [ 3, 'Fred The Phenotype' ],
196 $schema->populate('CD_to_Producer', [
206 $schema->populate('Track', [
207 [ qw/trackid cd position title last_updated_on/ ],
208 [ 4, 2, 1, "Stung with Success"],
209 [ 5, 2, 2, "Stripy"],
210 [ 6, 2, 3, "Sticky Honey"],
211 [ 7, 3, 1, "Yowlin"],
212 [ 8, 3, 2, "Howlin"],
213 [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'],
214 [ 10, 4, 1, "Boring Name"],
215 [ 11, 4, 2, "Boring Song"],
216 [ 12, 4, 3, "No More Ideas"],
218 [ 14, 5, 2, "Under The Weather"],
219 [ 15, 5, 3, "Suicidal"],
220 [ 16, 1, 1, "The Bees Knees"],
221 [ 17, 1, 2, "Apiary"],
222 [ 18, 1, 3, "Beehind You"],