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