Merge branch 'topic/parallelize-tests' of https://github.com/RsrchBoy/DBIx-Class...
[dbsrgits/DBIx-Class-Fixtures.git] / t / lib / DBICTest.pm
1 package # hide from PAUSE 
2     DBICTest;
3
4 use strict;
5 use warnings;
6 use DBICTest::Schema;
7
8 =head1 NAME
9
10 DBICTest - Library to be used by DBIx::Class test scripts.
11
12 =head1 SYNOPSIS
13
14   use lib qw(t/lib);
15   use DBICTest;
16   use Test::More;
17   
18   my $schema = DBICTest->init_schema();
19
20 =head1 DESCRIPTION
21
22 This module provides the basic utilities to write tests against 
23 DBIx::Class.
24
25 =head1 METHODS
26
27 =head2 init_schema
28
29   my $schema = DBICTest->init_schema(
30     no_deploy=>1,
31     no_populate=>1,
32   );
33
34 This method removes the test SQLite database in t/var/DBIxClass.db 
35 and then creates a new, empty database.
36
37 This method will call deploy_schema() by default, unless the 
38 no_deploy flag is set.
39
40 Also, by default, this method will call populate_schema() by 
41 default, unless the no_deploy or no_populate flags are set.
42
43 =cut
44
45 sub init_schema {
46     my $self = shift;
47     my %args = @_;
48
49     my $db_file
50         = $args{db_dir}
51         ? "$args{db_dir}/DBIxClass.db"
52         : "t/var/DBIxClass.db"
53         ;
54
55     mkdir("t/var") unless -d "t/var";
56     if ( !$args{no_deploy} ) {
57       unlink($db_file) if -e $db_file;
58       unlink($db_file . "-journal") if -e $db_file . "-journal";
59     }
60
61     my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}";
62     my $dbuser = $args{"user"} || '';
63     my $dbpass = $args{"pass"} || '';
64
65     my $schema;
66
67     my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
68
69     if ($args{compose_connection}) {
70       $schema = DBICTest::Schema->compose_connection(
71                   'DBICTest', @connect_info
72                 );
73     } else {
74       $schema = DBICTest::Schema->compose_namespace('DBICTest')
75                                 ->connect(@connect_info);
76     }
77
78     if ( !$args{no_deploy} ) {
79         __PACKAGE__->deploy_schema( $schema );
80         __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
81     }
82     return $schema;
83 }
84
85
86 sub get_ddl_file {
87   my $self = shift;
88   my $schema = shift;
89
90   return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql';
91 }
92
93 =head2 deploy_schema
94
95   DBICTest->deploy_schema( $schema );
96
97 =cut
98
99 sub deploy_schema {
100     my $self = shift;
101     my $schema = shift;
102
103     my $file = shift || $self->get_ddl_file($schema);
104     open(  my $fh, "<",$file ) or die "couldnt open $file, $!";
105     my $sql;
106     { local $/ = undef; $sql = <$fh>; }
107
108     foreach my $line (split(/;\n/, $sql)) {
109       print "$line\n";
110       next if(!$line);
111       next if($line =~ /^--/);
112       next if($line =~ /^BEGIN TRANSACTION/m);
113       next if($line =~ /^COMMIT/m);
114       next if $line =~ /^\s+$/; # skip whitespace only
115
116       $schema->storage->dbh->do($line) || print "Error on SQL: $line\n";
117     }
118 }
119  
120
121 =head2 clear_schema
122
123   DBICTest->clear_schema( $schema );
124
125 =cut
126
127 sub clear_schema {
128     my $self = shift;
129     my $schema = shift;
130
131     foreach my $class ($schema->sources) {
132       $schema->resultset($class)->delete;
133     }
134 }
135
136
137 =head2 populate_schema
138
139   DBICTest->populate_schema( $schema );
140
141 After you deploy your schema you can use this method to populate 
142 the tables with test data.
143
144 =cut
145
146 sub populate_schema {
147     my $self = shift;
148     my $schema = shift;
149
150     $schema->populate('Artist', [
151         [ qw/artistid name/ ],
152         [ 1, 'Caterwauler McCrae' ],
153         [ 2, 'Random Boy Band' ],
154         [ 3, 'We Are Goth' ],
155         [ 4, '' ], # Test overridden new will default name to "Test Name" using use_create => 1.
156         [ 32948, 'Big PK' ],
157     ]);
158
159     $schema->populate('Artist::WashedUp', [
160         [ qw/fk_artistid/ ],
161         [ 2 ],
162     ]);
163
164     $schema->populate('CD', [
165         [ qw/cdid artist title year/ ],
166         [ 1, 1, "Spoonful of bees", 1999 ],
167         [ 2, 1, "Forkful of bees", 2001 ],
168         [ 3, 1, "Caterwaulin' Blues", 1997 ],
169         [ 4, 2, "Generic Manufactured Singles", 2001 ],
170         [ 5, 2, "We like girls and stuff", 2003 ],
171         [ 6, 3, "Come Be Depressed With Us", 1998 ],
172     ]);
173
174     $schema->populate('Tag', [
175         [ qw/tagid cd tag/ ],
176         [ 1, 1, "Blue" ],
177         [ 2, 2, "Blue" ],
178         [ 3, 3, "Blue" ],
179         [ 4, 5, "Blue" ],
180         [ 5, 2, "Cheesy" ],
181         [ 6, 4, "Cheesy" ],
182         [ 7, 5, "Cheesy" ],
183         [ 8, 2, "Shiny" ],
184         [ 9, 4, "Shiny" ],
185     ]);
186
187     $schema->populate('Producer', [
188         [ qw/producerid name/ ],
189         [ 1, 'Matt S Trout' ],
190         [ 2, 'Bob The Builder' ],
191         [ 3, 'Fred The Phenotype' ],
192     ]);
193
194     $schema->populate('CD_to_Producer', [
195         [ qw/cd producer/ ],
196         [ 1, 1 ],
197         [ 1, 2 ],
198         [ 1, 3 ],
199         [ 2, 1 ],
200         [ 2, 2 ],
201         [ 3, 3 ],
202     ]);
203
204     $schema->populate('Track', [
205         [ qw/trackid cd  position title last_updated_on/ ],
206         [ 4, 2, 1, "Stung with Success"],
207         [ 5, 2, 2, "Stripy"],
208         [ 6, 2, 3, "Sticky Honey"],
209         [ 7, 3, 1, "Yowlin"],
210         [ 8, 3, 2, "Howlin"],
211         [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'],
212         [ 10, 4, 1, "Boring Name"],
213         [ 11, 4, 2, "Boring Song"],
214         [ 12, 4, 3, "No More Ideas"],
215         [ 13, 5, 1, "Sad"],
216         [ 14, 5, 2, "Under The Weather"],
217         [ 15, 5, 3, "Suicidal"],
218         [ 16, 1, 1, "The Bees Knees"],
219         [ 17, 1, 2, "Apiary"],
220         [ 18, 1, 3, "Beehind You"],
221     ]);
222 }
223
224 1;