9 sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} };
13 my $schema = DBICTest->init_schema();
15 mc_diag '* simple create + parent (the stuff $rs belongs_to)';
17 my $cd = $schema->resultset('CD')->create({
25 isa_ok($cd, 'DBICTest::CD', 'Created CD object');
26 isa_ok($cd->artist, 'DBICTest::Artist', 'Created related Artist');
27 is($cd->artist->name, 'Fred Bloggs', 'Artist created correctly');
31 mc_diag '* same as above but the child and parent have no values, except for an explicit parent pk';
33 my $bm_rs = $schema->resultset('Bookmark');
34 my $bookmark = $bm_rs->create({
40 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
41 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
44 { 'link.title' => $bookmark->link->title },
48 'Bookmark and link made it to the DB',
53 mc_diag '* create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )';
55 my $artist = $schema->resultset('Artist')->first;
56 my $cd = $artist->create_related (cds => {
57 title => 'Music to code by',
64 isa_ok($cd, 'DBICTest::CD', 'Created CD');
65 is($cd->title, 'Music to code by', 'CD created correctly');
66 is($cd->tags->count, 1, 'One tag created for CD');
67 is($cd->tags->first->tag, 'rock', 'Tag created correctly');
74 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
75 $schema->resultset('Artist')->first->update({
77 { title => 'Yet another CD',
83 qr/Recursive update is not supported over relationships of type multi/,
84 'create via update of multi relationships throws an exception'
87 mc_diag '* Create m2m while originating in the linker table';
89 my $artist = $schema->resultset('Artist')->first;
90 my $c2p = $schema->resultset('CD_to_Producer')->create ({
93 title => 'Bad investment',
96 { title => 'Just buy' },
97 { title => 'Why did we do it' },
98 { title => 'Burn baby burn' },
102 name => 'Lehman Bros.',
106 isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created');
107 my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' });
108 isa_ok ($prod, 'DBICTest::Producer', 'Producer row found');
109 is ($prod->cds->count, 1, 'Producer has one production');
110 my $cd = $prod->cds->first;
111 is ($cd->title, 'Bad investment', 'CD created correctly');
112 is ($cd->tracks->count, 3, 'CD has 3 tracks');
118 * Create over > 1 levels of might_have with multiple has_many and multiple m2m
119 but starting at a has_many level
121 CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
131 my $artist = $schema->resultset('Artist')->first;
132 my $cd = $schema->resultset('CD')->create ({
134 title => 'Music to code by at night',
138 title => 'Off by one again',
141 title => 'The dereferencer',
145 title => 'Was that a null (Single)',
147 { title => 'The dereferencer' },
148 { title => 'The dereferencer II' },
167 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
168 is ($cd->title, 'Music to code by at night', 'Correct CD title');
169 is ($cd->tracks->count, 2, 'Two tracks on main CD');
171 my ($t1, $t2) = $cd->tracks->all;
172 is ($t1->title, 'Off by one again', 'Correct 1st track name');
173 is ($t1->cd_single, undef, 'No single for 1st track');
174 is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
175 isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
177 my $single = $t2->cd_single;
178 is ($single->tracks->count, 2, 'Two tracks on single CD');
179 is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
180 is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
182 is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
184 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
185 ['Don Knuth', 'K&R'],
186 'Producers named correctly',
192 * Same as above but starting at the might_have directly
194 Track -> might have -> Single -> has_many -> Tracks
204 my $cd = $schema->resultset('CD')->first;
205 my $track = $schema->resultset('Track')->create ({
207 title => 'Multicreate rocks',
209 artist => $cd->artist,
211 title => 'Disemboweling MultiCreate',
213 { title => 'Why does mst write this way' },
214 { title => 'Chainsaw celebration' },
215 { title => 'Purl cleans up' },
237 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
238 is ($track->title, 'Multicreate rocks', 'Correct Track title');
240 my $single = $track->cd_single;
241 isa_ok ($single, 'DBICTest::CD', 'Created a single with the track');
242 is ($single->tracks->count, 3, '3 tracks on single CD');
243 is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title');
244 is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title');
245 is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title');
247 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
249 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
250 ['castaway', 'mst', 'theorbtwo'],
251 'Producers named correctly',
256 mc_diag '* Test might_have again but with a PK == FK in the middle (obviously not specified)';
258 my $artist = $schema->resultset('Artist')->first;
259 my $cd = $schema->resultset('CD')->create ({
261 title => 'Music to code by at twilight',
265 { name => 'recursive descent' },
266 { name => 'tail packing' },
271 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
272 is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
273 isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
275 # this test might look weird, but it failed at one point, keep it there
276 my $art_obj = $cd->artwork;
277 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
278 is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
280 [ sort $art_obj->images->get_column ('name')->all ],
281 [ 'recursive descent', 'tail packing' ],
282 'Images named correctly in objects',
285 my $artwork = $schema->resultset('Artwork')->search (
286 { 'cd.title' => 'Music to code by at twilight' },
290 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
293 [ sort $artwork->images->get_column ('name')->all ],
294 [ 'recursive descent', 'tail packing' ],
295 'Images named correctly after search',
300 mc_diag '* Test might_have again but with just a PK and FK (neither specified) in the mid-table';
302 my $cd = $schema->resultset('CD')->first;
303 my $track = $schema->resultset ('Track')->create ({
308 { text => 'The color black' },
309 { text => 'The colour black' },
314 isa_ok ($track, 'DBICTest::Track', 'Main track object created');
315 is ($track->title, 'Black', 'Correct track title');
316 isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
318 # this test might look weird, but it was failing at one point, keep it there
319 my $lyric_obj = $track->lyrics;
320 ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
321 ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
322 is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
324 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
325 [ 'The color black', 'The colour black' ],
326 'Lyrics text in objects matches',
330 my $lyric = $schema->resultset('Lyrics')->search (
331 { 'track.title' => 'Black' },
335 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
338 [ sort $lyric->lyric_versions->get_column ('text')->all ],
339 [ 'The color black', 'The colour black' ],
340 'Lyrics text via search matches',
346 * Test a multilevel might-have with a PK == FK in the might_have/has_many table
348 CD -> might have -> Artwork
351 --> Artwork_to_Artist
358 my $someartist = $schema->resultset('Artist')->first;
359 my $cd = $schema->resultset('CD')->create ({
360 artist => $someartist,
361 title => 'Music to code by until the cows come home',
364 artwork_to_artist => [
365 { artist => { name => 'cowboy joe' } },
366 { artist => { name => 'billy the kid' } },
371 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
372 is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title');
374 my $art_obj = $cd->artwork;
375 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
376 is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object');
378 [ sort $art_obj->artists->get_column ('name')->all ],
379 [ 'billy the kid', 'cowboy joe' ],
380 'Artists named correctly when queried via object',
383 my $artwork = $schema->resultset('Artwork')->search (
384 { 'cd.title' => 'Music to code by until the cows come home' },
387 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
389 [ sort $artwork->artists->get_column ('name')->all ],
390 [ 'billy the kid', 'cowboy joe' ],
391 'Artists named correctly queried via a new search',
396 mc_diag '* Nested find_or_create';
398 my $newartist2 = $schema->resultset('Artist')->find_or_create({
407 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
411 mc_diag '* Multiple same level has_many create';
413 my $artist2 = $schema->resultset('Artist')->create({
417 title => 'Music to code by',
423 title => 'Music to code by',
429 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
433 mc_diag '* First create_related pass';
435 my $artist = $schema->resultset('Artist')->first;
437 my $cd_result = $artist->create_related('cds', {
439 title => 'TestOneCD1',
442 { title => 'TrackOne' },
443 { title => 'TrackTwo' },
448 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
449 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
451 my $tracks = $cd_result->tracks;
453 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
455 foreach my $track ($tracks->all)
457 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
462 mc_diag '* second create_related with same arguments';
464 my $artist = $schema->resultset('Artist')->first;
466 my $cd_result = $artist->create_related('cds', {
468 title => 'TestOneCD2',
471 { title => 'TrackOne' },
472 { title => 'TrackTwo' },
475 liner_notes => { notes => 'I can haz liner notes?' },
479 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
480 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
481 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
483 my $tracks = $cd_result->tracks;
485 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
487 foreach my $track ($tracks->all)
489 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
494 mc_diag '* create of parents of a record linker table';
496 my $cdp = $schema->resultset('CD_to_Producer')->create({
497 cd => { artist => 1, title => 'foo', year => 2000 },
498 producer => { name => 'jorge' }
500 ok($cdp, 'join table record created ok');
505 my $kurt_cobain = { name => 'Kurt Cobain' };
507 my $in_utero = $schema->resultset('CD')->new({
512 $kurt_cobain->{cds} = [ $in_utero ];
515 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
516 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
518 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
519 is($a->cds && $a->cds->first && $a->cds->first->title,
520 'In Utero', 'CD insertion ok');
525 # This test case has been moved to t/96multi_create/cd_single.t
527 my $pink_floyd = { name => 'Pink Floyd' };
529 my $the_wall = { title => 'The Wall', year => 1979 };
531 $pink_floyd->{cds} = [ $the_wall ];
534 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
535 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
537 is($a->name, 'Pink Floyd', 'Artist insertion ok');
538 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
543 mc_diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
544 ## Create foreign key col obj including PK
545 ## See test 20 in 66relationships.t
547 my $new_cd_hashref = {
549 title => 'Boogie Woogie',
551 artist => { artistid => 17, name => 'king luke' }
554 my $cd = $schema->resultset("CD")->find(1);
556 is($cd->artist->id, 1, 'rel okay');
558 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
559 is($new_cd->artist->id, 17, 'new id retained okay');
564 $schema->resultset("CD")->create({
566 title => 'Boogie Wiggle',
568 artist => { artistid => 18, name => 'larry' }
571 is($@, '', 'new cd created without clash on related artist');
573 mc_diag '* Make sure exceptions from errors in created rels propogate';
575 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
576 #$t->cd($t->new_related('cd', { artist => undef } ) );
577 #$t->{_rel_in_storage} = 0;
580 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
582 mc_diag '* Test multi create over many_to_many';
584 $schema->resultset('CD')->create ({
586 name => 'larry', # should already exist
588 title => 'Warble Marble',
591 { producer => { name => 'Cowboy Neal' } },
595 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
596 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
597 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
598 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');