10 # an insane multicreate
11 # (should work, despite the fact that no one will probably use it this way)
13 my $schema = DBICTest->init_schema();
15 # first count how many rows do we initially have
17 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
21 $schema->resultset('CD')->create ({
25 title => 'Greatest hits 1',
28 name => '"Greatest" collections',
45 title => 'Greatest hits 2',
48 name => '"Greatest" collections',
54 # This cd is created via artist so it doesn't know about producers
56 { producer => { name => 'bob' } },
57 { producer => { name => 'paul' } },
66 title => 'Greatest hits 3',
69 name => '"Greatest" collections',
77 title => 'Greatest hits 4',
80 name => '"Greatest" collections2',
89 title => 'Greatest hits 5',
92 name => '"Greatest" collections2',
101 title => 'Greatest hits 6',
104 name => '"Greatest" collections',
115 name => 'lars', # should already exist
116 # even though the artist 'name' is not uniquely constrained
117 # find_or_create will arguably DWIM
119 title => 'Greatest hits 7',
129 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
130 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
131 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
132 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
133 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
135 my $cd_rs = $schema->resultset ('CD')
136 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
137 is ($cd_rs->count, 7, '7 greatest hits created');
139 my $cds_2012 = $cd_rs->search ({ year => 2012});
140 is ($cds_2012->count, 5, '5 CDs created in 2012');
144 { 'tags.tag' => { -in => [qw/A B/] } },
147 group_by => 'me.cdid',
148 having => 'count(me.cdid) = 2',
152 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
155 my $paul_prod = $cd_rs->search (
156 { 'producer.name' => 'paul'},
157 { join => { cd_to_producer => 'producer' } }
159 is ($paul_prod->count, 1, 'Paul had 1 production');
160 my $pauls_cd = $paul_prod->single;
161 is ($pauls_cd->cd_to_producer->count, 3, 'Paul had two co-producers');
163 $pauls_cd->search_related ('cd_to_producer',
164 { 'producer.name' => 'flemming'},
165 { join => 'producer' }
168 'The second producer is flemming',
171 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
172 is ($kirk_cds, 3, 'Kirk had 3 CDs');
175 { 'cd_to_producer.cd' => { '!=', undef } },
176 { join => 'cd_to_producer' },
179 'Kirk had a producer only on one cd',
182 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
183 is ($lars_cds->count, 3, 'Lars had 3 CDs');
186 { 'cd_to_producer.cd' => undef },
187 { join => 'cd_to_producer' },
190 'Lars always had a producer',
193 $lars_cds->search_related ('cd_to_producer',
194 { 'producer.name' => 'flemming'},
195 { join => 'producer' }
198 'Lars produced 1 CD with flemming',
201 $lars_cds->search_related ('cd_to_producer',
202 { 'producer.name' => 'bob'},
203 { join => 'producer' }
206 'Lars produced 3 CDs with bob',
209 my $bob_prod = $cd_rs->search (
210 { 'producer.name' => 'bob'},
211 { join => { cd_to_producer => 'producer' } }
213 is ($bob_prod->count, 4, 'Bob produced a total of 4 CDs');
214 ok ($bob_prod->find ({ title => 'Greatest hits 1'}), '1st Bob production name correct');
215 ok ($bob_prod->find ({ title => 'Greatest hits 6'}), '2nd Bob production name correct');
216 ok ($bob_prod->find ({ title => 'Greatest hits 2'}), '3rd Bob production name correct');
217 ok ($bob_prod->find ({ title => 'Greatest hits 7'}), '4th Bob production name correct');
220 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
222 "Bob produced james' only CD",