Commit | Line | Data |
c6d74d3e |
1 | package # hide from PAUSE |
2 | DBICTest; |
1c339d71 |
3 | |
4 | use strict; |
5 | use warnings; |
6 | use DBICTest::Schema; |
17e7d7f0 |
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, |
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 | |
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 |
1c339d71 |
48 | |
857d66ca |
49 | sub has_custom_dsn { |
50 | return $ENV{"DBICTEST_DSN"} ? 1:0; |
51 | } |
52 | |
53 | sub _sqlite_dbfilename { |
bcb3e850 |
54 | return "t/var/DBIxClass.db"; |
55 | } |
56 | |
57 | sub _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 |
64 | sub _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 | |
82 | sub 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 | |
115 | This method does one of two things to the schema. It can either call |
116 | the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment |
117 | variable is set, otherwise the default is to read in the t/lib/sqlite.sql |
118 | file and execute the SQL within. Either way you end up with a fresh set |
119 | of tables for testing. |
120 | |
121 | =cut |
122 | |
123 | sub 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; |
ebf846e8 |
135 | for my $chunk ( split (/;\s*\n+/, $sql) ) { |
136 | if ( $chunk =~ / ^ (?! --\s* ) \S /xm ) { # there is some real sql in the chunk - a non-space at the start of the string which is not a comment |
137 | $schema->storage->dbh->do($chunk) or print "Error on SQL: $chunk\n"; |
138 | } |
139 | } |
17e7d7f0 |
140 | } |
89cf6a70 |
141 | return; |
17e7d7f0 |
142 | } |
143 | |
144 | =head2 populate_schema |
145 | |
146 | DBICTest->populate_schema( $schema ); |
147 | |
148 | After you deploy your schema you can use this method to populate |
149 | the tables with test data. |
150 | |
151 | =cut |
152 | |
153 | sub populate_schema { |
154 | my $self = shift; |
155 | my $schema = shift; |
156 | |
17e7d7f0 |
157 | $schema->populate('Artist', [ |
77211009 |
158 | [ qw/artistid name/ ], |
159 | [ 1, 'Caterwauler McCrae' ], |
160 | [ 2, 'Random Boy Band' ], |
161 | [ 3, 'We Are Goth' ], |
17e7d7f0 |
162 | ]); |
163 | |
164 | $schema->populate('CD', [ |
165 | [ qw/cdid artist title year/ ], |
166 | [ 1, 1, "Spoonful of bees", 1999 ], |
167 | [ 2, 1, "Forkful of bees", 2001 ], |
168 | [ 3, 1, "Caterwaulin' Blues", 1997 ], |
169 | [ 4, 2, "Generic Manufactured Singles", 2001 ], |
170 | [ 5, 3, "Come Be Depressed With Us", 1998 ], |
171 | ]); |
172 | |
173 | $schema->populate('LinerNotes', [ |
174 | [ qw/liner_id notes/ ], |
175 | [ 2, "Buy Whiskey!" ], |
176 | [ 4, "Buy Merch!" ], |
177 | [ 5, "Kill Yourself!" ], |
178 | ]); |
179 | |
180 | $schema->populate('Tag', [ |
181 | [ qw/tagid cd tag/ ], |
182 | [ 1, 1, "Blue" ], |
183 | [ 2, 2, "Blue" ], |
184 | [ 3, 3, "Blue" ], |
185 | [ 4, 5, "Blue" ], |
186 | [ 5, 2, "Cheesy" ], |
187 | [ 6, 4, "Cheesy" ], |
188 | [ 7, 5, "Cheesy" ], |
189 | [ 8, 2, "Shiny" ], |
190 | [ 9, 4, "Shiny" ], |
191 | ]); |
192 | |
193 | $schema->populate('TwoKeys', [ |
194 | [ qw/artist cd/ ], |
195 | [ 1, 1 ], |
196 | [ 1, 2 ], |
197 | [ 2, 2 ], |
198 | ]); |
199 | |
200 | $schema->populate('FourKeys', [ |
3bd6e3e0 |
201 | [ qw/foo bar hello goodbye sensors/ ], |
202 | [ 1, 2, 3, 4, 'online' ], |
203 | [ 5, 4, 3, 6, 'offline' ], |
17e7d7f0 |
204 | ]); |
205 | |
206 | $schema->populate('OneKey', [ |
207 | [ qw/id artist cd/ ], |
208 | [ 1, 1, 1 ], |
209 | [ 2, 1, 2 ], |
210 | [ 3, 2, 2 ], |
211 | ]); |
212 | |
213 | $schema->populate('SelfRef', [ |
214 | [ qw/id name/ ], |
215 | [ 1, 'First' ], |
216 | [ 2, 'Second' ], |
217 | ]); |
218 | |
219 | $schema->populate('SelfRefAlias', [ |
220 | [ qw/self_ref alias/ ], |
221 | [ 1, 2 ] |
222 | ]); |
223 | |
224 | $schema->populate('ArtistUndirectedMap', [ |
225 | [ qw/id1 id2/ ], |
226 | [ 1, 2 ] |
227 | ]); |
228 | |
229 | $schema->populate('Producer', [ |
230 | [ qw/producerid name/ ], |
231 | [ 1, 'Matt S Trout' ], |
232 | [ 2, 'Bob The Builder' ], |
233 | [ 3, 'Fred The Phenotype' ], |
234 | ]); |
235 | |
236 | $schema->populate('CD_to_Producer', [ |
237 | [ qw/cd producer/ ], |
238 | [ 1, 1 ], |
239 | [ 1, 2 ], |
240 | [ 1, 3 ], |
241 | ]); |
8871d4ad |
242 | |
243 | $schema->populate('TreeLike', [ |
61177e44 |
244 | [ qw/id parent name/ ], |
8871d4ad |
245 | [ 1, undef, 'root' ], |
246 | [ 2, 1, 'foo' ], |
247 | [ 3, 2, 'bar' ], |
248 | [ 6, 2, 'blop' ], |
249 | [ 4, 3, 'baz' ], |
250 | [ 5, 4, 'quux' ], |
251 | [ 7, 3, 'fong' ], |
252 | ]); |
4b8dcc58 |
253 | |
17e7d7f0 |
254 | $schema->populate('Track', [ |
255 | [ qw/trackid cd position title/ ], |
256 | [ 4, 2, 1, "Stung with Success"], |
257 | [ 5, 2, 2, "Stripy"], |
258 | [ 6, 2, 3, "Sticky Honey"], |
259 | [ 7, 3, 1, "Yowlin"], |
260 | [ 8, 3, 2, "Howlin"], |
261 | [ 9, 3, 3, "Fowlin"], |
262 | [ 10, 4, 1, "Boring Name"], |
263 | [ 11, 4, 2, "Boring Song"], |
264 | [ 12, 4, 3, "No More Ideas"], |
265 | [ 13, 5, 1, "Sad"], |
266 | [ 14, 5, 2, "Under The Weather"], |
267 | [ 15, 5, 3, "Suicidal"], |
268 | [ 16, 1, 1, "The Bees Knees"], |
269 | [ 17, 1, 2, "Apiary"], |
270 | [ 18, 1, 3, "Beehind You"], |
271 | ]); |
4b8dcc58 |
272 | |
ae515736 |
273 | $schema->populate('Event', [ |
ff8a6e3b |
274 | [ qw/id starts_at created_on varchar_date varchar_datetime skip_inflation/ ], |
275 | [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05', '2006-07-23', '2006-05-22 19:05:07', '2006-04-21 18:04:06'], |
ae515736 |
276 | ]); |
277 | |
17e7d7f0 |
278 | $schema->populate('Link', [ |
54e0bd06 |
279 | [ qw/id url title/ ], |
280 | [ 1, '', 'aaa' ] |
17e7d7f0 |
281 | ]); |
e673f011 |
282 | |
17e7d7f0 |
283 | $schema->populate('Bookmark', [ |
284 | [ qw/id link/ ], |
285 | [ 1, 1 ] |
286 | ]); |
78060df8 |
287 | |
288 | $schema->populate('Collection', [ |
289 | [ qw/collectionid name/ ], |
290 | [ 1, "Tools" ], |
291 | [ 2, "Body Parts" ], |
292 | ]); |
89cf6a70 |
293 | |
78060df8 |
294 | $schema->populate('TypedObject', [ |
295 | [ qw/objectid type value/ ], |
296 | [ 1, "pointy", "Awl" ], |
297 | [ 2, "round", "Bearing" ], |
298 | [ 3, "pointy", "Knife" ], |
299 | [ 4, "pointy", "Tooth" ], |
300 | [ 5, "round", "Head" ], |
301 | ]); |
89cf6a70 |
302 | $schema->populate('CollectionObject', [ |
303 | [ qw/collection object/ ], |
304 | [ 1, 1 ], |
305 | [ 1, 2 ], |
306 | [ 1, 3 ], |
307 | [ 2, 4 ], |
308 | [ 2, 5 ], |
309 | ]); |
78060df8 |
310 | |
311 | $schema->populate('Owners', [ |
312 | [ qw/ownerid name/ ], |
313 | [ 1, "Newton" ], |
314 | [ 2, "Waltham" ], |
315 | ]); |
316 | |
317 | $schema->populate('BooksInLibrary', [ |
cda5e082 |
318 | [ qw/id owner title source price/ ], |
319 | [ 1, 1, "Programming Perl", "Library", 23 ], |
320 | [ 2, 1, "Dynamical Systems", "Library", 37 ], |
321 | [ 3, 2, "Best Recipe Cookbook", "Library", 65 ], |
78060df8 |
322 | ]); |
1c339d71 |
323 | } |
4b8dcc58 |
324 | |
510ca912 |
325 | 1; |