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