The final (for now) part of the saga - castaway's real failing test
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
CommitLineData
33dd4e80 1use strict;
af2d42c0 2use warnings;
33dd4e80 3
04ec3909 4use Test::More;
fe8cee09 5use Test::Exception;
33dd4e80 6use lib qw(t/lib);
7use DBICTest;
8
3424e5e8 9plan tests => 104;
04ec3909 10
33dd4e80 11my $schema = DBICTest->init_schema();
12
c45f4e10 13# simple create + parent (the stuff $rs belongs_to)
04ec3909 14eval {
c45f4e10 15 my $cd = $schema->resultset('CD')->create({
04ec3909 16 artist => {
17 name => 'Fred Bloggs'
18 },
19 title => 'Some CD',
20 year => 1996
21 });
22
c45f4e10 23 isa_ok($cd, 'DBICTest::CD', 'Created CD object');
24 isa_ok($cd->artist, 'DBICTest::Artist', 'Created related Artist');
25 is($cd->artist->name, 'Fred Bloggs', 'Artist created correctly');
26};
27diag $@ if $@;
28
ab4b097b 29# same as above but the child and parent have no values,
c45f4e10 30# except for an explicit parent pk
31eval {
32 my $bm_rs = $schema->resultset('Bookmark');
33 my $bookmark = $bm_rs->create({
34 link => {
35 id => 66,
36 },
37 });
38
39 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
40 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
41 is (
42 $bm_rs->search (
43 { 'link.title' => $bookmark->link->title },
44 { join => 'link' },
45 )->count,
46 1,
47 'Bookmark and link made it to the DB',
48 );
04ec3909 49};
50diag $@ if $@;
51
52# create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
53eval {
ab4b097b 54 my $artist = $schema->resultset('Artist')->first;
55 my $cd = $artist->create_related (cds => {
56 title => 'Music to code by',
57 year => 2007,
58 tags => [
59 { 'tag' => 'rock' },
60 ],
61 });
04ec3909 62
ab4b097b 63 isa_ok($cd, 'DBICTest::CD', 'Created CD');
64 is($cd->title, 'Music to code by', 'CD created correctly');
65 is($cd->tags->count, 1, 'One tag created for CD');
66 is($cd->tags->first->tag, 'rock', 'Tag created correctly');
04ec3909 67
04ec3909 68};
69diag $@ if $@;
70
fe8cee09 71throws_ok (
72 sub {
73 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
74 $schema->resultset('Artist')->first->update({
75 cds => [
76 { title => 'Yet another CD',
77 year => 2006,
78 },
79 ],
80 });
81 },
82 qr/Recursive update is not supported over relationships of type multi/,
83 'create via update of multi relationships throws an exception'
84);
85
ab4b097b 86# Create m2m while originating in the linker table
87eval {
88 my $artist = $schema->resultset('Artist')->first;
89 my $c2p = $schema->resultset('CD_to_Producer')->create ({
90 cd => {
91 artist => $artist,
92 title => 'Bad investment',
93 year => 2008,
94 tracks => [
95 { position => 1, title => 'Just buy' },
96 { position => 2, title => 'Why did we do it' },
97 { position => 3, title => 'Burn baby burn' },
98 ],
99 },
100 producer => {
101 name => 'Lehman Bros.',
102 },
103 });
104
105 isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created');
106 my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' });
107 isa_ok ($prod, 'DBICTest::Producer', 'Producer row found');
108 is ($prod->cds->count, 1, 'Producer has one production');
109 my $cd = $prod->cds->first;
110 is ($cd->title, 'Bad investment', 'CD created correctly');
111 is ($cd->tracks->count, 3, 'CD has 3 tracks');
112
113};
114diag $@ if $@;
115
43483f30 116# create over > 1 levels of might_have with multiple has_many and multiple m2m
117# but starting at a has_many level
118# (A => { might_have => { B => has_many => C <= has_many D} } ) or
119# CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
120# \
121# \-> has_many \
122# --> CD2Producer
123# /-> has_many /
124# /
125# Producer
51f30f72 126eval {
127 my $artist = $schema->resultset('Artist')->first;
128 my $cd = $schema->resultset('CD')->create ({
129 artist => $artist,
130 title => 'Music to code by at night',
131 year => 2008,
132 tracks => [
133 {
43483f30 134 position => 1, # some day me might test this with Ordered
51f30f72 135 title => 'Off by one again',
136 },
137 {
138 position => 2,
139 title => 'The dereferencer',
140 cd_single => {
141 artist => $artist,
142 year => 2008,
143 title => 'Was that a null (Single)',
144 tracks => [
145 { title => 'The dereferencer', position => 1 },
146 { title => 'The dereferencer II', position => 2 },
147 ],
148 cd_to_producer => [
149 {
150 producer => {
151 name => 'K&R',
152 }
43483f30 153 },
154 {
155 producer => {
156 name => 'Don Knuth',
157 }
158 },
51f30f72 159 ]
160 },
161 },
162 ],
163 });
164
165 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
166 is ($cd->title, 'Music to code by at night', 'Correct CD title');
167 is ($cd->tracks->count, 2, 'Two tracks on main CD');
168
169 my ($t1, $t2) = $cd->tracks->all;
170 is ($t1->title, 'Off by one again', 'Correct 1st track name');
171 is ($t1->cd_single, undef, 'No single for 1st track');
172 is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
173 isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
174
175 my $single = $t2->cd_single;
176 is ($single->tracks->count, 2, 'Two tracks on single CD');
177 is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
178 is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
179
43483f30 180 is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
181 is_deeply (
182 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
183 ['Don Knuth', 'K&R'],
184 'Producers named correctly',
185 );
186};
187diag $@ if $@;
188
189# Same as above but starting at the might_have directly
190# Track -> might have -> Single -> has_many -> Tracks
191# \
192# \-> has_many \
193# --> CD2Producer
194# /-> has_many /
195# /
196# Producer
197eval {
198 my $cd = $schema->resultset('CD')->first;
199 my $track = $schema->resultset('Track')->create ({
200 cd => $cd,
201 position => 77, # some day me might test this with Ordered
202 title => 'Multicreate rocks',
203 cd_single => {
204 artist => $cd->artist,
205 year => 2008,
206 title => 'Disemboweling MultiCreate',
207 tracks => [
208 { title => 'Why does mst write this way', position => 1 },
209 { title => 'Chainsaw celebration', position => 2 },
210 { title => 'Purl cleans up', position => 3 },
211 ],
212 cd_to_producer => [
213 {
214 producer => {
215 name => 'mst',
216 }
217 },
218 {
219 producer => {
220 name => 'castaway',
221 }
222 },
223 {
224 producer => {
225 name => 'theorbtwo',
226 }
227 },
228 ]
229 },
230 });
231
232 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
233 is ($track->title, 'Multicreate rocks', 'Correct Track title');
234
235 my $single = $track->cd_single;
236 isa_ok ($single, 'DBICTest::CD', 'Created a single with the track');
237 is ($single->tracks->count, 3, '3 tracks on single CD');
238 is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title');
239 is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title');
240 is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title');
241
242 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
243 is_deeply (
244 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
245 ['castaway', 'mst', 'theorbtwo'],
246 'Producers named correctly',
247 );
51f30f72 248};
249diag $@ if $@;
250
0c9b6ece 251# test might_have again but with a PK == FK in the middle (obviously not specified)
252eval {
253 my $artist = $schema->resultset('Artist')->first;
254 my $cd = $schema->resultset('CD')->create ({
255 artist => $artist,
256 title => 'Music to code by at twilight',
257 year => 2008,
258 artwork => {
259 images => [
260 { name => 'recursive descent' },
261 { name => 'tail packing' },
262 ],
263 },
264 });
265
266 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
267 is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
268 isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
269
270 # this test might look weird, but it failed at one point, keep it there
e5b05576 271 my $art_obj = $cd->artwork;
272 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
273 is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
0c9b6ece 274 is_deeply (
e5b05576 275 [ sort $art_obj->images->get_column ('name')->all ],
0c9b6ece 276 [ 'recursive descent', 'tail packing' ],
277 'Images named correctly in objects',
278 );
279
0c9b6ece 280 my $artwork = $schema->resultset('Artwork')->search (
281 { 'cd.title' => 'Music to code by at twilight' },
282 { join => 'cd' },
283 )->single;
284
285 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
286
287 is_deeply (
288 [ sort $artwork->images->get_column ('name')->all ],
289 [ 'recursive descent', 'tail packing' ],
290 'Images named correctly after search',
291 );
292};
293diag $@ if $@;
294
295# test might_have again but with just a PK and FK (neither specified) in the mid-table
296eval {
297 my $cd = $schema->resultset('CD')->first;
298 my $track = $schema->resultset ('Track')->create ({
299 cd => $cd,
300 position => 66,
301 title => 'Black',
302 lyrics => {
303 lyric_versions => [
304 { text => 'The color black' },
305 { text => 'The colour black' },
306 ],
307 },
308 });
309
310 isa_ok ($track, 'DBICTest::Track', 'Main track object created');
311 is ($track->title, 'Black', 'Correct track title');
312 isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
313
314 # this test might look weird, but it was failing at one point, keep it there
e5b05576 315 my $lyric_obj = $track->lyrics;
316 ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
317 ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
318 is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
0c9b6ece 319 is_deeply (
e5b05576 320 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
0c9b6ece 321 [ 'The color black', 'The colour black' ],
322 'Lyrics text in objects matches',
323 );
324
325
326 my $lyric = $schema->resultset('Lyrics')->search (
327 { 'track.title' => 'Black' },
328 { join => 'track' },
329 )->single;
330
331 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
332
333 is_deeply (
334 [ sort $lyric->lyric_versions->get_column ('text')->all ],
335 [ 'The color black', 'The colour black' ],
336 'Lyrics text via search matches',
337 );
338};
339diag $@ if $@;
340
3424e5e8 341# test a multilevel might-have with a PK == FK in the might_have/has_many table
342#
343# CD -> might have -> Artwork
344# \
345# \-> has_many \
346# --> Artwork_to_Artist
347# /-> has_many /
348# /
349# Artist
350eval {
351 my $someartist = $schema->resultset('Artist')->first;
352 my $cd = $schema->resultset('CD')->create ({
353 artist => $someartist,
354 title => 'Music to code by until the cows come home',
355 year => 2008,
356 artwork => {
357 artwork_to_artist => [
358 { artist => { name => 'cowboy joe' } },
359 { artist => { name => 'billy the kid' } },
360 ],
361 },
362 });
363
364 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
365 is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title');
366
367 my $art_obj = $cd->artwork;
368 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
369 is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object');
370 is_deeply (
371 [ sort $art_obj->artists->get_column ('name')->all ],
372 [ 'billy the kid', 'cowboy joe' ],
373 'Artists named correctly when queried via object',
374 );
375
376 my $artwork = $schema->resultset('Artwork')->search (
377 { 'cd.title' => 'Music to code by until the cows come home' },
378 { join => 'cd' },
379 )->single;
380 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
381 is_deeply (
382 [ sort $artwork->artists->get_column ('name')->all ],
383 [ 'billy the kid', 'cowboy joe' ],
384 'Artists named correctly queried via a new search',
385 );
386};
387diag $@ if $@;
388
389
04ec3909 390# nested find_or_create
391eval {
392 my $newartist2 = $schema->resultset('Artist')->find_or_create({
393 name => 'Fred 3',
394 cds => [
395 {
396 title => 'Noah Act',
397 year => 2007,
398 },
399 ],
400 });
401 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
402};
403diag $@ if $@;
404
405# multiple same level has_many create
406eval {
407 my $artist2 = $schema->resultset('Artist')->create({
51f30f72 408 name => 'Fred 4',
04ec3909 409 cds => [
410 {
411 title => 'Music to code by',
412 year => 2007,
413 },
414 ],
415 cds_unordered => [
416 {
417 title => 'Music to code by',
418 year => 2007,
419 },
420 ]
421 });
422
423 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
424};
425diag $@ if $@;
3d8ee6ab 426
04ec3909 427# first create_related pass
428eval {
3d8ee6ab 429 my $artist = $schema->resultset('Artist')->first;
430
431 my $cd_result = $artist->create_related('cds', {
432
433 title => 'TestOneCD1',
434 year => 2007,
435 tracks => [
436
437 { position=>111,
438 title => 'TrackOne',
439 },
440 { position=>112,
441 title => 'TrackTwo',
442 }
443 ],
444
445 });
446
447 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
448 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
449
450 my $tracks = $cd_result->tracks;
451
452 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
453
454 foreach my $track ($tracks->all)
455 {
456 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
457 }
04ec3909 458};
459diag $@ if $@;
3d8ee6ab 460
04ec3909 461# second create_related with same arguments
462eval {
2ec8e594 463 my $artist = $schema->resultset('Artist')->first;
3d8ee6ab 464
2ec8e594 465 my $cd_result = $artist->create_related('cds', {
3d8ee6ab 466
2ec8e594 467 title => 'TestOneCD2',
3d8ee6ab 468 year => 2007,
469 tracks => [
470
471 { position=>111,
472 title => 'TrackOne',
473 },
474 { position=>112,
475 title => 'TrackTwo',
476 }
477 ],
478
9c6d6d93 479 liner_notes => { notes => 'I can haz liner notes?' },
480
3d8ee6ab 481 });
482
483 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
2ec8e594 484 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
9c6d6d93 485 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
3d8ee6ab 486
487 my $tracks = $cd_result->tracks;
488
489 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
490
491 foreach my $track ($tracks->all)
492 {
493 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
494 }
04ec3909 495};
496diag $@ if $@;
e5dddc05 497
04ec3909 498# create of parents of a record linker table
499eval {
500 my $cdp = $schema->resultset('CD_to_Producer')->create({
501 cd => { artist => 1, title => 'foo', year => 2000 },
502 producer => { name => 'jorge' }
503 });
504 ok($cdp, 'join table record created ok');
505};
506diag $@ if $@;
2bc3c81e 507
e040a768 508TODO: {
26f9141c 509local $TODO = 'Next 2 evals are NOT supposed to work, jnaps code will be torn to bits in another branch';
04ec3909 510#SPECIAL_CASE
511eval {
2bc3c81e 512 my $kurt_cobain = { name => 'Kurt Cobain' };
513
514 my $in_utero = $schema->resultset('CD')->new({
515 title => 'In Utero',
516 year => 1993
517 });
518
519 $kurt_cobain->{cds} = [ $in_utero ];
520
521
522 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
523 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
524
525 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
526 is($a->cds && $a->cds->first && $a->cds->first->title,
527 'In Utero', 'CD insertion ok');
04ec3909 528};
529diag $@ if $@;
2bc3c81e 530
04ec3909 531#SPECIAL_CASE2
532eval {
2bc3c81e 533 my $pink_floyd = { name => 'Pink Floyd' };
534
535 my $the_wall = { title => 'The Wall', year => 1979 };
536
537 $pink_floyd->{cds} = [ $the_wall ];
538
539
540 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
541 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
542
543 is($a->name, 'Pink Floyd', 'Artist insertion ok');
544 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
04ec3909 545};
546diag $@ if $@;
26f9141c 547}
548
e02b9964 549
550## Create foreign key col obj including PK
551## See test 20 in 66relationships.t
04ec3909 552eval {
553 my $new_cd_hashref = {
554 cdid => 27,
555 title => 'Boogie Woogie',
556 year => '2007',
557 artist => { artistid => 17, name => 'king luke' }
558 };
e02b9964 559
04ec3909 560 my $cd = $schema->resultset("CD")->find(1);
f10ac17d 561
04ec3909 562 is($cd->artist->id, 1, 'rel okay');
f10ac17d 563
04ec3909 564 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
565 is($new_cd->artist->id, 17, 'new id retained okay');
566};
567diag $@ if $@;
6ede9177 568
569eval {
570 $schema->resultset("CD")->create({
38c03c20 571 cdid => 28,
370f2ba2 572 title => 'Boogie Wiggle',
38c03c20 573 year => '2007',
574 artist => { artistid => 18, name => 'larry' }
6ede9177 575 });
38c03c20 576};
6ede9177 577is($@, '', 'new cd created without clash on related artist');
38c03c20 578
f10ac17d 579# Make sure exceptions from errors in created rels propogate
580eval {
370f2ba2 581 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
582 #$t->cd($t->new_related('cd', { artist => undef } ) );
583 #$t->{_rel_in_storage} = 0;
f10ac17d 584 $t->insert;
585};
586like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
bbbf67eb 587
588# Test multi create over many_to_many
04ec3909 589eval {
590 $schema->resultset('CD')->create ({
591 artist => {
592 name => 'larry', # should already exist
593 },
76b8cf98 594 title => 'Warble Marble',
595 year => '2009',
596 cd_to_producer => [
04ec3909 597 { producer => { name => 'Cowboy Neal' } },
76b8cf98 598 ],
04ec3909 599 });
76b8cf98 600
04ec3909 601 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
602 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
603 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
604 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
605};
76b8cf98 606
607# and some insane multicreate
608# (should work, despite the fact that no one will probably use it this way)
609
04ec3909 610# first count how many rows do we initially have
76b8cf98 611my $counts;
04ec3909 612$counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
76b8cf98 613
614# do the crazy create
04ec3909 615eval {
616 $schema->resultset('CD')->create ({
617 artist => {
533292a0 618 name => 'james',
04ec3909 619 },
76b8cf98 620 title => 'Greatest hits 1',
621 year => '2012',
622 genre => {
623 name => '"Greatest" collections',
624 },
04ec3909 625 tags => [
626 { tag => 'A' },
627 { tag => 'B' },
628 ],
76b8cf98 629 cd_to_producer => [
630 {
631 producer => {
533292a0 632 name => 'bob',
76b8cf98 633 producer_to_cd => [
634 {
635 cd => {
636 artist => {
533292a0 637 name => 'lars',
04ec3909 638 cds => [
639 {
533292a0 640 title => 'Greatest hits 2',
04ec3909 641 year => 2012,
642 genre => {
643 name => '"Greatest" collections',
644 },
645 tags => [
646 { tag => 'A' },
647 { tag => 'B' },
648 ],
533292a0 649 # This cd is created via artist so it doesn't know about producers
650 cd_to_producer => [
651 # if we specify 'bob' here things bomb
652 # as the producer attached to Greatest Hits 1 is
653 # already created, but not yet inserted.
654 # Maybe this can be fixed, but things are hairy
655 # enough already.
656 #
657 #{ producer => { name => 'bob' } },
658 { producer => { name => 'paul' } },
659 { producer => {
660 name => 'flemming',
661 producer_to_cd => [
662 { cd => {
663 artist => {
664 name => 'kirk',
665 cds => [
666 {
667 title => 'Greatest hits 3',
668 year => 2012,
669 genre => {
670 name => '"Greatest" collections',
671 },
672 tags => [
673 { tag => 'A' },
674 { tag => 'B' },
675 ],
676 },
677 {
678 title => 'Greatest hits 4',
679 year => 2012,
680 genre => {
681 name => '"Greatest" collections2',
682 },
683 tags => [
684 { tag => 'A' },
685 { tag => 'B' },
686 ],
687 },
688 ],
689 },
690 title => 'Greatest hits 5',
691 year => 2013,
692 genre => {
693 name => '"Greatest" collections2',
694 },
695 }},
696 ],
697 }},
698 ],
04ec3909 699 },
700 ],
76b8cf98 701 },
533292a0 702 title => 'Greatest hits 6',
76b8cf98 703 year => 2012,
704 genre => {
705 name => '"Greatest" collections',
706 },
04ec3909 707 tags => [
708 { tag => 'A' },
709 { tag => 'B' },
710 ],
76b8cf98 711 },
712 },
713 {
714 cd => {
715 artist => {
533292a0 716 name => 'lars', # should already exist
717 # even though the artist 'name' is not uniquely constrained
718 # find_or_create will arguably DWIM
76b8cf98 719 },
533292a0 720 title => 'Greatest hits 7',
721 year => 2013,
76b8cf98 722 },
723 },
724 ],
725 },
726 },
727 ],
04ec3909 728 });
76b8cf98 729
533292a0 730 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
731 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
732 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
733 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
734 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
735
736 my $cd_rs = $schema->resultset ('CD')
737 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
738 is ($cd_rs->count, 7, '7 greatest hits created');
739
740 my $cds_2012 = $cd_rs->search ({ year => 2012});
741 is ($cds_2012->count, 5, '5 CDs created in 2012');
742
743 is (
744 $cds_2012->search(
745 { 'tags.tag' => { -in => [qw/A B/] } },
746 { join => 'tags', group_by => 'me.cdid' }
747 ),
748 5,
c45f4e10 749 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
533292a0 750 );
751
752 my $paul_prod = $cd_rs->search (
753 { 'producer.name' => 'paul'},
754 { join => { cd_to_producer => 'producer' } }
755 );
756 is ($paul_prod->count, 1, 'Paul had 1 production');
757 my $pauls_cd = $paul_prod->single;
758 is ($pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer');
759 is (
760 $pauls_cd->search_related ('cd_to_producer',
761 { 'producer.name' => 'flemming'},
762 { join => 'producer' }
763 )->count,
764 1,
765 'The second producer is flemming',
766 );
767
768 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
769 is ($kirk_cds, 3, 'Kirk had 3 CDs');
770 is (
771 $kirk_cds->search (
772 { 'cd_to_producer.cd' => { '!=', undef } },
773 { join => 'cd_to_producer' },
774 ),
775 1,
776 'Kirk had a producer only on one cd',
777 );
778
779 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
780 is ($lars_cds->count, 3, 'Lars had 3 CDs');
781 is (
782 $lars_cds->search (
783 { 'cd_to_producer.cd' => undef },
784 { join => 'cd_to_producer' },
785 ),
786 0,
787 'Lars always had a producer',
788 );
789 is (
790 $lars_cds->search_related ('cd_to_producer',
791 { 'producer.name' => 'flemming'},
792 { join => 'producer' }
793 )->count,
794 1,
795 'Lars produced 1 CD with flemming',
796 );
797 is (
798 $lars_cds->search_related ('cd_to_producer',
799 { 'producer.name' => 'bob'},
800 { join => 'producer' }
801 )->count,
802 2,
803 'Lars produced 2 CDs with bob',
804 );
805
806 my $bob_prod = $cd_rs->search (
807 { 'producer.name' => 'bob'},
808 { join => { cd_to_producer => 'producer' } }
809 );
810 is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
811
812 is (
813 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
814 1,
c45f4e10 815 "Bob produced james' only CD",
533292a0 816 );
04ec3909 817};
818diag $@ if $@;
ab4b097b 819
8201;