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 # if we specify 'bob' here things bomb
58 # as the producer attached to Greatest Hits 1 is
59 # already created, but not yet inserted.
60 # Maybe this can be fixed, but things are hairy
63 #{ producer => { name => 'bob' } },
64 { producer => { name => 'paul' } },
73 title => 'Greatest hits 3',
76 name => '"Greatest" collections',
84 title => 'Greatest hits 4',
87 name => '"Greatest" collections2',
96 title => 'Greatest hits 5',
99 name => '"Greatest" collections2',
108 title => 'Greatest hits 6',
111 name => '"Greatest" collections',
122 name => 'lars', # should already exist
123 # even though the artist 'name' is not uniquely constrained
124 # find_or_create will arguably DWIM
126 title => 'Greatest hits 7',
136 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
137 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
138 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
139 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
140 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
142 my $cd_rs = $schema->resultset ('CD')
143 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
144 is ($cd_rs->count, 7, '7 greatest hits created');
146 my $cds_2012 = $cd_rs->search ({ year => 2012});
147 is ($cds_2012->count, 5, '5 CDs created in 2012');
151 { 'tags.tag' => { -in => [qw/A B/] } },
152 { join => 'tags', group_by => 'me.cdid' }
155 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
158 my $paul_prod = $cd_rs->search (
159 { 'producer.name' => 'paul'},
160 { join => { cd_to_producer => 'producer' } }
162 is ($paul_prod->count, 1, 'Paul had 1 production');
163 my $pauls_cd = $paul_prod->single;
164 is ($pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer');
166 $pauls_cd->search_related ('cd_to_producer',
167 { 'producer.name' => 'flemming'},
168 { join => 'producer' }
171 'The second producer is flemming',
174 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
175 is ($kirk_cds, 3, 'Kirk had 3 CDs');
178 { 'cd_to_producer.cd' => { '!=', undef } },
179 { join => 'cd_to_producer' },
182 'Kirk had a producer only on one cd',
185 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
186 is ($lars_cds->count, 3, 'Lars had 3 CDs');
189 { 'cd_to_producer.cd' => undef },
190 { join => 'cd_to_producer' },
193 'Lars always had a producer',
196 $lars_cds->search_related ('cd_to_producer',
197 { 'producer.name' => 'flemming'},
198 { join => 'producer' }
201 'Lars produced 1 CD with flemming',
204 $lars_cds->search_related ('cd_to_producer',
205 { 'producer.name' => 'bob'},
206 { join => 'producer' }
209 'Lars produced 2 CDs with bob',
212 my $bob_prod = $cd_rs->search (
213 { 'producer.name' => 'bob'},
214 { join => { cd_to_producer => 'producer' } }
216 is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
219 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
221 "Bob produced james' only CD",