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