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