11 my $schema = DBICTest->init_schema();
13 diag '* simple create + parent (the stuff $rs belongs_to)';
15 my $cd = $schema->resultset('CD')->create({
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->create({
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 over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )';
53 my $artist = $schema->resultset('Artist')->first;
54 my $cd = $artist->create_related (cds => {
55 title => 'Music to code by',
62 isa_ok($cd, 'DBICTest::CD', 'Created CD');
63 is($cd->title, 'Music to code by', 'CD created correctly');
64 is($cd->tags->count, 1, 'One tag created for CD');
65 is($cd->tags->first->tag, 'rock', 'Tag created correctly');
72 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
73 $schema->resultset('Artist')->first->update({
75 { title => 'Yet another CD',
81 qr/Recursive update is not supported over relationships of type multi/,
82 'create via update of multi relationships throws an exception'
85 diag '* Create m2m while originating in the linker table';
87 my $artist = $schema->resultset('Artist')->first;
88 my $c2p = $schema->resultset('CD_to_Producer')->create ({
91 title => 'Bad investment',
94 { title => 'Just buy' },
95 { title => 'Why did we do it' },
96 { title => 'Burn baby burn' },
100 name => 'Lehman Bros.',
104 isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created');
105 my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' });
106 isa_ok ($prod, 'DBICTest::Producer', 'Producer row found');
107 is ($prod->cds->count, 1, 'Producer has one production');
108 my $cd = $prod->cds->first;
109 is ($cd->title, 'Bad investment', 'CD created correctly');
110 is ($cd->tracks->count, 3, 'CD has 3 tracks');
116 * Create over > 1 levels of might_have with multiple has_many and multiple m2m
117 but starting at a has_many level
119 CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
129 my $artist = $schema->resultset('Artist')->first;
130 my $cd = $schema->resultset('CD')->create ({
132 title => 'Music to code by at night',
136 title => 'Off by one again',
139 title => 'The dereferencer',
143 title => 'Was that a null (Single)',
145 { title => 'The dereferencer' },
146 { title => 'The dereferencer II' },
165 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
166 is ($cd->title, 'Music to code by at night', 'Correct CD title');
167 is ($cd->tracks->count, 2, 'Two tracks on main CD');
169 my ($t1, $t2) = $cd->tracks->all;
170 is ($t1->title, 'Off by one again', 'Correct 1st track name');
171 is ($t1->cd_single, undef, 'No single for 1st track');
172 is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
173 isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
175 my $single = $t2->cd_single;
176 is ($single->tracks->count, 2, 'Two tracks on single CD');
177 is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
178 is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
180 is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
182 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
183 ['Don Knuth', 'K&R'],
184 'Producers named correctly',
190 * Same as above but starting at the might_have directly
192 Track -> might have -> Single -> has_many -> Tracks
202 my $cd = $schema->resultset('CD')->first;
203 my $track = $schema->resultset('Track')->create ({
205 title => 'Multicreate rocks',
207 artist => $cd->artist,
209 title => 'Disemboweling MultiCreate',
211 { title => 'Why does mst write this way' },
212 { title => 'Chainsaw celebration' },
213 { title => 'Purl cleans up' },
235 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
236 is ($track->title, 'Multicreate rocks', 'Correct Track title');
238 my $single = $track->cd_single;
239 isa_ok ($single, 'DBICTest::CD', 'Created a single with the track');
240 is ($single->tracks->count, 3, '3 tracks on single CD');
241 is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title');
242 is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title');
243 is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title');
245 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
247 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
248 ['castaway', 'mst', 'theorbtwo'],
249 'Producers named correctly',
254 diag '* Test might_have again but with a PK == FK in the middle (obviously not specified)';
256 my $artist = $schema->resultset('Artist')->first;
257 my $cd = $schema->resultset('CD')->create ({
259 title => 'Music to code by at twilight',
263 { name => 'recursive descent' },
264 { name => 'tail packing' },
269 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
270 is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
271 isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
273 # this test might look weird, but it failed at one point, keep it there
274 my $art_obj = $cd->artwork;
275 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
276 is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
278 [ sort $art_obj->images->get_column ('name')->all ],
279 [ 'recursive descent', 'tail packing' ],
280 'Images named correctly in objects',
283 my $artwork = $schema->resultset('Artwork')->search (
284 { 'cd.title' => 'Music to code by at twilight' },
288 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
291 [ sort $artwork->images->get_column ('name')->all ],
292 [ 'recursive descent', 'tail packing' ],
293 'Images named correctly after search',
298 diag '* Test might_have again but with just a PK and FK (neither specified) in the mid-table';
300 my $cd = $schema->resultset('CD')->first;
301 my $track = $schema->resultset ('Track')->create ({
306 { text => 'The color black' },
307 { text => 'The colour black' },
312 isa_ok ($track, 'DBICTest::Track', 'Main track object created');
313 is ($track->title, 'Black', 'Correct track title');
314 isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
316 # this test might look weird, but it was failing at one point, keep it there
317 my $lyric_obj = $track->lyrics;
318 ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
319 ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
320 is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
322 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
323 [ 'The color black', 'The colour black' ],
324 'Lyrics text in objects matches',
328 my $lyric = $schema->resultset('Lyrics')->search (
329 { 'track.title' => 'Black' },
333 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
336 [ sort $lyric->lyric_versions->get_column ('text')->all ],
337 [ 'The color black', 'The colour black' ],
338 'Lyrics text via search matches',
344 * Test a multilevel might-have with a PK == FK in the might_have/has_many table
346 CD -> might have -> Artwork
349 --> Artwork_to_Artist
356 my $someartist = $schema->resultset('Artist')->first;
357 my $cd = $schema->resultset('CD')->create ({
358 artist => $someartist,
359 title => 'Music to code by until the cows come home',
362 artwork_to_artist => [
363 { artist => { name => 'cowboy joe' } },
364 { artist => { name => 'billy the kid' } },
369 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
370 is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title');
372 my $art_obj = $cd->artwork;
373 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
374 is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object');
376 [ sort $art_obj->artists->get_column ('name')->all ],
377 [ 'billy the kid', 'cowboy joe' ],
378 'Artists named correctly when queried via object',
381 my $artwork = $schema->resultset('Artwork')->search (
382 { 'cd.title' => 'Music to code by until the cows come home' },
385 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
387 [ sort $artwork->artists->get_column ('name')->all ],
388 [ 'billy the kid', 'cowboy joe' ],
389 'Artists named correctly queried via a new search',
394 diag '* Nested find_or_create';
396 my $newartist2 = $schema->resultset('Artist')->find_or_create({
405 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
409 diag '* Multiple same level has_many create';
411 my $artist2 = $schema->resultset('Artist')->create({
415 title => 'Music to code by',
421 title => 'Music to code by',
427 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
431 diag '* First create_related pass';
433 my $artist = $schema->resultset('Artist')->first;
435 my $cd_result = $artist->create_related('cds', {
437 title => 'TestOneCD1',
440 { title => 'TrackOne' },
441 { title => 'TrackTwo' },
446 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
447 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
449 my $tracks = $cd_result->tracks;
451 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
453 foreach my $track ($tracks->all)
455 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
460 diag '* second create_related with same arguments';
462 my $artist = $schema->resultset('Artist')->first;
464 my $cd_result = $artist->create_related('cds', {
466 title => 'TestOneCD2',
469 { title => 'TrackOne' },
470 { title => 'TrackTwo' },
473 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( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
485 foreach my $track ($tracks->all)
487 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
492 diag '* create of parents of a record linker table';
494 my $cdp = $schema->resultset('CD_to_Producer')->create({
495 cd => { artist => 1, title => 'foo', year => 2000 },
496 producer => { name => 'jorge' }
498 ok($cdp, 'join table record created ok');
503 my $kurt_cobain = { name => 'Kurt Cobain' };
505 my $in_utero = $schema->resultset('CD')->new({
510 $kurt_cobain->{cds} = [ $in_utero ];
513 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
514 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
516 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
517 is($a->cds && $a->cds->first && $a->cds->first->title,
518 'In Utero', 'CD insertion ok');
523 # This test case has been moved to t/96multi_create/cd_single.t
525 my $pink_floyd = { name => 'Pink Floyd' };
527 my $the_wall = { title => 'The Wall', year => 1979 };
529 $pink_floyd->{cds} = [ $the_wall ];
532 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
533 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
535 is($a->name, 'Pink Floyd', 'Artist insertion ok');
536 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
541 diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
542 ## Create foreign key col obj including PK
543 ## See test 20 in 66relationships.t
545 my $new_cd_hashref = {
547 title => 'Boogie Woogie',
549 artist => { artistid => 17, name => 'king luke' }
552 my $cd = $schema->resultset("CD")->find(1);
554 is($cd->artist->id, 1, 'rel okay');
556 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
557 is($new_cd->artist->id, 17, 'new id retained okay');
562 $schema->resultset("CD")->create({
564 title => 'Boogie Wiggle',
566 artist => { artistid => 18, name => 'larry' }
569 is($@, '', 'new cd created without clash on related artist');
571 diag '* Make sure exceptions from errors in created rels propogate';
573 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
574 #$t->cd($t->new_related('cd', { artist => undef } ) );
575 #$t->{_rel_in_storage} = 0;
578 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
580 diag '* Test multi create over many_to_many';
582 $schema->resultset('CD')->create ({
584 name => 'larry', # should already exist
586 title => 'Warble Marble',
589 { producer => { name => 'Cowboy Neal' } },
593 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
594 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
595 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
596 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');