11 # an insane multicreate
12 # (should work, despite the fact that no one will probably use it this way)
14 my $schema = DBICTest->init_schema();
16 # first count how many rows do we initially have
18 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
22 $schema->resultset('CD')->create ({
26 title => 'Greatest hits 1',
29 name => '"Greatest" collections',
31 name => '"Greatest" collections demographic',
49 title => 'Greatest hits 2',
52 name => '"Greatest" collections',
54 name => '"Greatest" collections demographic',
61 # This cd is created via artist so it doesn't know about producers
63 { producer => { name => 'bob' } },
64 { producer => { name => 'paul' } },
73 title => 'Greatest hits 3',
76 name => '"Greatest" collections',
78 name => '"Greatest" collections demographic',
87 title => 'Greatest hits 4',
90 name => '"Greatest" collections2',
92 name => '"Greatest" collections demographic',
102 title => 'Greatest hits 5',
105 name => '"Greatest" collections2',
107 name => '"Greatest" collections demographic',
117 title => 'Greatest hits 6',
120 name => '"Greatest" collections',
122 name => '"Greatest" collections demographic',
134 name => 'lars', # should already exist
135 # even though the artist 'name' is not uniquely constrained
136 # find_or_create will arguably DWIM
138 title => 'Greatest hits 7',
148 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
149 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
150 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
151 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
152 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
154 my $cd_rs = $schema->resultset ('CD')
155 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
156 is ($cd_rs->count, 7, '7 greatest hits created');
158 my $cds_2012 = $cd_rs->search ({ year => 2012});
159 is ($cds_2012->count, 5, '5 CDs created in 2012');
163 { 'tags.tag' => { -in => [qw/A B/] } },
164 { join => 'tags', group_by => 'me.cdid' }
167 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
170 my $paul_prod = $cd_rs->search (
171 { 'producer.name' => 'paul'},
172 { join => { cd_to_producer => 'producer' } }
174 is ($paul_prod->count, 1, 'Paul had 1 production');
175 my $pauls_cd = $paul_prod->single;
176 is ($pauls_cd->cd_to_producer->count, 3, 'Paul had two co-producers');
178 $pauls_cd->search_related ('cd_to_producer',
179 { 'producer.name' => 'flemming'},
180 { join => 'producer' }
183 'The second producer is flemming',
186 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
187 is ($kirk_cds, 3, 'Kirk had 3 CDs');
190 { 'cd_to_producer.cd' => { '!=', undef } },
191 { join => 'cd_to_producer' },
194 'Kirk had a producer only on one cd',
197 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
198 is ($lars_cds->count, 3, 'Lars had 3 CDs');
201 { 'cd_to_producer.cd' => undef },
202 { join => 'cd_to_producer' },
205 'Lars always had a producer',
208 $lars_cds->search_related ('cd_to_producer',
209 { 'producer.name' => 'flemming'},
210 { join => 'producer' }
213 'Lars produced 1 CD with flemming',
216 $lars_cds->search_related ('cd_to_producer',
217 { 'producer.name' => 'bob'},
218 { join => 'producer' }
221 'Lars produced 3 CDs with bob',
224 my $bob_prod = $cd_rs->search (
225 { 'producer.name' => 'bob'},
226 { join => { cd_to_producer => 'producer' } }
228 is ($bob_prod->count, 4, 'Bob produced a total of 4 CDs');
229 ok ($bob_prod->find ({ title => 'Greatest hits 1'}), '1st Bob production name correct');
230 ok ($bob_prod->find ({ title => 'Greatest hits 6'}), '2nd Bob production name correct');
231 ok ($bob_prod->find ({ title => 'Greatest hits 2'}), '3rd Bob production name correct');
232 ok ($bob_prod->find ({ title => 'Greatest hits 7'}), '4th Bob production name correct');
235 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
237 "Bob produced james' only CD",