Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / 79aliasing.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
11
12 plan tests => 11;
13
14 # Check that you can leave off the alias
15 {
16   my $artist = $schema->resultset('Artist')->find(1);
17
18   my $existing_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
19     title => 'Ted',
20     year  => 2006,
21   });
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');
24
25   my $new_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
26     title => 'Something Else',
27     year  => 2006,
28   });
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');
31 }
32
33 # Check that you can specify the alias
34 {
35   my $artist = $schema->resultset('Artist')->find(1);
36
37   my $existing_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
38     'me.title' => 'Something Else',
39     'me.year'  => 2006,
40   });
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');
43
44   my $new_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
45     'me.title' => 'Some New Guy',
46     'me.year'  => 2006,
47   });
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');
50 }
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 });
57   is($cd->in_storage, 0, 'new CD not in storage yet');
58   is($cd->title, 'Huh?', 'new CD title is correct');
59   is($cd->year, 2006, 'new CD year is correct');
60 }