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