Added a use DateTime
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest.pm
1 package DBICTest;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class::Core/;
6 use DateTime;
7
8 my $db_file = "t/var/DBIxClass.db";
9
10 unlink($db_file) if -e $db_file;
11 unlink($db_file . "-journal") if -e $db_file . "-journal";
12 mkdir("t/var") unless -d "t/var";
13
14 __PACKAGE__->connection("dbi:SQLite:${db_file}");
15
16 my $dbh = __PACKAGE__->_get_dbh;
17
18 my $sql = <<EOSQL;
19 CREATE TABLE artist (artistid INTEGER NOT NULL PRIMARY KEY, name VARCHAR);
20
21 CREATE TABLE cd (cdid INTEGER NOT NULL PRIMARY KEY, artist INTEGER NOT NULL,
22                      title VARCHAR, year VARCHAR);
23
24 CREATE TABLE liner_notes (liner_id INTEGER NOT NULL PRIMARY KEY, notes VARCHAR);
25
26 CREATE TABLE track (trackid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL,
27                        position INTEGER NOT NULL, title VARCHAR);
28
29 CREATE TABLE tags (tagid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL,
30                       tag VARCHAR);
31
32 CREATE TABLE twokeys (artist INTEGER NOT NULL, cd INTEGER NOT NULL,
33                       PRIMARY KEY (artist, cd) );
34
35 CREATE TABLE fourkeys (foo INTEGER NOT NULL, bar INTEGER NOT NULL,
36                       hello INTEGER NOT NULL, goodbye INTEGER NOT NULL,
37                       PRIMARY KEY (foo, bar, hello, goodbye) );
38
39 CREATE TABLE onekey (id INTEGER NOT NULL PRIMARY KEY,
40                       artist INTEGER NOT NULL, cd INTEGER NOT NULL );
41
42 INSERT INTO artist (artistid, name) VALUES (1, 'Caterwauler McCrae');
43
44 INSERT INTO artist (artistid, name) VALUES (2, 'Random Boy Band');
45
46 INSERT INTO artist (artistid, name) VALUES (3, 'We Are Goth');
47
48 INSERT INTO cd (cdid, artist, title, year)
49     VALUES (1, 1, "Spoonful of bees", 1999);
50
51 INSERT INTO cd (cdid, artist, title, year)
52     VALUES (2, 1, "Forkful of bees", 2001);
53
54 INSERT INTO cd (cdid, artist, title, year)
55     VALUES (3, 1, "Caterwaulin' Blues", 1997);
56
57 INSERT INTO cd (cdid, artist, title, year)
58     VALUES (4, 2, "Generic Manufactured Singles", 2001);
59
60 INSERT INTO cd (cdid, artist, title, year)
61     VALUES (5, 3, "Come Be Depressed With Us", 1998);
62
63 INSERT INTO liner_notes (liner_id, notes)
64     VALUES (2, "Buy Whiskey!");
65
66 INSERT INTO liner_notes (liner_id, notes)
67     VALUES (4, "Buy Merch!");
68
69 INSERT INTO liner_notes (liner_id, notes)
70     VALUES (5, "Kill Yourself!");
71
72 INSERT INTO tags (tagid, cd, tag) VALUES (1, 1, "Blue");
73
74 INSERT INTO tags (tagid, cd, tag) VALUES (2, 2, "Blue");
75
76 INSERT INTO tags (tagid, cd, tag) VALUES (3, 3, "Blue");
77
78 INSERT INTO tags (tagid, cd, tag) VALUES (4, 5, "Blue");
79
80 INSERT INTO tags (tagid, cd, tag) VALUES (5, 2, "Cheesy");
81
82 INSERT INTO tags (tagid, cd, tag) VALUES (6, 4, "Cheesy");
83
84 INSERT INTO tags (tagid, cd, tag) VALUES (7, 5, "Cheesy");
85
86 INSERT INTO tags (tagid, cd, tag) VALUES (8, 2, "Shiny");
87
88 INSERT INTO tags (tagid, cd, tag) VALUES (9, 4, "Shiny");
89
90 INSERT INTO twokeys (artist, cd) VALUES (1, 1);
91
92 INSERT INTO twokeys (artist, cd) VALUES (1, 2);
93
94 INSERT INTO twokeys (artist, cd) VALUES (2, 2);
95
96 INSERT INTO fourkeys (foo, bar, hello, goodbye) VALUES (1, 2, 3, 4);
97
98 INSERT INTO fourkeys (foo, bar, hello, goodbye) VALUES (5, 4, 3, 6);
99
100 INSERT INTO onekey (id, artist, cd) VALUES (1, 1, 1);
101
102 INSERT INTO onekey (id, artist, cd) VALUES (2, 1, 2);
103
104 INSERT INTO onekey (id, artist, cd) VALUES (3, 2, 2);
105 EOSQL
106
107 $dbh->do($_) for split(/\n\n/, $sql);
108
109 package DBICTest::LinerNotes;
110
111 use base 'DBICTest';
112
113 DBICTest::LinerNotes->table('liner_notes');
114 DBICTest::LinerNotes->add_columns(qw/liner_id notes/);
115 DBICTest::LinerNotes->set_primary_key('liner_id');
116
117 package DBICTest::Tag;
118
119 use base 'DBICTest';
120
121 DBICTest::Tag->table('tags');
122 DBICTest::Tag->add_columns(qw/tagid cd tag/);
123 DBICTest::Tag->set_primary_key('tagid');
124 DBICTest::Tag->add_relationship(
125     cd => 'DBICTest::CD',
126     { 'foreign.cdid' => 'self.cd' }
127 );
128
129 package DBICTest::Track;
130
131 use base 'DBICTest';
132
133 DBICTest::Track->table('track');
134 DBICTest::Track->add_columns(qw/trackid cd position title/);
135 DBICTest::Track->set_primary_key('trackid');
136 DBICTest::Track->add_relationship(
137     cd => 'DBICTest::CD',
138     { 'foreign.cdid' => 'self.cd' }
139 );
140
141 package DBICTest::CD;
142
143 use base 'DBICTest';
144
145 DBICTest::CD->table('cd');
146 DBICTest::CD->add_columns(qw/cdid artist title year/);
147 DBICTest::CD->set_primary_key('cdid');
148 DBICTest::CD->add_relationship(
149     artist => 'DBICTest::Artist',
150     { 'foreign.artistid' => 'self.artist' }
151 );
152 DBICTest::CD->add_relationship(
153     tracks => 'DBICTest::Track',
154     { 'foreign.cd' => 'self.cdid' }
155 );
156 DBICTest::CD->add_relationship(
157     tags => 'DBICTest::Tag',
158     { 'foreign.cd' => 'self.cdid' }
159 );
160 #DBICTest::CD->might_have(liner_notes => 'DBICTest::LinerNotes' => qw/notes/);
161 DBICTest::CD->inflate_column( 'year',
162     { inflate => sub { DateTime->new( year => shift ) } },
163     { deflate => sub { shift->year } }
164 );
165
166 package DBICTest::Artist;
167
168 use base 'DBICTest';
169
170 DBICTest::Artist->table('artist');
171 DBICTest::Artist->add_columns(qw/artistid name/);
172 DBICTest::Artist->set_primary_key('artistid');
173 DBICTest::Artist->add_relationship(
174     cds => 'DBICTest::CD',
175     { 'foreign.artist' => 'self.artistid' },
176     { order_by => 'year' }
177 );
178 DBICTest::Artist->add_relationship(
179     twokeys => 'DBICTest::TwoKeys',
180     { 'foreign.artist' => 'self.artistid' }
181 );
182 DBICTest::Artist->add_relationship(
183     onekeys => 'DBICTest::OneKey',
184     { 'foreign.artist' => 'self.artistid' }
185 );
186
187 package DBICTest::OneKey;
188
189 use base 'DBICTest';
190
191 DBICTest::OneKey->table('onekey');
192 DBICTest::OneKey->add_columns(qw/id artist cd/);
193 DBICTest::OneKey->set_primary_key('id');
194
195 package DBICTest::TwoKeys;
196
197 use base 'DBICTest';
198
199 DBICTest::TwoKeys->table('twokeys');
200 DBICTest::TwoKeys->add_columns(qw/artist cd/);
201 DBICTest::TwoKeys->set_primary_key(qw/artist cd/);
202 DBICTest::TwoKeys->add_relationship(
203     artist => 'DBICTest::Artist',
204     { 'foreign.artistid' => 'self.artist' }
205 );
206 DBICTest::TwoKeys->add_relationship(
207     cd => 'DBICTest::CD',
208     { 'foreign.cdid' => 'self.cd' }
209 );
210
211 package DBICTest::FourKeys;
212
213 use base 'DBICTest';
214
215 DBICTest::FourKeys->table('fourkeys');
216 DBICTest::FourKeys->add_columns(qw/foo bar hello goodbye/);
217 DBICTest::FourKeys->set_primary_key(qw/foo bar hello goodbye/);
218
219 1;