bumb version and redue key size to solve reported errors in testing
[dbsrgits/DBIx-Class-Fixtures.git] / t / lib / DBICTest.pm
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 = @_;
48
49     my $db_file = "t/var/DBIxClass.db";
50
51     mkdir("t/var") unless -d "t/var";
52     if ( !$args{no_deploy} ) {
53       unlink($db_file) if -e $db_file;
54       unlink($db_file . "-journal") if -e $db_file . "-journal";
55     }
56
57     my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}";
58     my $dbuser = $args{"user"} || '';
59     my $dbpass = $args{"pass"} || '';
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     }
73
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
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
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
99     my $file = shift || $self->get_ddl_file($schema);
100     open IN, $file;
101     my $sql;
102     { local $/ = undef; $sql = <IN>; }
103     close IN;
104
105     foreach my $line (split(/;\n/, $sql)) {
106       print "$line\n";
107       next if(!$line);
108       next if($line =~ /^--/);
109       next if($line =~ /^BEGIN TRANSACTION/m);
110       next if($line =~ /^COMMIT/m);
111       next if $line =~ /^\s+$/; # skip whitespace only
112
113       $schema->storage->dbh->do($line) || print "Error on SQL: $line\n";
114     }
115 }
116  
117
118 =head2 clear_schema
119
120   DBICTest->clear_schema( $schema );
121
122 =cut
123
124 sub clear_schema {
125     my $self = shift;
126     my $schema = shift;
127
128     foreach my $class ($schema->sources) {
129       $schema->resultset($class)->delete;
130     }
131 }
132
133
134 =head2 populate_schema
135
136   DBICTest->populate_schema( $schema );
137
138 After you deploy your schema you can use this method to populate 
139 the tables with test data.
140
141 =cut
142
143 sub populate_schema {
144     my $self = shift;
145     my $schema = shift;
146
147     $schema->populate('Artist', [
148         [ qw/artistid name/ ],
149         [ 1, 'Caterwauler McCrae' ],
150         [ 2, 'Random Boy Band' ],
151         [ 3, 'We Are Goth' ],
152         [ 4, '' ], # Test overridden new will default name to "Test Name" using use_create => 1.
153         [ 32948, 'Big PK' ],
154     ]);
155
156     $schema->populate('CD', [
157         [ qw/cdid artist title year/ ],
158         [ 1, 1, "Spoonful of bees", 1999 ],
159         [ 2, 1, "Forkful of bees", 2001 ],
160         [ 3, 1, "Caterwaulin' Blues", 1997 ],
161         [ 4, 2, "Generic Manufactured Singles", 2001 ],
162         [ 5, 2, "We like girls and stuff", 2003 ],
163         [ 6, 3, "Come Be Depressed With Us", 1998 ],
164     ]);
165
166     $schema->populate('Tag', [
167         [ qw/tagid cd tag/ ],
168         [ 1, 1, "Blue" ],
169         [ 2, 2, "Blue" ],
170         [ 3, 3, "Blue" ],
171         [ 4, 5, "Blue" ],
172         [ 5, 2, "Cheesy" ],
173         [ 6, 4, "Cheesy" ],
174         [ 7, 5, "Cheesy" ],
175         [ 8, 2, "Shiny" ],
176         [ 9, 4, "Shiny" ],
177     ]);
178
179     $schema->populate('Producer', [
180         [ qw/producerid name/ ],
181         [ 1, 'Matt S Trout' ],
182         [ 2, 'Bob The Builder' ],
183         [ 3, 'Fred The Phenotype' ],
184     ]);
185
186     $schema->populate('CD_to_Producer', [
187         [ qw/cd producer/ ],
188         [ 1, 1 ],
189         [ 1, 2 ],
190         [ 1, 3 ],
191         [ 2, 1 ],
192         [ 2, 2 ],
193         [ 3, 3 ],
194     ]);
195
196     $schema->populate('Track', [
197         [ qw/trackid cd  position title last_updated_on/ ],
198         [ 4, 2, 1, "Stung with Success"],
199         [ 5, 2, 2, "Stripy"],
200         [ 6, 2, 3, "Sticky Honey"],
201         [ 7, 3, 1, "Yowlin"],
202         [ 8, 3, 2, "Howlin"],
203         [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'],
204         [ 10, 4, 1, "Boring Name"],
205         [ 11, 4, 2, "Boring Song"],
206         [ 12, 4, 3, "No More Ideas"],
207         [ 13, 5, 1, "Sad"],
208         [ 14, 5, 2, "Under The Weather"],
209         [ 15, 5, 3, "Suicidal"],
210         [ 16, 1, 1, "The Bees Knees"],
211         [ 17, 1, 2, "Apiary"],
212         [ 18, 1, 3, "Beehind You"],
213     ]);
214 }
215
216 1;