8 my $schema = DBICTest->init_schema();
10 diag '* simple create + parent (the stuff $rs belongs_to)';
12 my $cd = $schema->resultset('CD')->recursive_update(
13 { artist => { name => 'Fred Bloggs' },
19 isa_ok( $cd, 'DBICTest::CD', 'Created CD object' );
20 isa_ok( $cd->artist, 'DBICTest::Artist', 'Created related Artist' );
21 is( $cd->artist->name, 'Fred Bloggs', 'Artist created correctly' );
26 '* same as above but the child and parent have no values, except for an explicit parent pk';
28 my $bm_rs = $schema->resultset('Bookmark');
29 my $bookmark = $bm_rs->recursive_update( { link => { id => 66, }, } );
31 isa_ok( $bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object' );
32 isa_ok( $bookmark->link, 'DBICTest::Link', 'Created related Link' );
34 { 'link.title' => $bookmark->link->title },
38 'Bookmark and link made it to the DB',
43 diag '* Create m2m while originating in the linker table';
45 my $artist = $schema->resultset('Artist')->first;
46 my $c2p = $schema->resultset('CD_to_Producer')->recursive_update(
49 title => 'Bad investment',
52 { pos => 1, title => 'Just buy' },
53 { pos => 2, title => 'Why did we do it' },
54 { pos => 3, title => 'Burn baby burn' },
57 producer => { name => 'Lehman Bros.', },
61 isa_ok( $c2p, 'DBICTest::CD_to_Producer', 'Linker object created' );
63 $schema->resultset('Producer')->find( { name => 'Lehman Bros.' } );
64 isa_ok( $prod, 'DBICTest::Producer', 'Producer row found' );
65 is( $prod->cds->count, 1, 'Producer has one production' );
66 my $cd = $prod->cds->first;
67 is( $cd->title, 'Bad investment', 'CD created correctly' );
68 is( $cd->tracks->count, 3, 'CD has 3 tracks' );
74 * Create over > 1 levels of might_have with multiple has_many and multiple m2m
75 but starting at a has_many level
77 CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
87 my $artist = $schema->resultset('Artist')->first;
88 my $cd = $schema->resultset('CD')->recursive_update(
90 title => 'Music to code by at night',
93 { pos => 1, # some day me might test this with Ordered
94 title => 'Off by one again',
97 title => 'The dereferencer',
101 title => 'Was that a null (Single)',
103 { title => 'The dereferencer', pos => 1 },
104 { title => 'The dereferencer II', pos => 2 },
107 { producer => { name => 'K&R', } },
108 { producer => { name => 'Don Knuth', } },
116 isa_ok( $cd, 'DBICTest::CD', 'Main CD object created' );
117 is( $cd->title, 'Music to code by at night', 'Correct CD title' );
118 is( $cd->tracks->count, 2, 'Two tracks on main CD' );
120 my ( $t1, $t2 ) = $cd->tracks->all;
121 is( $t1->title, 'Off by one again', 'Correct 1st track name' );
122 is( $t1->cd_single, undef, 'No single for 1st track' );
123 is( $t2->title, 'The dereferencer', 'Correct 2nd track name' );
124 isa_ok( $t2->cd_single, 'DBICTest::CD',
125 'Created a single for 2nd track' );
127 my $single = $t2->cd_single;
128 is( $single->tracks->count, 2, 'Two tracks on single CD' );
129 is( $single->tracks->find( { position => 1 } )->title,
131 'Correct 1st track title'
133 is( $single->tracks->find( { position => 2 } )->title,
134 'The dereferencer II',
135 'Correct 2nd track title'
138 is( $single->cd_to_producer->count,
139 2, 'Two producers created for the single cd' );
141 [ sort map { $_->producer->name } ( $single->cd_to_producer->all ) ],
142 [ 'Don Knuth', 'K&R' ],
143 'Producers named correctly',
149 * Same as above but starting at the might_have directly
151 Track -> might have -> Single -> has_many -> Tracks
161 my $cd = $schema->resultset('CD')->first;
162 my $track = $schema->resultset('Track')->recursive_update(
164 pos => 77, # some day me might test this with Ordered
165 title => 'Multicreate rocks',
167 artist => $cd->artist,
169 title => 'Disemboweling MultiCreate',
171 { title => 'Why does mst write this way', pos => 1 },
172 { title => 'Chainsaw celebration', pos => 2 },
173 { title => 'Purl cleans up', pos => 3 },
176 { producer => { name => 'mst', } },
177 { producer => { name => 'castaway', } },
178 { producer => { name => 'theorbtwo', } },
184 isa_ok( $track, 'DBICTest::Track', 'Main Track object created' );
185 is( $track->title, 'Multicreate rocks', 'Correct Track title' );
187 my $single = $track->cd_single;
188 isa_ok( $single, 'DBICTest::CD', 'Created a single with the track' );
189 is( $single->tracks->count, 3, '3 tracks on single CD' );
190 is( $single->tracks->find( { position => 1 } )->title,
191 'Why does mst write this way',
192 'Correct 1st track title'
194 is( $single->tracks->find( { position => 2 } )->title,
195 'Chainsaw celebration',
196 'Correct 2nd track title'
198 is( $single->tracks->find( { position => 3 } )->title,
200 'Correct 3rd track title'
203 is( $single->cd_to_producer->count,
204 3, '3 producers created for the single cd' );
206 [ sort map { $_->producer->name } ( $single->cd_to_producer->all ) ],
207 [ 'castaway', 'mst', 'theorbtwo' ],
208 'Producers named correctly',
214 '* Test might_have again but with a PK == FK in the middle (obviously not specified)';
216 my $artist = $schema->resultset('Artist')->first;
217 my $cd = $schema->resultset('CD')->recursive_update(
219 title => 'Music to code by at twilight',
223 { name => 'recursive descent' },
224 { name => 'tail packing' },
230 isa_ok( $cd, 'DBICTest::CD', 'Main CD object created' );
231 is( $cd->title, 'Music to code by at twilight', 'Correct CD title' );
232 isa_ok( $cd->artwork, 'DBICTest::Artwork', 'Artwork created' );
234 # this test might look weird, but it failed at one point, keep it there
235 my $art_obj = $cd->artwork;
236 ok( $art_obj->has_column_loaded('cd_id'),
237 'PK/FK present on artwork object'
239 is( $art_obj->images->count, 2,
240 'Correct artwork image count via the new object' );
242 [ sort $art_obj->images->get_column('name')->all ],
243 [ 'recursive descent', 'tail packing' ],
244 'Images named correctly in objects',
247 my $artwork = $schema->resultset('Artwork')->search(
248 { 'cd.title' => 'Music to code by at twilight' },
252 is( $artwork->images->count, 2,
253 'Correct artwork image count via a new search' );
256 [ sort $artwork->images->get_column('name')->all ],
257 [ 'recursive descent', 'tail packing' ],
258 'Images named correctly after search',
264 '* Test might_have again but with just a PK and FK (neither specified) in the mid-table';
266 my $cd = $schema->resultset('CD')->first;
267 my $track = $schema->resultset('Track')->recursive_update(
273 { text => 'The color black' },
274 { text => 'The colour black' },
280 isa_ok( $track, 'DBICTest::Track', 'Main track object created' );
281 is( $track->title, 'Black', 'Correct track title' );
282 isa_ok( $track->lyrics, 'DBICTest::Lyrics', 'Lyrics created' );
284 # this test might look weird, but it was failing at one point, keep it there
285 my $lyric_obj = $track->lyrics;
286 ok( $lyric_obj->has_column_loaded('lyric_id'),
287 'PK present on lyric object' );
288 ok( $lyric_obj->has_column_loaded('track_id'),
289 'FK present on lyric object' );
290 is( $lyric_obj->lyric_versions->count,
291 2, 'Correct lyric versions count via the new object' );
293 [ sort $lyric_obj->lyric_versions->get_column('text')->all ],
294 [ 'The color black', 'The colour black' ],
295 'Lyrics text in objects matches',
299 $schema->resultset('Lyrics')
300 ->search( { 'track.title' => 'Black' }, { join => 'track' }, )
303 is( $lyric->lyric_versions->count,
304 2, 'Correct lyric versions count via a new search' );
307 [ sort $lyric->lyric_versions->get_column('text')->all ],
308 [ 'The color black', 'The colour black' ],
309 'Lyrics text via search matches',
315 * Test a multilevel might-have with a PK == FK in the might_have/has_many table
317 CD -> might have -> Artwork
320 --> Artwork_to_Artist
327 my $someartist = $schema->resultset('Artist')->first;
328 my $cd = $schema->resultset('CD')->recursive_update(
329 { artist => $someartist,
330 title => 'Music to code by until the cows come home',
333 artwork_to_artist => [
334 { artist => { name => 'cowboy joe' } },
335 { artist => { name => 'billy the kid' } },
341 isa_ok( $cd, 'DBICTest::CD', 'Main CD object created' );
343 'Music to code by until the cows come home',
347 my $art_obj = $cd->artwork;
348 ok( $art_obj->has_column_loaded('cd_id'),
349 'PK/FK present on artwork object'
351 is( $art_obj->artists->count, 2,
352 'Correct artwork creator count via the new object' );
354 [ sort $art_obj->artists->get_column('name')->all ],
355 [ 'billy the kid', 'cowboy joe' ],
356 'Artists named correctly when queried via object',
359 my $artwork = $schema->resultset('Artwork')->search(
360 { 'cd.title' => 'Music to code by until the cows come home' },
363 is( $artwork->artists->count, 2,
364 'Correct artwork creator count via a new search' );
366 [ sort $artwork->artists->get_column('name')->all ],
367 [ 'billy the kid', 'cowboy joe' ],
368 'Artists named correctly queried via a new search',
373 diag '* Nested find_or_create';
375 my $newartist2 = $schema->resultset('Artist')->recursive_update(
378 { title => 'Noah Act',
384 is( $newartist2->name, 'Fred 3',
385 'Created new artist with cds via find_or_create' );
389 diag '* Multiple same level has_many create';
391 my $artist2 = $schema->resultset('Artist')->recursive_update(
394 { title => 'Music to code by',
399 { title => 'Music to code by 1',
401 # original title => 'Music to code by',
408 is( $artist2->in_storage, 1, 'artist with duplicate rels inserted okay' );
412 diag '* First create_related pass';
414 my $artist = $schema->resultset('Artist')->first;
416 my $cd_result = $schema->resultset('CD')->recursive_update(
419 artist => $artist->artistid,
420 title => 'TestOneCD1',
435 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class" );
436 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title" );
438 my $tracks = $cd_result->tracks;
440 ok( $tracks->isa("DBIx::Class::ResultSet"),
441 "Got Expected Tracks ResultSet"
444 foreach my $track ( $tracks->all ) {
445 ok( $track && ref $track eq 'DBICTest::Track',
446 'Got Expected Track Class' );
451 diag '* second create_related with same arguments';
453 my $artist = $schema->resultset('Artist')->first;
455 my $cd_result = $schema->resultset('CD')->recursive_update(
458 artist => $artist->artistid,
460 title => 'TestOneCD2',
472 liner_notes => { notes => 'I can haz liner notes?' },
477 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class" );
478 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title" );
479 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes' );
481 my $tracks = $cd_result->tracks;
483 ok( $tracks->isa("DBIx::Class::ResultSet"),
484 "Got Expected Tracks ResultSet"
487 foreach my $track ( $tracks->all ) {
488 ok( $track && ref $track eq 'DBICTest::Track',
489 'Got Expected Track Class' );
494 diag '* create of parents of a record linker table';
496 my $cdp = $schema->resultset('CD_to_Producer')->recursive_update(
497 { cd => { artist => 1, title => 'foo', year => 2000 },
498 producer => { name => 'jorge' }
501 ok( $cdp, 'join table record created ok' );
506 '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
508 my $new_cd_hashref = {
510 title => 'Boogie Woogie',
512 artist => { artistid => 17, name => 'king luke' }
515 my $cd = $schema->resultset("CD")->find(1);
517 is( $cd->artist->id, 1, 'rel okay' );
519 my $new_cd = $schema->resultset("CD")->recursive_update($new_cd_hashref);
520 is( $new_cd->artist->id, 17, 'new id retained okay' );
525 $schema->resultset("CD")->recursive_update(
527 title => 'Boogie Wiggle',
529 artist => { artistid => 18, name => 'larry' }
533 is( $@, '', 'new cd created without clash on related artist' );
535 diag '* Test multi create over many_to_many';
537 $schema->resultset('CD')->recursive_update(
539 name => 'larry', # should already exist
541 title => 'Warble Marble',
543 cd_to_producer => [ { producer => { name => 'Cowboy Neal' } }, ],
548 $schema->resultset('CD')->search( { title => 'Warble Marble' } );
549 is( $m2m_cd->count, 1, 'One CD row created via M2M create' );
550 is( $m2m_cd->first->producers->count,
551 1, 'CD row created with one producer' );
552 is( $m2m_cd->first->producers->first->name,
553 'Cowboy Neal', 'Correct producer row created' );
556 diag '* And the insane multicreate';
558 # (should work, despite the fact that no one will probably use it this way)
560 # first count how many rows do we initially have
562 $counts->{$_} = $schema->resultset($_)->count
563 for qw/Artist CD Genre Producer Tag/;
565 # do the crazy create
567 my $greatest_collections =
568 $schema->resultset('Genre')
569 ->create( { name => '"Greatest" collections' } );
570 my $greatest_collections2 =
571 $schema->resultset('Genre')
572 ->create( { name => '"Greatest" collections2' } );
574 $schema->resultset('CD')->recursive_update(
575 { artist => { name => 'james', },
576 title => 'Greatest hits 1',
578 genre => $greatest_collections,
579 tags => [ { tag => 'A' }, { tag => 'B' }, ],
588 { title => 'Greatest hits 2',
591 $greatest_collections,
597 # This cd is created via artist so it doesn't know about producers
600 # if we specify 'bob' here things bomb
601 # as the producer attached to Greatest Hits 1 is
602 # already created, but not yet inserted.
603 # Maybe this can be fixed, but things are hairy
606 #{ producer => { name => 'bob' } },
628 $greatest_collections,
648 $greatest_collections2,
671 $greatest_collections2,
681 title => 'Greatest hits 6',
683 genre => $greatest_collections,
694 # in recursive_update this creates a new artist - since no id provided
695 # in original create -
696 # should already exist
697 # even though the artist 'name' is not uniquely constrained
698 # find_or_create will arguably DWIM
700 title => 'Greatest hits 7',
711 is( $schema->resultset('Artist')->count,
712 $counts->{Artist} + 4,
713 '4 new artists created'
715 is( $schema->resultset('Genre')->count,
716 $counts->{Genre} + 2,
717 '2 additional genres created'
719 is( $schema->resultset('Producer')->count,
720 $counts->{Producer} + 3,
723 is( $schema->resultset('CD')->count, $counts->{CD} + 7, '7 new CDs' );
724 is( $schema->resultset('Tag')->count, $counts->{Tag} + 10,
728 $schema->resultset('CD')
729 ->search( { title => { -like => 'Greatest hits %' } },
730 { order_by => 'title' } );
731 is( $cd_rs->count, 7, '7 greatest hits created' );
733 my $cds_2012 = $cd_rs->search( { year => 2012 } );
734 is( $cds_2012->count, 5, '5 CDs created in 2012' );
736 is( $cds_2012->search(
737 { 'tags.tag' => { -in => [qw/A B/] } },
738 { join => 'tags', group_by => 'me.cdid' }
741 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
744 my $paul_prod = $cd_rs->search( { 'producer.name' => 'paul' },
745 { join => { cd_to_producer => 'producer' } } );
746 is( $paul_prod->count, 1, 'Paul had 1 production' );
747 my $pauls_cd = $paul_prod->single;
748 is( $pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer' );
749 is( $pauls_cd->search_related(
751 { 'producer.name' => 'flemming' },
752 { join => 'producer' }
755 'The second producer is flemming',
759 $cd_rs->search( { 'artist.name' => 'kirk' }, { join => 'artist' } );
760 is( $kirk_cds, 3, 'Kirk had 3 CDs' );
761 is( $kirk_cds->search(
762 { 'cd_to_producer.cd' => { '!=', undef } },
763 { join => 'cd_to_producer' },
766 'Kirk had a producer only on one cd',
770 $cd_rs->search( { 'artist.name' => 'lars' }, { join => 'artist' } );
771 is( $lars_cds->count, 3, 'Lars had 3 CDs' );
772 is( $lars_cds->search(
773 { 'cd_to_producer.cd' => undef },
774 { join => 'cd_to_producer' },
777 'Lars always had a producer',
779 is( $lars_cds->search_related(
781 { 'producer.name' => 'flemming' },
782 { join => 'producer' }
785 'Lars produced 1 CD with flemming',
787 is( $lars_cds->search_related(
789 { 'producer.name' => 'bob' },
790 { join => 'producer' }
793 'Lars produced 2 CDs with bob',
796 my $bob_prod = $cd_rs->search( { 'producer.name' => 'bob' },
797 { join => { cd_to_producer => 'producer' } } );
798 is( $bob_prod->count, 3, 'Bob produced a total of 3 CDs' );
800 is( $bob_prod->search( { 'artist.name' => 'james' },
801 { join => 'artist' } )->count,
803 "Bob produced james' only CD",
808 ## Test for the might_have is allowed empty bug (should check and see if this
809 ## needs patching upstream to DBIC
811 use DBIx::Class::ResultSet::RecursiveUpdate;
815 liner_notes => undef,
816 tracks => [ { title => 'hello', pos => '100' } ],
817 single_track_row => undef,
820 ok my $might_have_cd_rs = $schema->resultset('CD'), 'got a good resultset';
821 ok my $might_have_cd_row = $might_have_cd_rs->first, 'got cd to test';
823 my $track = $schema->resultset('Track')->next;
824 $might_have_cd_row->single_track_row($track);
825 $might_have_cd_row->update;
827 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
828 resultset => $might_have_cd_rs,
829 updates => $might_have,
830 object => $might_have_cd_row,
833 is( $might_have_cd_row->single_track, undef, 'Might have deleted' );