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