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