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',
46 title => 'Greatest hits 2',
49 name => '"Greatest" collections',
55 # This cd is created via artist so it doesn't know about producers
57 { producer => { name => 'bob' } },
58 { producer => { name => 'paul' } },
67 title => 'Greatest hits 3',
70 name => '"Greatest" collections',
78 title => 'Greatest hits 4',
81 name => '"Greatest" collections2',
90 title => 'Greatest hits 5',
93 name => '"Greatest" collections2',
102 title => 'Greatest hits 6',
105 name => '"Greatest" collections',
116 name => 'lars', # should already exist
117 # even though the artist 'name' is not uniquely constrained
118 # find_or_create will arguably DWIM
120 title => 'Greatest hits 7',
130 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
131 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
132 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
133 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
134 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
136 my $cd_rs = $schema->resultset ('CD')
137 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
138 is ($cd_rs->count, 7, '7 greatest hits created');
140 my $cds_2012 = $cd_rs->search ({ year => 2012});
141 is ($cds_2012->count, 5, '5 CDs created in 2012');
145 { 'tags.tag' => { -in => [qw/A B/] } },
148 group_by => 'me.cdid',
149 having => 'count(me.cdid) = 2',
153 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
156 my $paul_prod = $cd_rs->search (
157 { 'producer.name' => 'paul'},
158 { join => { cd_to_producer => 'producer' } }
160 is ($paul_prod->count, 1, 'Paul had 1 production');
161 my $pauls_cd = $paul_prod->single;
162 is ($pauls_cd->cd_to_producer->count, 3, 'Paul had two co-producers');
164 $pauls_cd->search_related ('cd_to_producer',
165 { 'producer.name' => 'flemming'},
166 { join => 'producer' }
169 'The second producer is flemming',
172 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
173 is ($kirk_cds, 3, 'Kirk had 3 CDs');
176 { 'cd_to_producer.cd' => { '!=', undef } },
177 { join => 'cd_to_producer' },
180 'Kirk had a producer only on one cd',
183 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
184 is ($lars_cds->count, 3, 'Lars had 3 CDs');
187 { 'cd_to_producer.cd' => undef },
188 { join => 'cd_to_producer' },
191 'Lars always had a producer',
194 $lars_cds->search_related ('cd_to_producer',
195 { 'producer.name' => 'flemming'},
196 { join => 'producer' }
199 'Lars produced 1 CD with flemming',
202 $lars_cds->search_related ('cd_to_producer',
203 { 'producer.name' => 'bob'},
204 { join => 'producer' }
207 'Lars produced 3 CDs with bob',
210 my $bob_prod = $cd_rs->search (
211 { 'producer.name' => 'bob'},
212 { join => { cd_to_producer => 'producer' } }
214 is ($bob_prod->count, 4, 'Bob produced a total of 4 CDs');
215 ok ($bob_prod->find ({ title => 'Greatest hits 1'}), '1st Bob production name correct');
216 ok ($bob_prod->find ({ title => 'Greatest hits 6'}), '2nd Bob production name correct');
217 ok ($bob_prod->find ({ title => 'Greatest hits 2'}), '3rd Bob production name correct');
218 ok ($bob_prod->find ({ title => 'Greatest hits 7'}), '4th Bob production name correct');
221 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
223 "Bob produced james' only CD",