changed set_primary_key() to use Tie::IxHash so order of pk's is remembered. this...
[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
7 my $db_file = "t/var/DBIxClass.db";
8
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";
12
13 __PACKAGE__->connection("dbi:SQLite:${db_file}");
14
15 my $dbh = __PACKAGE__->_get_dbh;
16
17 my $sql = <<EOSQL;
18 CREATE TABLE artist (artistid INTEGER NOT NULL PRIMARY KEY, name VARCHAR);
19
20 CREATE TABLE cd (cdid INTEGER NOT NULL PRIMARY KEY, artist INTEGER NOT NULL,
21                      title VARCHAR, year VARCHAR);
22
23 CREATE TABLE liner_notes (liner_id INTEGER NOT NULL PRIMARY KEY, notes VARCHAR);
24
25 CREATE TABLE track (trackid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL,
26                        position INTEGER NOT NULL, title VARCHAR);
27
28 CREATE TABLE tags (tagid INTEGER NOT NULL PRIMARY KEY, cd INTEGER NOT NULL,
29                       tag VARCHAR);
30
31 CREATE TABLE twokeys (artist INTEGER NOT NULL, cd INTEGER NOT NULL,
32                       PRIMARY KEY (artist, cd) );
33
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) );
37
38 CREATE TABLE onekey (id INTEGER NOT NULL PRIMARY KEY,
39                       artist INTEGER NOT NULL, cd INTEGER NOT NULL );
40
41 INSERT INTO artist (artistid, name) VALUES (1, 'Caterwauler McCrae');
42
43 INSERT INTO artist (artistid, name) VALUES (2, 'Random Boy Band');
44
45 INSERT INTO artist (artistid, name) VALUES (3, 'We Are Goth');
46
47 INSERT INTO cd (cdid, artist, title, year)
48     VALUES (1, 1, "Spoonful of bees", 1999);
49
50 INSERT INTO cd (cdid, artist, title, year)
51     VALUES (2, 1, "Forkful of bees", 2001);
52
53 INSERT INTO cd (cdid, artist, title, year)
54     VALUES (3, 1, "Caterwaulin' Blues", 1997);
55
56 INSERT INTO cd (cdid, artist, title, year)
57     VALUES (4, 2, "Generic Manufactured Singles", 2001);
58
59 INSERT INTO cd (cdid, artist, title, year)
60     VALUES (5, 3, "Come Be Depressed With Us", 1998);
61
62 INSERT INTO liner_notes (liner_id, notes)
63     VALUES (2, "Buy Whiskey!");
64
65 INSERT INTO liner_notes (liner_id, notes)
66     VALUES (4, "Buy Merch!");
67
68 INSERT INTO liner_notes (liner_id, notes)
69     VALUES (5, "Kill Yourself!");
70
71 INSERT INTO tags (tagid, cd, tag) VALUES (1, 1, "Blue");
72
73 INSERT INTO tags (tagid, cd, tag) VALUES (2, 2, "Blue");
74
75 INSERT INTO tags (tagid, cd, tag) VALUES (3, 3, "Blue");
76
77 INSERT INTO tags (tagid, cd, tag) VALUES (4, 5, "Blue");
78
79 INSERT INTO tags (tagid, cd, tag) VALUES (5, 2, "Cheesy");
80
81 INSERT INTO tags (tagid, cd, tag) VALUES (6, 4, "Cheesy");
82
83 INSERT INTO tags (tagid, cd, tag) VALUES (7, 5, "Cheesy");
84
85 INSERT INTO tags (tagid, cd, tag) VALUES (8, 2, "Shiny");
86
87 INSERT INTO tags (tagid, cd, tag) VALUES (9, 4, "Shiny");
88
89 INSERT INTO twokeys (artist, cd) VALUES (1, 1);
90
91 INSERT INTO twokeys (artist, cd) VALUES (1, 2);
92
93 INSERT INTO twokeys (artist, cd) VALUES (2, 2);
94
95 INSERT INTO fourkeys (foo, bar, hello, goodbye) VALUES (1, 2, 3, 4);
96
97 INSERT INTO fourkeys (foo, bar, hello, goodbye) VALUES (5, 4, 3, 6);
98
99 INSERT INTO onekey (id, artist, cd) VALUES (1, 1, 1);
100
101 INSERT INTO onekey (id, artist, cd) VALUES (2, 1, 2);
102
103 INSERT INTO onekey (id, artist, cd) VALUES (3, 2, 2);
104 EOSQL
105
106 $dbh->do($_) for split(/\n\n/, $sql);
107
108 package DBICTest::LinerNotes;
109
110 use base 'DBICTest';
111
112 DBICTest::LinerNotes->table('liner_notes');
113 DBICTest::LinerNotes->add_columns(qw/liner_id notes/);
114 DBICTest::LinerNotes->set_primary_key('liner_id');
115
116 package DBICTest::Tag;
117
118 use base 'DBICTest';
119
120 DBICTest::Tag->table('tags');
121 DBICTest::Tag->add_columns(qw/tagid cd tag/);
122 DBICTest::Tag->set_primary_key('tagid');
123 #DBICTest::Tag->has_a(cd => 'SweetTest::CD');
124
125 package DBICTest::Track;
126
127 use base 'DBICTest';
128
129 DBICTest::Track->table('track');
130 DBICTest::Track->add_columns(qw/trackid cd position title/);
131 DBICTest::Track->set_primary_key('trackid');
132 #DBICTest::Track->has_a(cd => 'SweetTest::CD');
133
134 package DBICTest::CD;
135
136 use base 'DBICTest';
137
138 DBICTest::CD->table('cd');
139 DBICTest::CD->add_columns(qw/cdid artist title year/);
140 DBICTest::CD->set_primary_key('trackid');
141
142 #DBICTest::CD->has_many(tracks => 'SweetTest::Track');
143 #DBICTest::CD->has_many(tags => 'SweetTest::Tag');
144 #DBICTest::CD->has_a(artist => 'SweetTest::Artist');
145
146 #DBICTest::CD->might_have(liner_notes => 'SweetTest::LinerNotes' => qw/notes/);
147
148 package DBICTest::Artist;
149
150 use base 'DBICTest';
151
152 DBICTest::Artist->table('artist');
153 DBICTest::Artist->add_columns(qw/artistid name/);
154 DBICTest::Artist->set_primary_key('artistid');
155 #DBICTest::Artist->has_many(cds => 'SweetTest::CD');
156 #DBICTest::Artist->has_many(twokeys => 'SweetTest::TwoKeys');
157 #DBICTest::Artist->has_many(onekeys => 'SweetTest::OneKey');
158
159 package DBICTest::TwoKeys;
160
161 use base 'DBICTest';
162
163 DBICTest::TwoKeys->table('twokeys');
164 DBICTest::TwoKeys->add_columns(qw/artist cd/);
165 DBICTest::TwoKeys->set_primary_key(qw/artist cd/);
166 #DBICTest::TwoKeys->has_a(artist => 'SweetTest::Artist');
167 #DBICTest::TwoKeys->has_a(cd => 'SweetTest::CD');
168
169 package DBICTest::FourKeys;
170
171 use base 'DBICTest';
172
173 DBICTest::FourKeys->table('fourkeys');
174 DBICTest::FourKeys->add_columns(qw/foo bar hello goodbye/);
175 DBICTest::FourKeys->set_primary_key(qw/foo bar hello goodbye/);
176
177 package DBICTest::OneKey;
178
179 use base 'DBICTest';
180
181 DBICTest::OneKey->table('onekey');
182 DBICTest::OneKey->add_columns(qw/id artist cd/);
183 DBICTest::OneKey->set_primary_key('id');
184
185 1;