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