Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / 79aliasing.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
ab8481f5 3use strict;
8273e845 4use warnings;
ab8481f5 5
6use Test::More;
c0329273 7
ab8481f5 8use DBICTest;
9
10my $schema = DBICTest->init_schema();
11
af7029b7 12plan tests => 11;
ab8481f5 13
14# Check that you can leave off the alias
15{
af7029b7 16 my $artist = $schema->resultset('Artist')->find(1);
17
77211009 18 my $existing_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
19 title => 'Ted',
20 year => 2006,
ab8481f5 21 });
77211009 22 ok(! $existing_cd->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean');
23 is($existing_cd->title, 'Ted', 'find_or_create on prefetched has_many with same column names: name matches existing entry');
ab8481f5 24
77211009 25 my $new_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
26 title => 'Something Else',
27 year => 2006,
ab8481f5 28 });
77211009 29 ok(! $new_cd->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean');
30 is($new_cd->title, 'Something Else', 'find_or_create on prefetched has_many with same column names: title matches');
ab8481f5 31}
32
33# Check that you can specify the alias
34{
af7029b7 35 my $artist = $schema->resultset('Artist')->find(1);
36
77211009 37 my $existing_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
38 'me.title' => 'Something Else',
39 'me.year' => 2006,
ab8481f5 40 });
77211009 41 ok(! $existing_cd->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean');
42 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');
ab8481f5 43
77211009 44 my $new_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
45 'me.title' => 'Some New Guy',
46 'me.year' => 2006,
ab8481f5 47 });
77211009 48 ok(! $new_cd->is_changed, 'find_or_create on prefetched has_many with same column names: row is clean');
49 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');
ab8481f5 50}
af7029b7 51
52# Don't pass column names with related alias to new_result
53{
54 my $cd_rs = $schema->resultset('CD')->search({ 'artist.name' => 'Caterwauler McCrae' }, { join => 'artist' });
55
56 my $cd = $cd_rs->find_or_new({ title => 'Huh?', year => 2006 });
63bb9738 57 is($cd->in_storage, 0, 'new CD not in storage yet');
af7029b7 58 is($cd->title, 'Huh?', 'new CD title is correct');
59 is($cd->year, 2006, 'new CD year is correct');
60}