File: 79aliasing.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (58 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use strict;
use warnings;

use Test::More;
use lib qw(t/lib);
use DBICTest;

my $schema = DBICTest->init_schema();

plan tests => 11;

# Check that you can leave off the alias
{
  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_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_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
    title => 'Something Else',
    year  => 2006,
  });
  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 $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_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_cd = $artist->search_related('cds', undef, { prefetch => 'tracks' })->find_or_create({
    'me.title' => 'Some New Guy',
    'me.year'  => 2006,
  });
  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');
}