Commit | Line | Data |
406086f3 |
1 | package # hide from PAUSE |
d2739840 |
2 | DBICTest; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | use RestTest::Schema; |
7 | |
8 | =head1 NAME |
9 | |
10 | DBICTest - 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; |
406086f3 |
17 | |
d2739840 |
18 | my $schema = DBICTest->init_schema(); |
19 | |
20 | =head1 DESCRIPTION |
21 | |
406086f3 |
22 | This module provides the basic utilities to write tests against |
d2739840 |
23 | DBIx::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 | |
406086f3 |
34 | This method removes the test SQLite database in t/var/DBIxClass.db |
d2739840 |
35 | and then creates a new, empty database. |
36 | |
406086f3 |
37 | This method will call deploy_schema() by default, unless the |
d2739840 |
38 | no_deploy flag is set. |
39 | |
406086f3 |
40 | Also, by default, this method will call populate_schema() by |
d2739840 |
41 | default, unless the no_deploy or no_populate flags are set. |
42 | |
43 | =cut |
44 | |
45 | sub init_schema { |
46 | my $self = shift; |
47 | my %args = @_; |
48 | |
49 | my $db_file = "t/var/DBIxClass.db"; |
50 | |
51 | unlink($db_file) if -e $db_file; |
52 | unlink($db_file . "-journal") if -e $db_file . "-journal"; |
53 | mkdir("t/var") unless -d "t/var"; |
54 | |
55 | my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}"; |
56 | my $dbuser = $args{"user"} || ''; |
57 | my $dbpass = $args{"pass"} || ''; |
58 | |
59 | my $schema; |
60 | |
61 | my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 }); |
62 | |
63 | if ($args{compose_connection}) { |
64 | $schema = RestTest::Schema->compose_connection( |
65 | 'DBICTest', @connect_info |
66 | ); |
67 | } else { |
68 | $schema = RestTest::Schema->compose_namespace('DBICTest') |
69 | ->connect(@connect_info); |
70 | } |
71 | |
72 | if ( !$args{no_deploy} ) { |
73 | __PACKAGE__->deploy_schema( $schema ); |
74 | __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} ); |
75 | } |
76 | return $schema; |
77 | } |
78 | |
79 | |
80 | sub get_ddl_file { |
81 | my $self = shift; |
82 | my $schema = shift; |
83 | |
84 | return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql'; |
85 | } |
86 | |
87 | =head2 deploy_schema |
88 | |
89 | DBICTest->deploy_schema( $schema ); |
90 | |
91 | =cut |
92 | |
93 | sub deploy_schema { |
94 | my $self = shift; |
95 | my $schema = shift; |
96 | |
97 | my $file = shift || $self->get_ddl_file($schema); |
98 | open IN, $file; |
99 | my $sql; |
100 | { local $/ = undef; $sql = <IN>; } |
101 | close IN; |
102 | ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql); |
103 | } |
406086f3 |
104 | |
d2739840 |
105 | |
106 | =head2 clear_schema |
107 | |
108 | DBICTest->clear_schema( $schema ); |
109 | |
110 | =cut |
111 | |
112 | sub clear_schema { |
113 | my $self = shift; |
114 | my $schema = shift; |
115 | |
116 | foreach my $class ($schema->sources) { |
117 | $schema->resultset($class)->delete; |
118 | } |
119 | } |
120 | |
121 | |
122 | =head2 populate_schema |
123 | |
124 | DBICTest->populate_schema( $schema ); |
125 | |
406086f3 |
126 | After you deploy your schema you can use this method to populate |
d2739840 |
127 | the tables with test data. |
128 | |
129 | =cut |
130 | |
131 | sub populate_schema { |
132 | my $self = shift; |
133 | my $schema = shift; |
134 | |
135 | $schema->populate('Artist', [ |
136 | [ qw/artistid name/ ], |
137 | [ 1, 'Caterwauler McCrae' ], |
138 | [ 2, 'Random Boy Band' ], |
139 | [ 3, 'We Are Goth' ], |
140 | ]); |
141 | |
142 | $schema->populate('CD', [ |
143 | [ qw/cdid artist title year/ ], |
144 | [ 1, 1, "Spoonful of bees", 1999 ], |
145 | [ 2, 1, "Forkful of bees", 2001 ], |
146 | [ 3, 1, "Caterwaulin' Blues", 1997 ], |
147 | [ 4, 2, "Generic Manufactured Singles", 2001 ], |
148 | [ 5, 2, "We like girls and stuff", 2003 ], |
149 | [ 6, 3, "Come Be Depressed With Us", 1998 ], |
150 | ]); |
151 | |
152 | $schema->populate('Tag', [ |
153 | [ qw/tagid cd tag/ ], |
154 | [ 1, 1, "Blue" ], |
155 | [ 2, 2, "Blue" ], |
156 | [ 3, 3, "Blue" ], |
157 | [ 4, 5, "Blue" ], |
158 | [ 5, 2, "Cheesy" ], |
159 | [ 6, 4, "Cheesy" ], |
160 | [ 7, 5, "Cheesy" ], |
161 | [ 8, 2, "Shiny" ], |
162 | [ 9, 4, "Shiny" ], |
163 | ]); |
164 | |
165 | $schema->populate('Producer', [ |
166 | [ qw/producerid name/ ], |
167 | [ 1, 'Matt S Trout' ], |
168 | [ 2, 'Bob The Builder' ], |
169 | [ 3, 'Fred The Phenotype' ], |
170 | ]); |
171 | |
172 | $schema->populate('CD_to_Producer', [ |
173 | [ qw/cd producer/ ], |
174 | [ 1, 1 ], |
175 | [ 3, 2 ], |
176 | [ 2, 3 ], |
177 | ]); |
178 | |
179 | $schema->populate('Track', [ |
180 | [ qw/trackid cd position title last_updated_on/ ], |
181 | [ 4, 2, 1, "Stung with Success"], |
182 | [ 5, 2, 2, "Stripy"], |
183 | [ 6, 2, 3, "Sticky Honey"], |
184 | [ 7, 3, 1, "Yowlin"], |
185 | [ 8, 3, 2, "Howlin"], |
186 | [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'], |
187 | [ 10, 4, 1, "Boring Name"], |
188 | [ 11, 4, 2, "Boring Song"], |
189 | [ 12, 4, 3, "No More Ideas"], |
190 | [ 13, 5, 1, "Sad"], |
191 | [ 14, 5, 2, "Under The Weather"], |
192 | [ 15, 5, 3, "Suicidal"], |
193 | [ 16, 1, 1, "The Bees Knees"], |
194 | [ 17, 1, 2, "Apiary"], |
195 | [ 18, 1, 3, "Beehind You"], |
196 | ]); |
197 | } |
198 | |
199 | 1; |