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 { position => 1, title => 'Just buy' },
95 { position => 2, title => 'Why did we do it' },
96 { position => 3, 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 position => 1, # some day me might test this with Ordered
137 title => 'Off by one again',
141 title => 'The dereferencer',
145 title => 'Was that a null (Single)',
147 { title => 'The dereferencer', position => 1 },
148 { title => 'The dereferencer II', position => 2 },
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 position => 77, # some day me might test this with Ordered
208 title => 'Multicreate rocks',
210 artist => $cd->artist,
212 title => 'Disemboweling MultiCreate',
214 { title => 'Why does mst write this way', position => 1 },
215 { title => 'Chainsaw celebration', position => 2 },
216 { title => 'Purl cleans up', position => 3 },
238 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
239 is ($track->title, 'Multicreate rocks', 'Correct Track title');
241 my $single = $track->cd_single;
242 isa_ok ($single, 'DBICTest::CD', 'Created a single with the track');
243 is ($single->tracks->count, 3, '3 tracks on single CD');
244 is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title');
245 is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title');
246 is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title');
248 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
250 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
251 ['castaway', 'mst', 'theorbtwo'],
252 'Producers named correctly',
257 diag '* Test might_have again but with a PK == FK in the middle (obviously not specified)';
259 my $artist = $schema->resultset('Artist')->first;
260 my $cd = $schema->resultset('CD')->create ({
262 title => 'Music to code by at twilight',
266 { name => 'recursive descent' },
267 { name => 'tail packing' },
272 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
273 is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
274 isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
276 # this test might look weird, but it failed at one point, keep it there
277 my $art_obj = $cd->artwork;
278 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
279 is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
281 [ sort $art_obj->images->get_column ('name')->all ],
282 [ 'recursive descent', 'tail packing' ],
283 'Images named correctly in objects',
286 my $artwork = $schema->resultset('Artwork')->search (
287 { 'cd.title' => 'Music to code by at twilight' },
291 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
294 [ sort $artwork->images->get_column ('name')->all ],
295 [ 'recursive descent', 'tail packing' ],
296 'Images named correctly after search',
301 diag '* Test might_have again but with just a PK and FK (neither specified) in the mid-table';
303 my $cd = $schema->resultset('CD')->first;
304 my $track = $schema->resultset ('Track')->create ({
310 { text => 'The color black' },
311 { text => 'The colour black' },
316 isa_ok ($track, 'DBICTest::Track', 'Main track object created');
317 is ($track->title, 'Black', 'Correct track title');
318 isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
320 # this test might look weird, but it was failing at one point, keep it there
321 my $lyric_obj = $track->lyrics;
322 ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
323 ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
324 is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
326 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
327 [ 'The color black', 'The colour black' ],
328 'Lyrics text in objects matches',
332 my $lyric = $schema->resultset('Lyrics')->search (
333 { 'track.title' => 'Black' },
337 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
340 [ sort $lyric->lyric_versions->get_column ('text')->all ],
341 [ 'The color black', 'The colour black' ],
342 'Lyrics text via search matches',
348 * Test a multilevel might-have with a PK == FK in the might_have/has_many table
350 CD -> might have -> Artwork
353 --> Artwork_to_Artist
360 my $someartist = $schema->resultset('Artist')->first;
361 my $cd = $schema->resultset('CD')->create ({
362 artist => $someartist,
363 title => 'Music to code by until the cows come home',
366 artwork_to_artist => [
367 { artist => { name => 'cowboy joe' } },
368 { artist => { name => 'billy the kid' } },
373 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
374 is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title');
376 my $art_obj = $cd->artwork;
377 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
378 is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object');
380 [ sort $art_obj->artists->get_column ('name')->all ],
381 [ 'billy the kid', 'cowboy joe' ],
382 'Artists named correctly when queried via object',
385 my $artwork = $schema->resultset('Artwork')->search (
386 { 'cd.title' => 'Music to code by until the cows come home' },
389 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
391 [ sort $artwork->artists->get_column ('name')->all ],
392 [ 'billy the kid', 'cowboy joe' ],
393 'Artists named correctly queried via a new search',
398 diag '* Nested find_or_create';
400 my $newartist2 = $schema->resultset('Artist')->find_or_create({
409 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
413 diag '* Multiple same level has_many create';
415 my $artist2 = $schema->resultset('Artist')->create({
419 title => 'Music to code by',
425 title => 'Music to code by',
431 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
435 diag '* First create_related pass';
437 my $artist = $schema->resultset('Artist')->first;
439 my $cd_result = $artist->create_related('cds', {
441 title => 'TestOneCD1',
455 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
456 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
458 my $tracks = $cd_result->tracks;
460 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
462 foreach my $track ($tracks->all)
464 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
469 diag '* second create_related with same arguments';
471 my $artist = $schema->resultset('Artist')->first;
473 my $cd_result = $artist->create_related('cds', {
475 title => 'TestOneCD2',
487 liner_notes => { notes => 'I can haz liner notes?' },
491 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
492 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
493 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
495 my $tracks = $cd_result->tracks;
497 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
499 foreach my $track ($tracks->all)
501 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
506 diag '* create of parents of a record linker table';
508 my $cdp = $schema->resultset('CD_to_Producer')->create({
509 cd => { artist => 1, title => 'foo', year => 2000 },
510 producer => { name => 'jorge' }
512 ok($cdp, 'join table record created ok');
517 my $kurt_cobain = { name => 'Kurt Cobain' };
519 my $in_utero = $schema->resultset('CD')->new({
524 $kurt_cobain->{cds} = [ $in_utero ];
527 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
528 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
530 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
531 is($a->cds && $a->cds->first && $a->cds->first->title,
532 'In Utero', 'CD insertion ok');
537 # This test case has been moved to t/96multi_create/cd_single.t
539 my $pink_floyd = { name => 'Pink Floyd' };
541 my $the_wall = { title => 'The Wall', year => 1979 };
543 $pink_floyd->{cds} = [ $the_wall ];
546 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
547 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
549 is($a->name, 'Pink Floyd', 'Artist insertion ok');
550 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
555 diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
556 ## Create foreign key col obj including PK
557 ## See test 20 in 66relationships.t
559 my $new_cd_hashref = {
561 title => 'Boogie Woogie',
563 artist => { artistid => 17, name => 'king luke' }
566 my $cd = $schema->resultset("CD")->find(1);
568 is($cd->artist->id, 1, 'rel okay');
570 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
571 is($new_cd->artist->id, 17, 'new id retained okay');
576 $schema->resultset("CD")->create({
578 title => 'Boogie Wiggle',
580 artist => { artistid => 18, name => 'larry' }
583 is($@, '', 'new cd created without clash on related artist');
585 diag '* Make sure exceptions from errors in created rels propogate';
587 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
588 #$t->cd($t->new_related('cd', { artist => undef } ) );
589 #$t->{_rel_in_storage} = 0;
592 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
594 diag '* Test multi create over many_to_many';
596 $schema->resultset('CD')->create ({
598 name => 'larry', # should already exist
600 title => 'Warble Marble',
603 { producer => { name => 'Cowboy Neal' } },
607 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
608 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
609 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
610 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');