69d0c740e120b496a29a4057165eac177cf59dd6
[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 use utf8;
9
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 = @_;
50
51     my $db_file = "t/var/DBIxClass.db";
52
53     mkdir("t/var") unless -d "t/var";
54     if ( !$args{no_deploy} ) {
55       unlink($db_file) if -e $db_file;
56       unlink($db_file . "-journal") if -e $db_file . "-journal";
57     }
58
59     my $dsn = $args{"dsn"} || "dbi:SQLite:${db_file}";
60     my $dbuser = $args{"user"} || '';
61     my $dbpass = $args{"pass"} || '';
62
63     my $schema;
64
65     my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1, sqlite_unicode => 1 });
66
67     if ($args{compose_connection}) {
68       $schema = DBICTest::Schema->compose_connection(
69                   'DBICTest', @connect_info
70                 );
71     } else {
72       $schema = DBICTest::Schema->compose_namespace('DBICTest')
73                                 ->connect(@connect_info);
74     }
75
76     if ( !$args{no_deploy} ) {
77         __PACKAGE__->deploy_schema( $schema );
78         __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
79     }
80     return $schema;
81 }
82
83
84 sub get_ddl_file {
85   my $self = shift;
86   my $schema = shift;
87
88   return 't/lib/' . lc($schema->storage->dbh->{Driver}->{Name}) . '.sql';
89 }
90
91 =head2 deploy_schema
92
93   DBICTest->deploy_schema( $schema );
94
95 =cut
96
97 sub deploy_schema {
98     my $self = shift;
99     my $schema = shift;
100
101     my $file = shift || $self->get_ddl_file($schema);
102     open(  my $fh, "<",$file ) or die "couldnt open $file, $!";
103     my $sql;
104     { local $/ = undef; $sql = <$fh>; }
105
106     foreach my $line (split(/;\n/, $sql)) {
107       print "$line\n";
108       next if(!$line);
109       next if($line =~ /^--/);
110       next if($line =~ /^BEGIN TRANSACTION/m);
111       next if($line =~ /^COMMIT/m);
112       next if $line =~ /^\s+$/; # skip whitespace only
113
114       $schema->storage->dbh->do($line) || print "Error on SQL: $line\n";
115     }
116 }
117  
118
119 =head2 clear_schema
120
121   DBICTest->clear_schema( $schema );
122
123 =cut
124
125 sub clear_schema {
126     my $self = shift;
127     my $schema = shift;
128
129     foreach my $class ($schema->sources) {
130       $schema->resultset($class)->delete;
131     }
132 }
133
134
135 =head2 populate_schema
136
137   DBICTest->populate_schema( $schema );
138
139 After you deploy your schema you can use this method to populate 
140 the tables with test data.
141
142 =cut
143
144 sub populate_schema {
145     my $self = shift;
146     my $schema = shift;
147
148     $schema->populate('Artist', [
149         [ qw/artistid name/ ],
150         [ 1, 'Caterwauler McCrae' ],
151         [ 2, 'Random Boy Band' ],
152         [ 3, 'We Are Goth' ],
153         [ 4, '' ], # Test overridden new will default name to "Test Name" using use_create => 1.
154         [ 32948, 'Big PK' ],
155     ]);
156
157     $schema->populate('CD', [
158         [ qw/cdid artist title year/ ],
159         [ 1, 1, "Spoonful of bees", 1999 ],
160         [ 2, 1, "Forkful of bees", 2001 ],
161         [ 3, 1, "Caterwaulin' Blues", 1997 ],
162         [ 4, 2, "Generic Manufactured Singles", 2001 ],
163         [ 5, 2, "Unicode Chars ™ © • † ∑ α β « » → …", 2015 ],
164         [ 6, 3, "Come Be Depressed With Us", 1998 ],
165     ]);
166
167     $schema->populate('Tag', [
168         [ qw/tagid cd tag/ ],
169         [ 1, 1, "Blue" ],
170         [ 2, 2, "Blue" ],
171         [ 3, 3, "Blue" ],
172         [ 4, 5, "Blue" ],
173         [ 5, 2, "Cheesy" ],
174         [ 6, 4, "Cheesy" ],
175         [ 7, 5, "Cheesy" ],
176         [ 8, 2, "Shiny" ],
177         [ 9, 4, "Shiny" ],
178     ]);
179
180     $schema->populate('Producer', [
181         [ qw/producerid name/ ],
182         [ 1, 'Matt S Trout' ],
183         [ 2, 'Bob The Builder' ],
184         [ 3, 'Fred The Phenotype' ],
185     ]);
186
187     $schema->populate('CD_to_Producer', [
188         [ qw/cd producer/ ],
189         [ 1, 1 ],
190         [ 1, 2 ],
191         [ 1, 3 ],
192         [ 2, 1 ],
193         [ 2, 2 ],
194         [ 3, 3 ],
195     ]);
196
197     $schema->populate('Track', [
198         [ qw/trackid cd  position title last_updated_on/ ],
199         [ 4, 2, 1, "Stung with Success"],
200         [ 5, 2, 2, "Stripy"],
201         [ 6, 2, 3, "Sticky Honey"],
202         [ 7, 3, 1, "Yowlin"],
203         [ 8, 3, 2, "Howlin"],
204         [ 9, 3, 3, "Fowlin", '2007-10-20 00:00:00'],
205         [ 10, 4, 1, "Boring Name"],
206         [ 11, 4, 2, "Boring Song"],
207         [ 12, 4, 3, "No More Ideas"],
208         [ 13, 5, 1, "Sad"],
209         [ 14, 5, 2, "Under The Weather"],
210         [ 15, 5, 3, "Suicidal"],
211         [ 16, 1, 1, "The Bees Knees"],
212         [ 17, 1, 2, "Apiary"],
213         [ 18, 1, 3, "Beehind You"],
214     ]);
215 }
216
217 1;