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