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