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