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