Passes tests again!
[dbsrgits/DBIx-Class-Journal.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     my $db_file = "t/var/DBIxClass.db";
49     my $db_audit = 't/var/Audit.db';
50
51     unlink($db_file) if -e $db_file;
52     unlink($db_file . "-journal") if -e $db_file . "-journal";
53     unlink($db_audit) if -e $db_audit;
54     unlink($db_audit . "-journal") if -e $db_audit . "-journal";
55     mkdir("t/var") unless -d "t/var";
56
57     my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
58     my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
59     my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
60
61     my $schema;
62     my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
63
64     if ($args{compose_connection}) {
65       $schema = DBICTest::Schema->compose_connection(
66                   'DBICTest', @connect_info
67                 );
68     } else {
69       $schema = DBICTest::Schema->compose_namespace('DBICTest')
70                                 ->connect(@connect_info);
71     print STDERR "Created Schema: ", ref($schema), "\n";
72     }
73 #    my $schema = DBICTest::Schema->compose_connection('DBICTest' => $dsn, $dbuser, $dbpass);
74 #    $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
75     if ( !$args{no_deploy} ) {
76         __PACKAGE__->deploy_schema( $schema );
77         __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
78     }
79     return $schema;
80 }
81
82 =head2 deploy_schema
83
84   DBICTest->deploy_schema( $schema );
85
86 This method does one of two things to the schema.  It can either call 
87 the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment 
88 variable is set, otherwise the default is to read in the t/lib/sqlite.sql 
89 file and execute the SQL within. Either way you end up with a fresh set 
90 of tables for testing.
91
92 =cut
93
94 sub deploy_schema {
95     my $self = shift;
96     my $schema = shift;
97
98 #    if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
99         return $schema->deploy();
100 #     } else {
101 #         open IN, "t/lib/sqlite.sql";
102 #         my $sql;
103 #         { local $/ = undef; $sql = <IN>; }
104 #         close IN;
105 #         ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
106 #     }
107 }
108
109 =head2 populate_schema
110
111   DBICTest->populate_schema( $schema );
112
113 After you deploy your schema you can use this method to populate 
114 the tables with test data.
115
116 =cut
117
118 sub populate_schema {
119     my $self = shift;
120     my $schema = shift;
121
122     $schema->populate('Artist', [
123         [ qw/artistid name/ ],
124         [ 1, 'Caterwauler McCrae' ],
125         [ 2, 'Random Boy Band' ],
126         [ 3, 'We Are Goth' ],
127     ]);
128
129     $schema->populate('CD', [
130         [ qw/cdid artist title year/ ],
131         [ 1, 1, "Spoonful of bees", 1999 ],
132         [ 2, 1, "Forkful of bees", 2001 ],
133         [ 3, 1, "Caterwaulin' Blues", 1997 ],
134         [ 4, 2, "Generic Manufactured Singles", 2001 ],
135         [ 5, 3, "Come Be Depressed With Us", 1998 ],
136     ]);
137
138     $schema->populate('LinerNotes', [
139         [ qw/liner_id notes/ ],
140         [ 2, "Buy Whiskey!" ],
141         [ 4, "Buy Merch!" ],
142         [ 5, "Kill Yourself!" ],
143     ]);
144
145     $schema->populate('Tag', [
146         [ qw/tagid cd tag/ ],
147         [ 1, 1, "Blue" ],
148         [ 2, 2, "Blue" ],
149         [ 3, 3, "Blue" ],
150         [ 4, 5, "Blue" ],
151         [ 5, 2, "Cheesy" ],
152         [ 6, 4, "Cheesy" ],
153         [ 7, 5, "Cheesy" ],
154         [ 8, 2, "Shiny" ],
155         [ 9, 4, "Shiny" ],
156     ]);
157
158     $schema->populate('TwoKeys', [
159         [ qw/artist cd/ ],
160         [ 1, 1 ],
161         [ 1, 2 ],
162         [ 2, 2 ],
163     ]);
164
165     $schema->populate('FourKeys', [
166         [ qw/foo bar hello goodbye sensors/ ],
167         [ 1, 2, 3, 4, 'online' ],
168         [ 5, 4, 3, 6, 'offline' ],
169     ]);
170
171     $schema->populate('OneKey', [
172         [ qw/id artist cd/ ],
173         [ 1, 1, 1 ],
174         [ 2, 1, 2 ],
175         [ 3, 2, 2 ],
176     ]);
177
178     $schema->populate('SelfRef', [
179         [ qw/id name/ ],
180         [ 1, 'First' ],
181         [ 2, 'Second' ],
182     ]);
183
184     $schema->populate('SelfRefAlias', [
185         [ qw/self_ref alias/ ],
186         [ 1, 2 ]
187     ]);
188
189     $schema->populate('ArtistUndirectedMap', [
190         [ qw/id1 id2/ ],
191         [ 1, 2 ]
192     ]);
193
194     $schema->populate('Producer', [
195         [ qw/producerid name/ ],
196         [ 1, 'Matt S Trout' ],
197         [ 2, 'Bob The Builder' ],
198         [ 3, 'Fred The Phenotype' ],
199     ]);
200
201     $schema->populate('CD_to_Producer', [
202         [ qw/cd producer/ ],
203         [ 1, 1 ],
204         [ 1, 2 ],
205         [ 1, 3 ],
206     ]);
207
208     $schema->populate('TreeLike', [
209         [ qw/id parent name/ ],
210         [ 1, 0, 'foo'  ],
211         [ 2, 1, 'bar'  ],
212         [ 5, 1, 'blop' ],
213         [ 3, 2, 'baz'  ],
214         [ 4, 3, 'quux' ],
215         [ 6, 2, 'fong'  ],
216     ]);
217
218     $schema->populate('Track', [
219         [ qw/trackid cd  position title/ ],
220         [ 4, 2, 1, "Stung with Success"],
221         [ 5, 2, 2, "Stripy"],
222         [ 6, 2, 3, "Sticky Honey"],
223         [ 7, 3, 1, "Yowlin"],
224         [ 8, 3, 2, "Howlin"],
225         [ 9, 3, 3, "Fowlin"],
226         [ 10, 4, 1, "Boring Name"],
227         [ 11, 4, 2, "Boring Song"],
228         [ 12, 4, 3, "No More Ideas"],
229         [ 13, 5, 1, "Sad"],
230         [ 14, 5, 2, "Under The Weather"],
231         [ 15, 5, 3, "Suicidal"],
232         [ 16, 1, 1, "The Bees Knees"],
233         [ 17, 1, 2, "Apiary"],
234         [ 18, 1, 3, "Beehind You"],
235     ]);
236
237     $schema->populate('Event', [
238         [ qw/id starts_at created_on/ ],
239         [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
240     ]);
241
242     $schema->populate('Link', [
243         [ qw/id title/ ],
244         [ 1, 'aaa' ]
245     ]);
246
247     $schema->populate('Bookmark', [
248         [ qw/id link/ ],
249         [ 1, 1 ]
250     ]);
251
252     $schema->populate('Collection', [
253         [ qw/collectionid name/ ],
254         [ 1, "Tools" ],
255         [ 2, "Body Parts" ],
256     ]);
257
258     $schema->populate('CollectionObject', [
259         [ qw/collection object/ ],
260         [ 1, 1 ],
261         [ 1, 2 ],
262         [ 1, 3 ],
263         [ 2, 4 ],
264         [ 2, 5 ],
265     ]);
266
267     $schema->populate('TypedObject', [
268         [ qw/objectid type value/ ],
269         [ 1, "pointy", "Awl" ],
270         [ 2, "round", "Bearing" ],
271         [ 3, "pointy", "Knife" ],
272         [ 4, "pointy", "Tooth" ],
273         [ 5, "round", "Head" ],
274     ]);
275
276     $schema->populate('Owners', [
277         [ qw/ownerid name/ ],
278         [ 1, "Newton" ],
279         [ 2, "Waltham" ],
280     ]);
281
282     $schema->populate('BooksInLibrary', [
283         [ qw/id owner title source/ ],
284         [ 1, 1, "Programming Perl", "Library" ],
285         [ 2, 1, "Dynamical Systems", "Library" ],
286         [ 3, 2, "Best Recipe Cookbook", "Library" ],
287     ]);
288 }
289
290 1;