X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F96multi_create.t;h=9c75cac618245265973bd76198e468e00c08914a;hb=1e891a37baefb4dc4410018bc87ea4a45f66fc02;hp=4a2c875a723787dd13d5ba0033e7dc066cc02777;hpb=04ec3909387cc83d5580043ad8f3f0667d3a07af;p=dbsrgits%2FDBIx-Class.git diff --git a/t/96multi_create.t b/t/96multi_create.t index 4a2c875..9c75cac 100644 --- a/t/96multi_create.t +++ b/t/96multi_create.t @@ -2,16 +2,17 @@ use strict; use warnings; use Test::More; +use Test::Exception; use lib qw(t/lib); use DBICTest; -plan tests => 51; +plan tests => 85; my $schema = DBICTest->init_schema(); -# simple create + belongs_to +diag '* simple create + parent (the stuff $rs belongs_to)'; eval { - my $cd2 = $schema->resultset('CD')->create({ + my $cd = $schema->resultset('CD')->create({ artist => { name => 'Fred Bloggs' }, @@ -19,50 +20,382 @@ eval { year => 1996 }); - isa_ok($cd2, 'DBICTest::CD', 'Created CD object'); - isa_ok($cd2->artist, 'DBICTest::Artist', 'Created related Artist'); - is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly'); + isa_ok($cd, 'DBICTest::CD', 'Created CD object'); + isa_ok($cd->artist, 'DBICTest::Artist', 'Created related Artist'); + is($cd->artist->name, 'Fred Bloggs', 'Artist created correctly'); }; diag $@ if $@; -# create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } ) +diag '* same as above but the child and parent have no values, except for an explicit parent pk'; eval { - my $artist = $schema->resultset('Artist')->create( - { name => 'Fred 2', + my $bm_rs = $schema->resultset('Bookmark'); + my $bookmark = $bm_rs->create({ + link => { + id => 66, + }, + }); + + isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object'); + isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link'); + is ( + $bm_rs->search ( + { 'link.title' => $bookmark->link->title }, + { join => 'link' }, + )->count, + 1, + 'Bookmark and link made it to the DB', + ); +}; +diag $@ if $@; + +diag '* create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )'; +eval { + my $artist = $schema->resultset('Artist')->first; + my $cd = $artist->create_related (cds => { + title => 'Music to code by', + year => 2007, + tags => [ + { 'tag' => 'rock' }, + ], + }); + + isa_ok($cd, 'DBICTest::CD', 'Created CD'); + is($cd->title, 'Music to code by', 'CD created correctly'); + is($cd->tags->count, 1, 'One tag created for CD'); + is($cd->tags->first->tag, 'rock', 'Tag created correctly'); + +}; +diag $@ if $@; + +throws_ok ( + sub { + # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED! + $schema->resultset('Artist')->first->update({ cds => [ - { title => 'Music to code by', - year => 2007, - tags => [ - { 'tag' => 'rock' }, - ], + { title => 'Yet another CD', + year => 2006, }, - ], + ], + }); + }, + qr/Recursive update is not supported over relationships of type multi/, + 'create via update of multi relationships throws an exception' +); + +diag '* Create m2m while originating in the linker table'; +eval { + my $artist = $schema->resultset('Artist')->first; + my $c2p = $schema->resultset('CD_to_Producer')->create ({ + cd => { + artist => $artist, + title => 'Bad investment', + year => 2008, + tracks => [ + { position => 1, title => 'Just buy' }, + { position => 2, title => 'Why did we do it' }, + { position => 3, title => 'Burn baby burn' }, + ], + }, + producer => { + name => 'Lehman Bros.', + }, }); - isa_ok($artist, 'DBICTest::Artist', 'Created Artist'); - is($artist->name, 'Fred 2', 'Artist created correctly'); - is($artist->cds->count, 1, 'One CD created for artist'); - is($artist->cds->first->title, 'Music to code by', 'CD created correctly'); - is($artist->cds->first->tags->count, 1, 'One tag created for CD'); - is($artist->cds->first->tags->first->tag, 'rock', 'Tag created correctly'); - - # Create via update - add a new CD - $artist->update({ - cds => [ $artist->cds, - { title => 'Yet another CD', - year => 2006, + isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created'); + my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' }); + isa_ok ($prod, 'DBICTest::Producer', 'Producer row found'); + is ($prod->cds->count, 1, 'Producer has one production'); + my $cd = $prod->cds->first; + is ($cd->title, 'Bad investment', 'CD created correctly'); + is ($cd->tracks->count, 3, 'CD has 3 tracks'); + +}; +diag $@ if $@; + +diag (<<'DG'); +* Create over > 1 levels of might_have with multiple has_many and multiple m2m +but starting at a has_many level + +CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks + \ + \-> has_many \ + --> CD2Producer + /-> has_many / + / + Producer +DG + +eval { + my $artist = $schema->resultset('Artist')->first; + my $cd = $schema->resultset('CD')->create ({ + artist => $artist, + title => 'Music to code by at night', + year => 2008, + tracks => [ + { + position => 1, # some day me might test this with Ordered + title => 'Off by one again', + }, + { + position => 2, + title => 'The dereferencer', + cd_single => { + artist => $artist, + year => 2008, + title => 'Was that a null (Single)', + tracks => [ + { title => 'The dereferencer', position => 1 }, + { title => 'The dereferencer II', position => 2 }, + ], + cd_to_producer => [ + { + producer => { + name => 'K&R', + } + }, + { + producer => { + name => 'Don Knuth', + } + }, + ] + }, }, ], }); - is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD'); - my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'}); + isa_ok ($cd, 'DBICTest::CD', 'Main CD object created'); + is ($cd->title, 'Music to code by at night', 'Correct CD title'); + is ($cd->tracks->count, 2, 'Two tracks on main CD'); + + my ($t1, $t2) = $cd->tracks->all; + is ($t1->title, 'Off by one again', 'Correct 1st track name'); + is ($t1->cd_single, undef, 'No single for 1st track'); + is ($t2->title, 'The dereferencer', 'Correct 2nd track name'); + isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track'); + + my $single = $t2->cd_single; + is ($single->tracks->count, 2, 'Two tracks on single CD'); + is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title'); + is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title'); + + is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd'); + is_deeply ( + [ sort map { $_->producer->name } ($single->cd_to_producer->all) ], + ['Don Knuth', 'K&R'], + 'Producers named correctly', + ); +}; +diag $@ if $@; + +diag (<<'DG'); +* Same as above but starting at the might_have directly + +Track -> might have -> Single -> has_many -> Tracks + \ + \-> has_many \ + --> CD2Producer + /-> has_many / + / + Producer +DG + +eval { + my $cd = $schema->resultset('CD')->first; + my $track = $schema->resultset('Track')->create ({ + cd => $cd, + position => 77, # some day me might test this with Ordered + title => 'Multicreate rocks', + cd_single => { + artist => $cd->artist, + year => 2008, + title => 'Disemboweling MultiCreate', + tracks => [ + { title => 'Why does mst write this way', position => 1 }, + { title => 'Chainsaw celebration', position => 2 }, + { title => 'Purl cleans up', position => 3 }, + ], + cd_to_producer => [ + { + producer => { + name => 'mst', + } + }, + { + producer => { + name => 'castaway', + } + }, + { + producer => { + name => 'theorbtwo', + } + }, + ] + }, + }); + + isa_ok ($track, 'DBICTest::Track', 'Main Track object created'); + is ($track->title, 'Multicreate rocks', 'Correct Track title'); + + my $single = $track->cd_single; + isa_ok ($single, 'DBICTest::CD', 'Created a single with the track'); + is ($single->tracks->count, 3, '3 tracks on single CD'); + is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title'); + is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title'); + is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title'); + + is ($single->cd_to_producer->count, 3, '3 producers created for the single cd'); + is_deeply ( + [ sort map { $_->producer->name } ($single->cd_to_producer->all) ], + ['castaway', 'mst', 'theorbtwo'], + 'Producers named correctly', + ); +}; +diag $@ if $@; + +diag '* Test might_have again but with a PK == FK in the middle (obviously not specified)'; +eval { + my $artist = $schema->resultset('Artist')->first; + my $cd = $schema->resultset('CD')->create ({ + artist => $artist, + title => 'Music to code by at twilight', + year => 2008, + artwork => { + images => [ + { name => 'recursive descent' }, + { name => 'tail packing' }, + ], + }, + }); - is($newartist->name, 'Fred 2', 'Retrieved the artist'); + isa_ok ($cd, 'DBICTest::CD', 'Main CD object created'); + is ($cd->title, 'Music to code by at twilight', 'Correct CD title'); + isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created'); + + # this test might look weird, but it failed at one point, keep it there + my $art_obj = $cd->artwork; + ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object'); + is ($art_obj->images->count, 2, 'Correct artwork image count via the new object'); + is_deeply ( + [ sort $art_obj->images->get_column ('name')->all ], + [ 'recursive descent', 'tail packing' ], + 'Images named correctly in objects', + ); + + my $artwork = $schema->resultset('Artwork')->search ( + { 'cd.title' => 'Music to code by at twilight' }, + { join => 'cd' }, + )->single; + + is ($artwork->images->count, 2, 'Correct artwork image count via a new search'); + + is_deeply ( + [ sort $artwork->images->get_column ('name')->all ], + [ 'recursive descent', 'tail packing' ], + 'Images named correctly after search', + ); }; diag $@ if $@; -# nested find_or_create +diag '* Test might_have again but with just a PK and FK (neither specified) in the mid-table'; +eval { + my $cd = $schema->resultset('CD')->first; + my $track = $schema->resultset ('Track')->create ({ + cd => $cd, + position => 66, + title => 'Black', + lyrics => { + lyric_versions => [ + { text => 'The color black' }, + { text => 'The colour black' }, + ], + }, + }); + + isa_ok ($track, 'DBICTest::Track', 'Main track object created'); + is ($track->title, 'Black', 'Correct track title'); + isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created'); + + # this test might look weird, but it was failing at one point, keep it there + my $lyric_obj = $track->lyrics; + ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object'); + ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object'); + is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object'); + is_deeply ( + [ sort $lyric_obj->lyric_versions->get_column ('text')->all ], + [ 'The color black', 'The colour black' ], + 'Lyrics text in objects matches', + ); + + + my $lyric = $schema->resultset('Lyrics')->search ( + { 'track.title' => 'Black' }, + { join => 'track' }, + )->single; + + is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search'); + + is_deeply ( + [ sort $lyric->lyric_versions->get_column ('text')->all ], + [ 'The color black', 'The colour black' ], + 'Lyrics text via search matches', + ); +}; +diag $@ if $@; + +diag (<<'DG'); +* Test a multilevel might-have with a PK == FK in the might_have/has_many table + +CD -> might have -> Artwork + \ + \-> has_many \ + --> Artwork_to_Artist + /-> has_many / + / + Artist +DG + +eval { + my $someartist = $schema->resultset('Artist')->first; + my $cd = $schema->resultset('CD')->create ({ + artist => $someartist, + title => 'Music to code by until the cows come home', + year => 2008, + artwork => { + artwork_to_artist => [ + { artist => { name => 'cowboy joe' } }, + { artist => { name => 'billy the kid' } }, + ], + }, + }); + + isa_ok ($cd, 'DBICTest::CD', 'Main CD object created'); + is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title'); + + my $art_obj = $cd->artwork; + ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object'); + is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object'); + is_deeply ( + [ sort $art_obj->artists->get_column ('name')->all ], + [ 'billy the kid', 'cowboy joe' ], + 'Artists named correctly when queried via object', + ); + + my $artwork = $schema->resultset('Artwork')->search ( + { 'cd.title' => 'Music to code by until the cows come home' }, + { join => 'cd' }, + )->single; + is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search'); + is_deeply ( + [ sort $artwork->artists->get_column ('name')->all ], + [ 'billy the kid', 'cowboy joe' ], + 'Artists named correctly queried via a new search', + ); +}; +diag $@ if $@; + +diag '* Nested find_or_create'; eval { my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3', @@ -77,10 +410,10 @@ eval { }; diag $@ if $@; -# multiple same level has_many create +diag '* Multiple same level has_many create'; eval { my $artist2 = $schema->resultset('Artist')->create({ - name => 'Fred 3', + name => 'Fred 4', cds => [ { title => 'Music to code by', @@ -99,7 +432,7 @@ eval { }; diag $@ if $@; -# first create_related pass +diag '* First create_related pass'; eval { my $artist = $schema->resultset('Artist')->first; @@ -133,7 +466,7 @@ eval { }; diag $@ if $@; -# second create_related with same arguments +diag '* second create_related with same arguments'; eval { my $artist = $schema->resultset('Artist')->first; @@ -170,7 +503,7 @@ eval { }; diag $@ if $@; -# create of parents of a record linker table +diag '* create of parents of a record linker table'; eval { my $cdp = $schema->resultset('CD_to_Producer')->create({ cd => { artist => 1, title => 'foo', year => 2000 }, @@ -180,6 +513,8 @@ eval { }; diag $@ if $@; +TODO: { +local $TODO = 'Next 2 evals are NOT supposed to work, jnaps code will be torn to bits in another branch'; #SPECIAL_CASE eval { my $kurt_cobain = { name => 'Kurt Cobain' }; @@ -217,9 +552,10 @@ eval { is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok'); }; diag $@ if $@; +} + -## Create foreign key col obj including PK -## See test 20 in 66relationships.t +diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)'; eval { my $new_cd_hashref = { cdid => 27, @@ -247,7 +583,7 @@ eval { }; is($@, '', 'new cd created without clash on related artist'); -# Make sure exceptions from errors in created rels propogate +diag '* Make sure exceptions from errors in created rels propogate'; eval { my $t = $schema->resultset("Track")->new({ cd => { artist => undef } }); #$t->cd($t->new_related('cd', { artist => undef } ) ); @@ -256,7 +592,7 @@ eval { }; like($@, qr/cd.artist may not be NULL/, "Exception propogated properly"); -# Test multi create over many_to_many +diag '* Test multi create over many_to_many'; eval { $schema->resultset('CD')->create ({ artist => { @@ -275,105 +611,4 @@ eval { is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created'); }; -# and some insane multicreate -# (should work, despite the fact that no one will probably use it this way) - -# first count how many rows do we initially have - -my $counts; -$counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/; - -# do the crazy create -eval { - $schema->resultset('CD')->create ({ - artist => { - name => 'larry', - }, - title => 'Greatest hits 1', - year => '2012', - genre => { - name => '"Greatest" collections', - }, - tags => [ - { tag => 'A' }, - { tag => 'B' }, - ], - cd_to_producer => [ - { - producer => { - name => 'Dirty Harry', - producer_to_cd => [ - { - cd => { - artist => { - name => 'Dirty Harry himself', - cds => [ - { - title => 'Greatest hits 3', - year => 2012, - genre => { - name => '"Greatest" collections', - }, - tags => [ - { tag => 'A' }, - { tag => 'B' }, - ], - }, - ], - }, - title => 'Greatest hits 2', - year => 2012, - genre => { - name => '"Greatest" collections', - }, - tags => [ - { tag => 'A' }, - { tag => 'B' }, - ], - }, - }, - { - cd => { - artist => { - name => 'larry', # should already exist - }, - title => 'Greatest hits 4', - year => 2012, - }, - }, - ], - }, - }, - ], - }); - - is ($schema->resultset ('Artist')->count, $counts->{Artist} + 1, 'One new artists created'); # even though the 'name' is not uniquely constrained find_or_create will arguably DWIM - is ($schema->resultset ('Genre')->count, $counts->{Genre} + 1, 'One additional genre created'); - is ($schema->resultset ('Producer')->count, $counts->{Producer} + 1, 'One new producer'); - is ($schema->resultset ('CD')->count, $counts->{CD} + 4, '4 new CDs'); - is ($schema->resultset ('Tag')->count, $counts->{Tag} + 6, '6 new Tags'); - - my $harry_cds = $schema->resultset ('Artist')->single ({name => 'Dirty Harry himself'})->cds; - is ($harry_cds->count, 2, 'Two CDs created by Harry'); - ok ($harry_cds->single ({title => 'Greatest hits 2'}), 'First CD name correct'); - ok ($harry_cds->single ({title => 'Greatest hits 3'}), 'Second CD name correct'); - - my $harry_productions = $schema->resultset ('Producer')->single ({name => 'Dirty Harry'}) - ->search_related ('producer_to_cd', {})->search_related ('cd', {}); - is ($harry_productions->count, 4, 'All 4 CDs are produced by Harry'); - is ($harry_productions->search ({ year => 2012 })->count, 4, 'All 4 CDs have the correct year'); - - my $hits_genre = $schema->resultset ('Genre')->single ({name => '"Greatest" collections'}); - ok ($hits_genre, 'New genre row found'); - is ($hits_genre->cds->count, 3, 'Three of the new CDs fall into the new genre'); - - my $a_tags = $schema->resultset('Tag')->search({ tag => 'A'}); - my $b_tags = $schema->resultset('Tag')->search({ tag => 'A'}); - is ($a_tags->count, 3, '3 A tags'); - is ($a_tags->count, 3, '3 B tags'); - - my $cds_with_ab = $schema->resultset('CD') - ->search({ 'tags.tag' => { -in => [qw/A B/] } }, { join => 'tags', group_by => 'me.cdid' } ); - is ($cds_with_ab->count, 3, '6 tags were pairwise distributed between 3 CDs'); -}; -diag $@ if $@; +1;