c3059cea648f1e2d95601696326b49ffcd15a1ae
[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     my $db_file = "t/var/DBIxClass.db";
49
50     unlink($db_file) if -e $db_file;
51     unlink($db_file . "-journal") if -e $db_file . "-journal";
52     mkdir("t/var") unless -d "t/var";
53
54     my $dsn = $ENV{"FIXTURETEST_DSN"} || "dbi:SQLite:${db_file}";
55     my $dbuser = $ENV{"FIXTURETEST_DBUSER"} || '';
56     my $dbpass = $ENV{"FIXTURETEST_DBPASS"} || '';
57
58     my $schema;
59
60     my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
61
62     if ($args{compose_connection}) {
63       $schema = DBICTest::Schema->compose_connection(
64                   'DBICTest', @connect_info
65                 );
66     } else {
67       $schema = DBICTest::Schema->compose_namespace('DBICTest')
68                                 ->connect(@connect_info);
69     }
70     $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
71     if ( !$args{no_deploy} ) {
72         __PACKAGE__->deploy_schema( $schema );
73         __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
74     }
75     return $schema;
76 }
77
78 =head2 deploy_schema
79
80   DBICTest->deploy_schema( $schema );
81
82 =cut
83
84 sub deploy_schema {
85     my $self = shift;
86     my $schema = shift;
87
88     open IN, "t/lib/sqlite.sql";
89     my $sql;
90     { local $/ = undef; $sql = <IN>; }
91     close IN;
92     ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
93 }
94  
95
96 =head2 clear_schema
97
98   DBICTest->clear_schema( $schema );
99
100 =cut
101
102 sub clear_schema {
103     my $self = shift;
104     my $schema = shift;
105
106     foreach my $class ($schema->sources) {
107       $schema->resultset($class)->delete;
108     }
109 }
110
111
112 =head2 populate_schema
113
114   DBICTest->populate_schema( $schema );
115
116 After you deploy your schema you can use this method to populate 
117 the tables with test data.
118
119 =cut
120
121 sub populate_schema {
122     my $self = shift;
123     my $schema = shift;
124
125     $schema->populate('Artist', [
126         [ qw/artistid name/ ],
127         [ 1, 'Caterwauler McCrae' ],
128         [ 2, 'Random Boy Band' ],
129         [ 3, 'We Are Goth' ],
130     ]);
131
132     $schema->populate('CD', [
133         [ qw/cdid artist title year/ ],
134         [ 1, 1, "Spoonful of bees", 1999 ],
135         [ 2, 1, "Forkful of bees", 2001 ],
136         [ 3, 1, "Caterwaulin' Blues", 1997 ],
137         [ 4, 2, "Generic Manufactured Singles", 2001 ],
138         [ 5, 2, "We like girls and stuff", 2003 ],
139         [ 6, 3, "Come Be Depressed With Us", 1998 ],
140     ]);
141
142     $schema->populate('Tag', [
143         [ qw/tagid cd tag/ ],
144         [ 1, 1, "Blue" ],
145         [ 2, 2, "Blue" ],
146         [ 3, 3, "Blue" ],
147         [ 4, 5, "Blue" ],
148         [ 5, 2, "Cheesy" ],
149         [ 6, 4, "Cheesy" ],
150         [ 7, 5, "Cheesy" ],
151         [ 8, 2, "Shiny" ],
152         [ 9, 4, "Shiny" ],
153     ]);
154
155     $schema->populate('Producer', [
156         [ qw/producerid name/ ],
157         [ 1, 'Matt S Trout' ],
158         [ 2, 'Bob The Builder' ],
159         [ 3, 'Fred The Phenotype' ],
160     ]);
161
162     $schema->populate('CD_to_Producer', [
163         [ qw/cd producer/ ],
164         [ 1, 1 ],
165         [ 1, 2 ],
166         [ 1, 3 ],
167     ]);
168
169     $schema->populate('Track', [
170         [ qw/trackid cd  position title/ ],
171         [ 4, 2, 1, "Stung with Success"],
172         [ 5, 2, 2, "Stripy"],
173         [ 6, 2, 3, "Sticky Honey"],
174         [ 7, 3, 1, "Yowlin"],
175         [ 8, 3, 2, "Howlin"],
176         [ 9, 3, 3, "Fowlin"],
177         [ 10, 4, 1, "Boring Name"],
178         [ 11, 4, 2, "Boring Song"],
179         [ 12, 4, 3, "No More Ideas"],
180         [ 13, 5, 1, "Sad"],
181         [ 14, 5, 2, "Under The Weather"],
182         [ 15, 5, 3, "Suicidal"],
183         [ 16, 1, 1, "The Bees Knees"],
184         [ 17, 1, 2, "Apiary"],
185         [ 18, 1, 3, "Beehind You"],
186     ]);
187 }
188
189 1;