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