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