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