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