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