X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F79aliasing.t;h=70738c612cc45f795ade822ae8e253582d0979fb;hb=HEAD;hp=e0eb04c38ed8061d4b8cd28d99ba9dcf57f9828d;hpb=ab8481f509e0c74b2dbe61441564c250b457193e;p=dbsrgits%2FDBIx-Class.git diff --git a/t/79aliasing.t b/t/79aliasing.t index e0eb04c..70738c6 100644 --- a/t/79aliasing.t +++ b/t/79aliasing.t @@ -1,42 +1,60 @@ +BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } + use strict; -use warnings; +use warnings; use Test::More; -use lib qw(t/lib); + use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 8; - -my $label = $schema->resultset('Label')->find({ labelid => 1 }); +plan tests => 11; # Check that you can leave off the alias { - my $existing_agent = $label->agents->find_or_create({ - name => 'Ted', + my $artist = $schema->resultset('Artist')->find(1); + + my $existing_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({ + title => 'Ted', + year => 2006, }); - ok(! $existing_agent->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean'); - is($existing_agent->name, 'Ted', 'find_or_create on prefetched has_many with same column names: name matches existing entry'); + ok(! $existing_cd->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean'); + is($existing_cd->title, 'Ted', 'find_or_create on prefetched has_many with same column names: name matches existing entry'); - my $new_agent = $label->agents->find_or_create({ - name => 'Someone Else', + my $new_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({ + title => 'Something Else', + year => 2006, }); - ok(! $new_agent->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean'); - is($new_agent->name, 'Someone Else', 'find_or_create on prefetched has_many with same column names: name matches'); + ok(! $new_cd->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean'); + is($new_cd->title, 'Something Else', 'find_or_create on prefetched has_many with same column names: title matches'); } # Check that you can specify the alias { - my $existing_agent = $label->agents->find_or_create({ - 'me.name' => 'Someone Else', + my $artist = $schema->resultset('Artist')->find(1); + + my $existing_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({ + 'me.title' => 'Something Else', + 'me.year' => 2006, }); - ok(! $existing_agent->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean'); - is($existing_agent->name, 'Someone Else', 'find_or_create on prefetched has_many with same column names: can be disambiguated with "me." for existing entry'); + ok(! $existing_cd->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean'); + is($existing_cd->title, 'Something Else', 'find_or_create on prefetched has_many with same column names: can be disambiguated with "me." for existing entry'); - my $new_agent = $label->agents->find_or_create({ - 'me.name' => 'Some New Guy', + my $new_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({ + 'me.title' => 'Some New Guy', + 'me.year' => 2006, }); - ok(! $new_agent->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean'); - is($new_agent->name, 'Some New Guy', 'find_or_create on prefetched has_many with same column names: can be disambiguated with "me." for new entry'); + ok(! $new_cd->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean'); + is($new_cd->title, 'Some New Guy', 'find_or_create on prefetched has_many with same column names: can be disambiguated with "me." for new entry'); +} + +# Don't pass column names with related alias to new_result +{ + my $cd_rs = $schema->resultset('CD')->search({ 'artist.name' => 'Caterwauler McCrae' }, { join => 'artist' }); + + my $cd = $cd_rs->find_or_new({ title => 'Huh?', year => 2006 }); + is($cd->in_storage, 0, 'new CD not in storage yet'); + is($cd->title, 'Huh?', 'new CD title is correct'); + is($cd->year, 2006, 'new CD year is correct'); }