find_related fix for single-type relationships
[dbsrgits/DBIx-Class.git] / t / relationship / update_or_create_single.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 #plan tests => 4;
11 plan 'no_plan';
12
13 my $artist = $schema->resultset ('Artist')->first;
14
15 my $genre = $schema->resultset ('Genre')
16             ->create ({ name => 'par excellence' });
17
18 is ($genre->search_related( 'model_cd' )->count, 0, 'No cds yet');
19
20 # expect a create
21 $genre->update_or_create_related ('model_cd', {
22   artist => $artist,
23   year => 2009,
24   title => 'the best thing since sliced bread',
25 });
26
27 # verify cd was inserted ok
28 is ($genre->search_related( 'model_cd' )->count, 1, 'One cd');
29 my $cd = $genre->find_related ('model_cd', {});
30 is_deeply (
31   { map { $_, $cd->get_column ($_) } qw/artist year title/ },
32   {
33     artist => $artist->id,
34     year => 2009,
35     title => 'the best thing since sliced bread',
36   },
37   'CD created correctly',
38 );
39
40 # expect a year update on the only related row
41 # (non-qunique column + unique column as disambiguator)
42 $genre->update_or_create_related ('model_cd', {
43   year => 2010,
44   title => 'the best thing since sliced bread',
45 });
46
47 # re-fetch the cd, verify update
48 is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
49 $cd = $genre->find_related ('model_cd', {});
50 is_deeply (
51   { map { $_, $cd->get_column ($_) } qw/artist year title/ },
52   {
53     artist => $artist->id,
54     year => 2010,
55     title => 'the best thing since sliced bread',
56   },
57   'CD year column updated correctly',
58 );
59
60
61 # expect an update of the only related row
62 # (update a unique column)
63 $genre->update_or_create_related ('model_cd', {
64   title => 'the best thing since vertical toasters',
65 });
66
67 # re-fetch the cd, verify update
68 is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
69 $cd = $genre->find_related ('model_cd', {});
70 is_deeply (
71   { map { $_, $cd->get_column ($_) } qw/artist year title/ },
72   {
73     artist => $artist->id,
74     year => 2010,
75     title => 'the best thing since vertical toasters',
76   },
77   'CD title column updated correctly',
78 );
79
80
81 # expect a year update on the only related row
82 # (non-qunique column only)
83 $genre->update_or_create_related ('model_cd', {
84   year => 2011,
85 });
86
87 # re-fetch the cd, verify update
88 is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
89 $cd = $genre->find_related ('model_cd', {});
90 is_deeply (
91   { map { $_, $cd->get_column ($_) } qw/artist year title/ },
92   {
93     artist => $artist->id,
94     year => 2011,
95     title => 'the best thing since vertical toasters',
96   },
97   'CD year column updated correctly without a disambiguator',
98 );
99
100