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