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