Commit | Line | Data |
c1a04675 |
1 | package # hide from PAUSE |
2 | DBICTest; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | use DBICTest::Schema; |
7 | |
6367db27 |
8 | use utf8; |
9 | |
c1a04675 |
10 | =head1 NAME |
11 | |
12 | DBICTest - Library to be used by DBIx::Class test scripts. |
13 | |
14 | =head1 SYNOPSIS |
15 | |
16 | use lib qw(t/lib); |
17 | use DBICTest; |
18 | use Test::More; |
19 | |
20 | my $schema = DBICTest->init_schema(); |
21 | |
22 | =head1 DESCRIPTION |
23 | |
24 | This module provides the basic utilities to write tests against |
25 | DBIx::Class. |
26 | |
27 | =head1 METHODS |
28 | |
29 | =head2 init_schema |
30 | |
31 | my $schema = DBICTest->init_schema( |
32 | no_deploy=>1, |
33 | no_populate=>1, |
34 | ); |
35 | |
36 | This method removes the test SQLite database in t/var/DBIxClass.db |
37 | and then creates a new, empty database. |
38 | |
39 | This method will call deploy_schema() by default, unless the |
40 | no_deploy flag is set. |
41 | |
42 | Also, by default, this method will call populate_schema() by |
43 | default, unless the no_deploy or no_populate flags are set. |
44 | |
45 | =cut |
46 | |
47 | sub init_schema { |
48 | my $self = shift; |
49 | my %args = @_; |
0566a82d |
50 | |
dc44796e |
51 | my $db_file |
52 | = $args{db_dir} |
53 | ? "$args{db_dir}/DBIxClass.db" |
54 | : "t/var/DBIxClass.db" |
55 | ; |
c1a04675 |
56 | |
c1a04675 |
57 | mkdir("t/var") unless -d "t/var"; |
8a1df391 |
58 | if ( !$args{no_deploy} ) { |
59 | unlink($db_file) if -e $db_file; |
60 | unlink($db_file . "-journal") if -e $db_file . "-journal"; |
61 | } |
c1a04675 |
62 | |
0566a82d |
63 | my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}"; |
64 | my $dbuser = $args{"user"} || ''; |
65 | my $dbpass = $args{"pass"} || ''; |
c1a04675 |
66 | |
67 | my $schema; |
68 | |
6367db27 |
69 | my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1, sqlite_unicode => 1 }); |
c1a04675 |
70 | |
71 | if ($args{compose_connection}) { |
72 | $schema = DBICTest::Schema->compose_connection( |
73 | 'DBICTest', @connect_info |
74 | ); |
75 | } else { |
76 | $schema = DBICTest::Schema->compose_namespace('DBICTest') |
77 | ->connect(@connect_info); |
78 | } |
0566a82d |
79 | |
c1a04675 |
80 | if ( !$args{no_deploy} ) { |
81 | __PACKAGE__->deploy_schema( $schema ); |
82 | __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} ); |
83 | } |
84 | return $schema; |
85 | } |
86 | |
0566a82d |
87 | |
88 | sub get_ddl_file { |
89 | my $self = shift; |
90 | my $schema = shift; |
91 | |
92 | return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql'; |
93 | } |
94 | |
c1a04675 |
95 | =head2 deploy_schema |
96 | |
97 | DBICTest->deploy_schema( $schema ); |
98 | |
99 | =cut |
100 | |
101 | sub deploy_schema { |
102 | my $self = shift; |
103 | my $schema = shift; |
104 | |
b099fee9 |
105 | my $file = shift || $self->get_ddl_file($schema); |
a9fe6f36 |
106 | open( my $fh, "<",$file ) or die "couldnt open $file, $!"; |
c1a04675 |
107 | my $sql; |
a9fe6f36 |
108 | { local $/ = undef; $sql = <$fh>; } |
885f16cb |
109 | |
110 | foreach my $line (split(/;\n/, $sql)) { |
111 | print "$line\n"; |
112 | next if(!$line); |
113 | next if($line =~ /^--/); |
114 | next if($line =~ /^BEGIN TRANSACTION/m); |
115 | next if($line =~ /^COMMIT/m); |
116 | next if $line =~ /^\s+$/; # skip whitespace only |
117 | |
118 | $schema->storage->dbh->do($line) || print "Error on SQL: $line\n"; |
119 | } |
c1a04675 |
120 | } |
3c528252 |
121 | |
122 | |
123 | =head2 clear_schema |
124 | |
125 | DBICTest->clear_schema( $schema ); |
126 | |
127 | =cut |
128 | |
129 | sub clear_schema { |
130 | my $self = shift; |
131 | my $schema = shift; |
132 | |
133 | foreach my $class ($schema->sources) { |
134 | $schema->resultset($class)->delete; |
135 | } |
136 | } |
137 | |
c1a04675 |
138 | |
139 | =head2 populate_schema |
140 | |
141 | DBICTest->populate_schema( $schema ); |
142 | |
143 | After you deploy your schema you can use this method to populate |
144 | the tables with test data. |
145 | |
146 | =cut |
147 | |
148 | sub populate_schema { |
149 | my $self = shift; |
150 | my $schema = shift; |
151 | |
152 | $schema->populate('Artist', [ |
153 | [ qw/artistid name/ ], |
154 | [ 1, 'Caterwauler McCrae' ], |
155 | [ 2, 'Random Boy Band' ], |
156 | [ 3, 'We Are Goth' ], |
017d2ab4 |
157 | [ 4, '' ], # Test overridden new will default name to "Test Name" using use_create => 1. |
ae7b4675 |
158 | [ 32948, 'Big PK' ], |
c1a04675 |
159 | ]); |
160 | |
c7aececc |
161 | $schema->populate('Artist::WashedUp', [ |
162 | [ qw/fk_artistid/ ], |
163 | [ 2 ], |
164 | ]); |
165 | |
c1a04675 |
166 | $schema->populate('CD', [ |
167 | [ qw/cdid artist title year/ ], |
168 | [ 1, 1, "Spoonful of bees", 1999 ], |
169 | [ 2, 1, "Forkful of bees", 2001 ], |
170 | [ 3, 1, "Caterwaulin' Blues", 1997 ], |
171 | [ 4, 2, "Generic Manufactured Singles", 2001 ], |
593b3c23 |
172 | [ 5, 2, "Unicode Chars ™ © • † ∑ α β « » → …", 2015 ], |
7e224e85 |
173 | [ 6, 3, "Übertreibung älterer Umlaute with us", 1998 ], |
c1a04675 |
174 | ]); |
175 | |
176 | $schema->populate('Tag', [ |
177 | [ qw/tagid cd tag/ ], |
178 | [ 1, 1, "Blue" ], |
179 | [ 2, 2, "Blue" ], |
180 | [ 3, 3, "Blue" ], |
181 | [ 4, 5, "Blue" ], |
182 | [ 5, 2, "Cheesy" ], |
183 | [ 6, 4, "Cheesy" ], |
184 | [ 7, 5, "Cheesy" ], |
185 | [ 8, 2, "Shiny" ], |
186 | [ 9, 4, "Shiny" ], |
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 ], |
610be089 |
201 | [ 2, 1 ], |
202 | [ 2, 2 ], |
203 | [ 3, 3 ], |
c1a04675 |
204 | ]); |
205 | |
206 | $schema->populate('Track', [ |
0566a82d |
207 | [ qw/trackid cd position title last_updated_on/ ], |
c1a04675 |
208 | [ 4, 2, 1, "Stung with Success"], |
209 | [ 5, 2, 2, "Stripy"], |
210 | [ 6, 2, 3, "Sticky Honey"], |
211 | [ 7, 3, 1, "Yowlin"], |
212 | [ 8, 3, 2, "Howlin"], |
0566a82d |
213 | [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'], |
c1a04675 |
214 | [ 10, 4, 1, "Boring Name"], |
215 | [ 11, 4, 2, "Boring Song"], |
216 | [ 12, 4, 3, "No More Ideas"], |
217 | [ 13, 5, 1, "Sad"], |
218 | [ 14, 5, 2, "Under The Weather"], |
219 | [ 15, 5, 3, "Suicidal"], |
220 | [ 16, 1, 1, "The Bees Knees"], |
221 | [ 17, 1, 2, "Apiary"], |
222 | [ 18, 1, 3, "Beehind You"], |
223 | ]); |
224 | } |
225 | |
226 | 1; |