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