11 my $schema = DBICTest->init_schema();
13 diag '* simple create + parent (the stuff $rs belongs_to)';
15 my $cd = $schema->resultset('CD')->recursive_update({
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');
29 diag '* same as above but the child and parent have no values, except for an explicit parent pk';
31 my $bm_rs = $schema->resultset('Bookmark');
32 my $bookmark = $bm_rs->recursive_update({
38 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
39 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
42 { 'link.title' => $bookmark->link->title },
46 'Bookmark and link made it to the DB',
51 diag '* Create m2m while originating in the linker table';
53 my $artist = $schema->resultset('Artist')->first;
54 my $c2p = $schema->resultset('CD_to_Producer')->recursive_update({
57 title => 'Bad investment',
60 { pos => 1, title => 'Just buy' },
61 { pos => 2, title => 'Why did we do it' },
62 { pos => 3, title => 'Burn baby burn' },
66 name => 'Lehman Bros.',
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');
82 * Create over > 1 levels of might_have with multiple has_many and multiple m2m
83 but starting at a has_many level
85 CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
95 my $artist = $schema->resultset('Artist')->first;
96 my $cd = $schema->resultset('CD')->recursive_update({
98 title => 'Music to code by at night',
102 pos => 1, # some day me might test this with Ordered
103 title => 'Off by one again',
107 title => 'The dereferencer',
111 title => 'Was that a null (Single)',
113 { title => 'The dereferencer', pos => 1 },
114 { title => 'The dereferencer II', pos => 2 },
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');
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');
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');
148 is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
150 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
151 ['Don Knuth', 'K&R'],
152 'Producers named correctly',
158 * Same as above but starting at the might_have directly
160 Track -> might have -> Single -> has_many -> Tracks
170 my $cd = $schema->resultset('CD')->first;
171 my $track = $schema->resultset('Track')->recursive_update({
173 pos => 77, # some day me might test this with Ordered
174 title => 'Multicreate rocks',
176 artist => $cd->artist,
178 title => 'Disemboweling MultiCreate',
180 { title => 'Why does mst write this way', pos => 1 },
181 { title => 'Chainsaw celebration', pos => 2 },
182 { title => 'Purl cleans up', pos => 3 },
204 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
205 is ($track->title, 'Multicreate rocks', 'Correct Track title');
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');
214 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
216 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
217 ['castaway', 'mst', 'theorbtwo'],
218 'Producers named correctly',
223 diag '* Test might_have again but with a PK == FK in the middle (obviously not specified)';
225 my $artist = $schema->resultset('Artist')->first;
226 my $cd = $schema->resultset('CD')->recursive_update({
228 title => 'Music to code by at twilight',
232 { name => 'recursive descent' },
233 { name => 'tail packing' },
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');
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');
247 [ sort $art_obj->images->get_column ('name')->all ],
248 [ 'recursive descent', 'tail packing' ],
249 'Images named correctly in objects',
252 my $artwork = $schema->resultset('Artwork')->search (
253 { 'cd.title' => 'Music to code by at twilight' },
257 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
260 [ sort $artwork->images->get_column ('name')->all ],
261 [ 'recursive descent', 'tail packing' ],
262 'Images named correctly after search',
267 diag '* Test might_have again but with just a PK and FK (neither specified) in the mid-table';
269 my $cd = $schema->resultset('CD')->first;
270 my $track = $schema->resultset ('Track')->recursive_update({
276 { text => 'The color black' },
277 { text => 'The colour black' },
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');
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');
292 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
293 [ 'The color black', 'The colour black' ],
294 'Lyrics text in objects matches',
298 my $lyric = $schema->resultset('Lyrics')->search (
299 { 'track.title' => 'Black' },
303 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
306 [ sort $lyric->lyric_versions->get_column ('text')->all ],
307 [ 'The color black', 'The colour black' ],
308 'Lyrics text via search matches',
314 * Test a multilevel might-have with a PK == FK in the might_have/has_many table
316 CD -> might have -> Artwork
319 --> Artwork_to_Artist
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',
332 artwork_to_artist => [
333 { artist => { name => 'cowboy joe' } },
334 { artist => { name => 'billy the kid' } },
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');
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');
346 [ sort $art_obj->artists->get_column ('name')->all ],
347 [ 'billy the kid', 'cowboy joe' ],
348 'Artists named correctly when queried via object',
351 my $artwork = $schema->resultset('Artwork')->search (
352 { 'cd.title' => 'Music to code by until the cows come home' },
355 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
357 [ sort $artwork->artists->get_column ('name')->all ],
358 [ 'billy the kid', 'cowboy joe' ],
359 'Artists named correctly queried via a new search',
364 diag '* Nested find_or_create';
366 my $newartist2 = $schema->resultset('Artist')->recursive_update({
375 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
379 diag '* Multiple same level has_many create';
381 my $artist2 = $schema->resultset('Artist')->recursive_update({
385 title => 'Music to code by',
391 title => 'Music to code by 1',
392 # original title => 'Music to code by',
398 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
402 diag '* First create_related pass';
404 my $artist = $schema->resultset('Artist')->first;
406 my $cd_result = $schema->resultset('CD')->recursive_update({
408 artist => $artist->artistid,
409 title => 'TestOneCD1',
423 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
424 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
426 my $tracks = $cd_result->tracks;
428 ok( $tracks->isa( "DBIx::Class::ResultSet" ), "Got Expected Tracks ResultSet");
430 foreach my $track ($tracks->all)
432 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
437 diag '* second create_related with same arguments';
439 my $artist = $schema->resultset('Artist')->first;
441 my $cd_result = $schema->resultset('CD')->recursive_update({
443 artist => $artist->artistid,
445 title => 'TestOneCD2',
457 liner_notes => { notes => 'I can haz liner notes?' },
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');
465 my $tracks = $cd_result->tracks;
467 ok( $tracks->isa( "DBIx::Class::ResultSet" ), "Got Expected Tracks ResultSet");
469 foreach my $track ($tracks->all)
471 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
476 diag '* create of parents of a record linker table';
478 my $cdp = $schema->resultset('CD_to_Producer')->recursive_update({
479 cd => { artist => 1, title => 'foo', year => 2000 },
480 producer => { name => 'jorge' }
482 ok($cdp, 'join table record created ok');
487 diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
489 my $new_cd_hashref = {
491 title => 'Boogie Woogie',
493 artist => { artistid => 17, name => 'king luke' }
496 my $cd = $schema->resultset("CD")->find(1);
498 is($cd->artist->id, 1, 'rel okay');
500 my $new_cd = $schema->resultset("CD")->recursive_update($new_cd_hashref);
501 is($new_cd->artist->id, 17, 'new id retained okay');
506 $schema->resultset("CD")->recursive_update({
508 title => 'Boogie Wiggle',
510 artist => { artistid => 18, name => 'larry' }
513 is($@, '', 'new cd created without clash on related artist');
515 diag '* Test multi create over many_to_many';
517 $schema->resultset('CD')->recursive_update({
519 name => 'larry', # should already exist
521 title => 'Warble Marble',
524 { producer => { name => 'Cowboy Neal' } },
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');
534 diag '* And the insane multicreate';
535 # (should work, despite the fact that no one will probably use it this way)
537 # first count how many rows do we initially have
539 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
541 # do the crazy create
543 my $greatest_collections = $schema->resultset('Genre')->create( { name => '"Greatest" collections' } );
544 my $greatest_collections2 = $schema->resultset('Genre')->create( { name => '"Greatest" collections2' } );
546 $schema->resultset('CD')->recursive_update({
550 title => 'Greatest hits 1',
552 genre => $greatest_collections,
568 title => 'Greatest hits 2',
570 genre => $greatest_collections,
575 # This cd is created via artist so it doesn't know about producers
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
583 #{ producer => { name => 'bob' } },
584 { producer => { name => 'paul' } },
593 title => 'Greatest hits 3',
595 genre => $greatest_collections,
602 title => 'Greatest hits 4',
604 genre => $greatest_collections2,
612 title => 'Greatest hits 5',
614 genre => $greatest_collections2,
622 title => 'Greatest hits 6',
624 genre => $greatest_collections,
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
641 title => 'Greatest hits 7',
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');
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');
661 my $cds_2012 = $cd_rs->search ({ year => 2012});
662 is ($cds_2012->count, 5, '5 CDs created in 2012');
666 { 'tags.tag' => { -in => [qw/A B/] } },
667 { join => 'tags', group_by => 'me.cdid' }
670 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
673 my $paul_prod = $cd_rs->search (
674 { 'producer.name' => 'paul'},
675 { join => { cd_to_producer => 'producer' } }
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');
681 $pauls_cd->search_related ('cd_to_producer',
682 { 'producer.name' => 'flemming'},
683 { join => 'producer' }
686 'The second producer is flemming',
689 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
690 is ($kirk_cds, 3, 'Kirk had 3 CDs');
693 { 'cd_to_producer.cd' => { '!=', undef } },
694 { join => 'cd_to_producer' },
697 'Kirk had a producer only on one cd',
700 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
701 is ($lars_cds->count, 3, 'Lars had 3 CDs');
704 { 'cd_to_producer.cd' => undef },
705 { join => 'cd_to_producer' },
708 'Lars always had a producer',
711 $lars_cds->search_related ('cd_to_producer',
712 { 'producer.name' => 'flemming'},
713 { join => 'producer' }
716 'Lars produced 1 CD with flemming',
719 $lars_cds->search_related ('cd_to_producer',
720 { 'producer.name' => 'bob'},
721 { join => 'producer' }
724 'Lars produced 2 CDs with bob',
727 my $bob_prod = $cd_rs->search (
728 { 'producer.name' => 'bob'},
729 { join => { cd_to_producer => 'producer' } }
731 is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
734 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
736 "Bob produced james' only CD",
742 ## Test for the might_have is allowed empty bug (should check and see if this
743 ## needs patching upstream to DBIC
745 use DBIx::Class::ResultSet::RecursiveUpdate;
749 liner_notes => undef,
750 tracks => [{title=>'hello', pos=>'100'}],
751 single_track_row => undef,
754 ok my $might_have_cd_rs = $schema->resultset('CD'), 'got a good resultset';
755 ok my $might_have_cd_row = $might_have_cd_rs->first, 'got cd to test';
757 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
758 resultset => $might_have_cd_rs,
759 updates => $might_have,
760 object => $might_have_cd_row,
763 ok $schema->resultset('Track')->recursive_update($might_have), 'handled might_have';