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