5 use base qw/DBIx::Class::Core/;
7 my $db_file = "t/var/DBIxClass.db";
9 unlink($db_file) if -e $db_file;
10 unlink($db_file . "-journal") if -e $db_file . "-journal";
11 mkdir("t/var") unless -d "t/var";
13 __PACKAGE__->connection("dbi:SQLite:${db_file}");
15 my $dbh = __PACKAGE__->_get_dbh;
18 CREATE TABLE artist (artistid INTEGER NOT NULL PRIMARY KEY, name VARCHAR);
20 CREATE TABLE cd (cdid INTEGER NOT NULL PRIMARY KEY, artist INTEGER NOT NULL,
21 title VARCHAR, year VARCHAR);
23 CREATE TABLE liner_notes (liner_id INTEGER NOT NULL PRIMARY KEY, notes VARCHAR);
25 CREATE TABLE track (trackid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL,
26 position INTEGER NOT NULL, title VARCHAR);
28 CREATE TABLE tags (tagid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL,
31 CREATE TABLE twokeys (artist INTEGER NOT NULL, cd INTEGER NOT NULL,
32 PRIMARY KEY (artist, cd) );
34 CREATE 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) );
38 CREATE TABLE onekey (id INTEGER NOT NULL PRIMARY KEY,
39 artist INTEGER NOT NULL, cd INTEGER NOT NULL );
41 INSERT INTO artist (artistid, name) VALUES (1, 'Caterwauler McCrae');
43 INSERT INTO artist (artistid, name) VALUES (2, 'Random Boy Band');
45 INSERT INTO artist (artistid, name) VALUES (3, 'We Are Goth');
47 INSERT INTO cd (cdid, artist, title, year)
48 VALUES (1, 1, "Spoonful of bees", 1999);
50 INSERT INTO cd (cdid, artist, title, year)
51 VALUES (2, 1, "Forkful of bees", 2001);
53 INSERT INTO cd (cdid, artist, title, year)
54 VALUES (3, 1, "Caterwaulin' Blues", 1997);
56 INSERT INTO cd (cdid, artist, title, year)
57 VALUES (4, 2, "Generic Manufactured Singles", 2001);
59 INSERT INTO cd (cdid, artist, title, year)
60 VALUES (5, 3, "Come Be Depressed With Us", 1998);
62 INSERT INTO liner_notes (liner_id, notes)
63 VALUES (2, "Buy Whiskey!");
65 INSERT INTO liner_notes (liner_id, notes)
66 VALUES (4, "Buy Merch!");
68 INSERT INTO liner_notes (liner_id, notes)
69 VALUES (5, "Kill Yourself!");
71 INSERT INTO tags (tagid, cd, tag) VALUES (1, 1, "Blue");
73 INSERT INTO tags (tagid, cd, tag) VALUES (2, 2, "Blue");
75 INSERT INTO tags (tagid, cd, tag) VALUES (3, 3, "Blue");
77 INSERT INTO tags (tagid, cd, tag) VALUES (4, 5, "Blue");
79 INSERT INTO tags (tagid, cd, tag) VALUES (5, 2, "Cheesy");
81 INSERT INTO tags (tagid, cd, tag) VALUES (6, 4, "Cheesy");
83 INSERT INTO tags (tagid, cd, tag) VALUES (7, 5, "Cheesy");
85 INSERT INTO tags (tagid, cd, tag) VALUES (8, 2, "Shiny");
87 INSERT INTO tags (tagid, cd, tag) VALUES (9, 4, "Shiny");
89 INSERT INTO twokeys (artist, cd) VALUES (1, 1);
91 INSERT INTO twokeys (artist, cd) VALUES (1, 2);
93 INSERT INTO twokeys (artist, cd) VALUES (2, 2);
95 INSERT INTO fourkeys (foo, bar, hello, goodbye) VALUES (1, 2, 3, 4);
97 INSERT INTO fourkeys (foo, bar, hello, goodbye) VALUES (5, 4, 3, 6);
99 INSERT INTO onekey (id, artist, cd) VALUES (1, 1, 1);
101 INSERT INTO onekey (id, artist, cd) VALUES (2, 1, 2);
103 INSERT INTO onekey (id, artist, cd) VALUES (3, 2, 2);
106 $dbh->do($_) for split(/\n\n/, $sql);
108 package DBICTest::LinerNotes;
112 DBICTest::LinerNotes->table('liner_notes');
113 DBICTest::LinerNotes->add_columns(qw/liner_id notes/);
114 DBICTest::LinerNotes->set_primary_key('liner_id');
116 package DBICTest::Tag;
120 DBICTest::Tag->table('tags');
121 DBICTest::Tag->add_columns(qw/tagid cd tag/);
122 DBICTest::Tag->set_primary_key('tagid');
123 DBICTest::Tag->add_relationship(
124 cd => 'DBICTest::CD',
125 { 'foreign.cdid' => 'self.cd' }
128 package DBICTest::Track;
132 DBICTest::Track->table('track');
133 DBICTest::Track->add_columns(qw/trackid cd position title/);
134 DBICTest::Track->set_primary_key('trackid');
135 DBICTest::Track->add_relationship(
136 cd => 'DBICTest::CD',
137 { 'foreign.cdid' => 'self.cd' }
140 package DBICTest::CD;
144 DBICTest::CD->table('cd');
145 DBICTest::CD->add_columns(qw/cdid artist title year/);
146 DBICTest::CD->set_primary_key('cdid');
147 DBICTest::CD->add_relationship(
148 artist => 'DBICTest::Artist',
149 { 'foreign.artistid' => 'self.artist' }
151 DBICTest::CD->add_relationship(
152 tracks => 'DBICTest::Track',
153 { 'foreign.cd' => 'self.cdid' }
155 DBICTest::CD->add_relationship(
156 tags => 'DBICTest::Tag',
157 { 'foreign.cd' => 'self.cdid' }
159 #DBICTest::CD->might_have(liner_notes => 'DBICTest::LinerNotes' => qw/notes/);
161 package DBICTest::Artist;
165 DBICTest::Artist->table('artist');
166 DBICTest::Artist->add_columns(qw/artistid name/);
167 DBICTest::Artist->set_primary_key('artistid');
168 DBICTest::Artist->add_relationship(
169 cds => 'DBICTest::CD',
170 { 'foreign.artist' => 'self.artistid' },
171 { order_by => 'year' }
173 DBICTest::Artist->add_relationship(
174 twokeys => 'DBICTest::TwoKeys',
175 { 'foreign.artist' => 'self.artistid' }
177 DBICTest::Artist->add_relationship(
178 onekeys => 'DBICTest::OneKey',
179 { 'foreign.artist' => 'self.artistid' }
182 package DBICTest::OneKey;
186 DBICTest::OneKey->table('onekey');
187 DBICTest::OneKey->add_columns(qw/id artist cd/);
188 DBICTest::OneKey->set_primary_key('id');
190 package DBICTest::TwoKeys;
194 DBICTest::TwoKeys->table('twokeys');
195 DBICTest::TwoKeys->add_columns(qw/artist cd/);
196 DBICTest::TwoKeys->set_primary_key(qw/artist cd/);
197 DBICTest::TwoKeys->add_relationship(
198 artist => 'DBICTest::Artist',
199 { 'foreign.artistid' => 'self.artist' }
201 DBICTest::TwoKeys->add_relationship(
202 cd => 'DBICTest::CD',
203 { 'foreign.cdid' => 'self.cd' }
206 package DBICTest::FourKeys;
210 DBICTest::FourKeys->table('fourkeys');
211 DBICTest::FourKeys->add_columns(qw/foo bar hello goodbye/);
212 DBICTest::FourKeys->set_primary_key(qw/foo bar hello goodbye/);