5 use base qw/DBIx::Class::Core/;
8 my $db_file = "t/var/DBIxClass.db";
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";
14 __PACKAGE__->connection("dbi:SQLite:${db_file}");
16 my $dbh = __PACKAGE__->storage->dbh;
19 CREATE TABLE artist (artistid INTEGER NOT NULL PRIMARY KEY, name VARCHAR);
21 CREATE TABLE cd (cdid INTEGER NOT NULL PRIMARY KEY, artist INTEGER NOT NULL,
22 title VARCHAR, year VARCHAR);
24 CREATE TABLE liner_notes (liner_id INTEGER NOT NULL PRIMARY KEY, notes VARCHAR);
26 CREATE TABLE track (trackid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL,
27 position INTEGER NOT NULL, title VARCHAR);
29 CREATE TABLE tags (tagid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL,
32 CREATE TABLE twokeys (artist INTEGER NOT NULL, cd INTEGER NOT NULL,
33 PRIMARY KEY (artist, cd) );
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) );
39 CREATE TABLE onekey (id INTEGER NOT NULL PRIMARY KEY,
40 artist INTEGER NOT NULL, cd INTEGER NOT NULL );
42 INSERT INTO artist (artistid, name) VALUES (1, 'Caterwauler McCrae');
44 INSERT INTO artist (artistid, name) VALUES (2, 'Random Boy Band');
46 INSERT INTO artist (artistid, name) VALUES (3, 'We Are Goth');
48 INSERT INTO cd (cdid, artist, title, year)
49 VALUES (1, 1, "Spoonful of bees", 1999);
51 INSERT INTO cd (cdid, artist, title, year)
52 VALUES (2, 1, "Forkful of bees", 2001);
54 INSERT INTO cd (cdid, artist, title, year)
55 VALUES (3, 1, "Caterwaulin' Blues", 1997);
57 INSERT INTO cd (cdid, artist, title, year)
58 VALUES (4, 2, "Generic Manufactured Singles", 2001);
60 INSERT INTO cd (cdid, artist, title, year)
61 VALUES (5, 3, "Come Be Depressed With Us", 1998);
63 INSERT INTO liner_notes (liner_id, notes)
64 VALUES (2, "Buy Whiskey!");
66 INSERT INTO liner_notes (liner_id, notes)
67 VALUES (4, "Buy Merch!");
69 INSERT INTO liner_notes (liner_id, notes)
70 VALUES (5, "Kill Yourself!");
72 INSERT INTO tags (tagid, cd, tag) VALUES (1, 1, "Blue");
74 INSERT INTO tags (tagid, cd, tag) VALUES (2, 2, "Blue");
76 INSERT INTO tags (tagid, cd, tag) VALUES (3, 3, "Blue");
78 INSERT INTO tags (tagid, cd, tag) VALUES (4, 5, "Blue");
80 INSERT INTO tags (tagid, cd, tag) VALUES (5, 2, "Cheesy");
82 INSERT INTO tags (tagid, cd, tag) VALUES (6, 4, "Cheesy");
84 INSERT INTO tags (tagid, cd, tag) VALUES (7, 5, "Cheesy");
86 INSERT INTO tags (tagid, cd, tag) VALUES (8, 2, "Shiny");
88 INSERT INTO tags (tagid, cd, tag) VALUES (9, 4, "Shiny");
90 INSERT INTO twokeys (artist, cd) VALUES (1, 1);
92 INSERT INTO twokeys (artist, cd) VALUES (1, 2);
94 INSERT INTO twokeys (artist, cd) VALUES (2, 2);
96 INSERT INTO fourkeys (foo, bar, hello, goodbye) VALUES (1, 2, 3, 4);
98 INSERT INTO fourkeys (foo, bar, hello, goodbye) VALUES (5, 4, 3, 6);
100 INSERT INTO onekey (id, artist, cd) VALUES (1, 1, 1);
102 INSERT INTO onekey (id, artist, cd) VALUES (2, 1, 2);
104 INSERT INTO onekey (id, artist, cd) VALUES (3, 2, 2);
107 $dbh->do($_) for split(/\n\n/, $sql);
109 package DBICTest::LinerNotes;
113 DBICTest::LinerNotes->table('liner_notes');
114 DBICTest::LinerNotes->add_columns(qw/liner_id notes/);
115 DBICTest::LinerNotes->set_primary_key('liner_id');
117 package DBICTest::Tag;
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' }
129 package DBICTest::Track;
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' }
141 package DBICTest::CD;
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' }
152 DBICTest::CD->add_relationship(
153 tracks => 'DBICTest::Track',
154 { 'foreign.cd' => 'self.cdid' }
156 DBICTest::CD->add_relationship(
157 tags => 'DBICTest::Tag',
158 { 'foreign.cd' => 'self.cdid' }
160 #DBICTest::CD->might_have(liner_notes => 'DBICTest::LinerNotes' => qw/notes/);
162 package DBICTest::Artist;
166 DBICTest::Artist->table('artist');
167 DBICTest::Artist->add_columns(qw/artistid name/);
168 DBICTest::Artist->set_primary_key('artistid');
169 DBICTest::Artist->add_relationship(
170 cds => 'DBICTest::CD',
171 { 'foreign.artist' => 'self.artistid' },
172 { order_by => 'year' }
174 DBICTest::Artist->add_relationship(
175 twokeys => 'DBICTest::TwoKeys',
176 { 'foreign.artist' => 'self.artistid' }
178 DBICTest::Artist->add_relationship(
179 onekeys => 'DBICTest::OneKey',
180 { 'foreign.artist' => 'self.artistid' }
183 package DBICTest::OneKey;
187 DBICTest::OneKey->table('onekey');
188 DBICTest::OneKey->add_columns(qw/id artist cd/);
189 DBICTest::OneKey->set_primary_key('id');
191 package DBICTest::TwoKeys;
195 DBICTest::TwoKeys->table('twokeys');
196 DBICTest::TwoKeys->add_columns(qw/artist cd/);
197 DBICTest::TwoKeys->set_primary_key(qw/artist cd/);
198 DBICTest::TwoKeys->add_relationship(
199 artist => 'DBICTest::Artist',
200 { 'foreign.artistid' => 'self.artist' }
202 DBICTest::TwoKeys->add_relationship(
203 cd => 'DBICTest::CD',
204 { 'foreign.cdid' => 'self.cd' }
207 package DBICTest::FourKeys;
211 DBICTest::FourKeys->table('fourkeys');
212 DBICTest::FourKeys->add_columns(qw/foo bar hello goodbye/);
213 DBICTest::FourKeys->set_primary_key(qw/foo bar hello goodbye/);