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