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