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',
345 mc_diag '* Nested find_or_create';
347 my $newartist2 = $schema->resultset('Artist')->find_or_create({
356 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
360 mc_diag '* Multiple same level has_many create';
362 my $artist2 = $schema->resultset('Artist')->create({
366 title => 'Music to code by',
372 title => 'Music to code by',
378 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
382 mc_diag '* First create_related pass';
384 my $artist = $schema->resultset('Artist')->first;
386 my $cd_result = $artist->create_related('cds', {
388 title => 'TestOneCD1',
391 { title => 'TrackOne' },
392 { title => 'TrackTwo' },
397 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
398 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
400 my $tracks = $cd_result->tracks;
402 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
404 foreach my $track ($tracks->all)
406 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
411 mc_diag '* second create_related with same arguments';
413 my $artist = $schema->resultset('Artist')->first;
415 my $cd_result = $artist->create_related('cds', {
417 title => 'TestOneCD2',
420 { title => 'TrackOne' },
421 { title => 'TrackTwo' },
424 liner_notes => { notes => 'I can haz liner notes?' },
428 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
429 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
430 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
432 my $tracks = $cd_result->tracks;
434 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
436 foreach my $track ($tracks->all)
438 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
443 mc_diag '* create of parents of a record linker table';
445 my $cdp = $schema->resultset('CD_to_Producer')->create({
446 cd => { artist => 1, title => 'foo', year => 2000 },
447 producer => { name => 'jorge' }
449 ok($cdp, 'join table record created ok');
454 my $kurt_cobain = { name => 'Kurt Cobain' };
456 my $in_utero = $schema->resultset('CD')->new({
461 $kurt_cobain->{cds} = [ $in_utero ];
464 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
465 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
467 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
468 is($a->cds && $a->cds->first && $a->cds->first->title,
469 'In Utero', 'CD insertion ok');
474 # This test case has been moved to t/96multi_create/cd_single.t
476 my $pink_floyd = { name => 'Pink Floyd' };
478 my $the_wall = { title => 'The Wall', year => 1979 };
480 $pink_floyd->{cds} = [ $the_wall ];
483 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
484 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
486 is($a->name, 'Pink Floyd', 'Artist insertion ok');
487 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
492 mc_diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
493 ## Create foreign key col obj including PK
494 ## See test 20 in 66relationships.t
496 my $new_cd_hashref = {
498 title => 'Boogie Woogie',
500 artist => { artistid => 17, name => 'king luke' }
503 my $cd = $schema->resultset("CD")->find(1);
505 is($cd->artist->id, 1, 'rel okay');
507 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
508 is($new_cd->artist->id, 17, 'new id retained okay');
513 $schema->resultset("CD")->create({
515 title => 'Boogie Wiggle',
517 artist => { artistid => 18, name => 'larry' }
520 is($@, '', 'new cd created without clash on related artist');
522 mc_diag '* Make sure exceptions from errors in created rels propogate';
524 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
525 #$t->cd($t->new_related('cd', { artist => undef } ) );
526 #$t->{_rel_in_storage} = 0;
529 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
531 mc_diag '* Test multi create over many_to_many';
533 $schema->resultset('CD')->create ({
535 name => 'larry', # should already exist
537 title => 'Warble Marble',
540 { producer => { name => 'Cowboy Neal' } },
544 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
545 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
546 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
547 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');