1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
12 # an insane multicreate
13 # (should work, despite the fact that no one will probably use it this way)
15 my $schema = DBICTest->init_schema();
17 # first count how many rows do we initially have
19 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
23 $schema->resultset('CD')->create ({
27 title => 'Greatest hits 1',
30 name => '"Greatest" collections',
47 title => 'Greatest hits 2',
50 name => '"Greatest" collections',
56 # This cd is created via artist so it doesn't know about producers
58 { producer => { name => 'bob' } },
59 { producer => { name => 'paul' } },
68 title => 'Greatest hits 3',
71 name => '"Greatest" collections',
79 title => 'Greatest hits 4',
82 name => '"Greatest" collections2',
91 title => 'Greatest hits 5',
94 name => '"Greatest" collections2',
103 title => 'Greatest hits 6',
106 name => '"Greatest" collections',
117 name => 'lars', # should already exist
118 # even though the artist 'name' is not uniquely constrained
119 # find_or_create will arguably DWIM
121 title => 'Greatest hits 7',
131 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
132 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
133 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
134 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
135 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
137 my $cd_rs = $schema->resultset ('CD')
138 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
139 is ($cd_rs->count, 7, '7 greatest hits created');
141 my $cds_2012 = $cd_rs->search ({ year => 2012});
142 is ($cds_2012->count, 5, '5 CDs created in 2012');
146 { 'tags.tag' => { -in => [qw/A B/] } },
149 group_by => 'me.cdid',
150 having => 'count(me.cdid) = 2',
154 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
157 my $paul_prod = $cd_rs->search (
158 { 'producer.name' => 'paul'},
159 { join => { cd_to_producer => 'producer' } }
161 is ($paul_prod->count, 1, 'Paul had 1 production');
162 my $pauls_cd = $paul_prod->single;
163 is ($pauls_cd->cd_to_producer->count, 3, 'Paul had two co-producers');
165 $pauls_cd->search_related ('cd_to_producer',
166 { 'producer.name' => 'flemming'},
167 { join => 'producer' }
170 'The second producer is flemming',
173 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
174 is ($kirk_cds, 3, 'Kirk had 3 CDs');
177 { 'cd_to_producer.cd' => { '!=', undef } },
178 { join => 'cd_to_producer' },
181 'Kirk had a producer only on one cd',
184 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
185 is ($lars_cds->count, 3, 'Lars had 3 CDs');
188 { 'cd_to_producer.cd' => undef },
189 { join => 'cd_to_producer' },
192 'Lars always had a producer',
195 $lars_cds->search_related ('cd_to_producer',
196 { 'producer.name' => 'flemming'},
197 { join => 'producer' }
200 'Lars produced 1 CD with flemming',
203 $lars_cds->search_related ('cd_to_producer',
204 { 'producer.name' => 'bob'},
205 { join => 'producer' }
208 'Lars produced 3 CDs with bob',
211 my $bob_prod = $cd_rs->search (
212 { 'producer.name' => 'bob'},
213 { join => { cd_to_producer => 'producer' } }
215 is ($bob_prod->count, 4, 'Bob produced a total of 4 CDs');
216 ok ($bob_prod->find ({ title => 'Greatest hits 1'}), '1st Bob production name correct');
217 ok ($bob_prod->find ({ title => 'Greatest hits 6'}), '2nd Bob production name correct');
218 ok ($bob_prod->find ({ title => 'Greatest hits 2'}), '3rd Bob production name correct');
219 ok ($bob_prod->find ({ title => 'Greatest hits 7'}), '4th Bob production name correct');
222 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
224 "Bob produced james' only CD",