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