added test for relative date support
[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     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 = DBICTest::Schema->compose_connection(
65                   'DBICTest', @connect_info
66                 );
67     } else {
68       $schema = DBICTest::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
98     my $file = $self->get_ddl_file($schema);
99     open IN, $file;
100     my $sql;
101     { local $/ = undef; $sql = <IN>; }
102     close IN;
103     ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
104 }
105  
106
107 =head2 clear_schema
108
109   DBICTest->clear_schema( $schema );
110
111 =cut
112
113 sub clear_schema {
114     my $self = shift;
115     my $schema = shift;
116
117     foreach my $class ($schema->sources) {
118       $schema->resultset($class)->delete;
119     }
120 }
121
122
123 =head2 populate_schema
124
125   DBICTest->populate_schema( $schema );
126
127 After you deploy your schema you can use this method to populate 
128 the tables with test data.
129
130 =cut
131
132 sub populate_schema {
133     my $self = shift;
134     my $schema = shift;
135
136     $schema->populate('Artist', [
137         [ qw/artistid name/ ],
138         [ 1, 'Caterwauler McCrae' ],
139         [ 2, 'Random Boy Band' ],
140         [ 3, 'We Are Goth' ],
141     ]);
142
143     $schema->populate('CD', [
144         [ qw/cdid artist title year/ ],
145         [ 1, 1, "Spoonful of bees", 1999 ],
146         [ 2, 1, "Forkful of bees", 2001 ],
147         [ 3, 1, "Caterwaulin' Blues", 1997 ],
148         [ 4, 2, "Generic Manufactured Singles", 2001 ],
149         [ 5, 2, "We like girls and stuff", 2003 ],
150         [ 6, 3, "Come Be Depressed With Us", 1998 ],
151     ]);
152
153     $schema->populate('Tag', [
154         [ qw/tagid cd tag/ ],
155         [ 1, 1, "Blue" ],
156         [ 2, 2, "Blue" ],
157         [ 3, 3, "Blue" ],
158         [ 4, 5, "Blue" ],
159         [ 5, 2, "Cheesy" ],
160         [ 6, 4, "Cheesy" ],
161         [ 7, 5, "Cheesy" ],
162         [ 8, 2, "Shiny" ],
163         [ 9, 4, "Shiny" ],
164     ]);
165
166     $schema->populate('Producer', [
167         [ qw/producerid name/ ],
168         [ 1, 'Matt S Trout' ],
169         [ 2, 'Bob The Builder' ],
170         [ 3, 'Fred The Phenotype' ],
171     ]);
172
173     $schema->populate('CD_to_Producer', [
174         [ qw/cd producer/ ],
175         [ 1, 1 ],
176         [ 1, 2 ],
177         [ 1, 3 ],
178     ]);
179
180     $schema->populate('Track', [
181         [ qw/trackid cd  position title last_updated_on/ ],
182         [ 4, 2, 1, "Stung with Success"],
183         [ 5, 2, 2, "Stripy"],
184         [ 6, 2, 3, "Sticky Honey"],
185         [ 7, 3, 1, "Yowlin"],
186         [ 8, 3, 2, "Howlin"],
187         [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'],
188         [ 10, 4, 1, "Boring Name"],
189         [ 11, 4, 2, "Boring Song"],
190         [ 12, 4, 3, "No More Ideas"],
191         [ 13, 5, 1, "Sad"],
192         [ 14, 5, 2, "Under The Weather"],
193         [ 15, 5, 3, "Suicidal"],
194         [ 16, 1, 1, "The Bees Knees"],
195         [ 17, 1, 2, "Apiary"],
196         [ 18, 1, 3, "Beehind You"],
197     ]);
198 }
199
200 1;