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