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