Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / relationship / update_or_create_single.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
2284af7e 3use strict;
4use warnings;
5
6use Test::More;
c0329273 7
2284af7e 8use DBICTest;
9
10my $schema = DBICTest->init_schema();
11
a2287768 12plan tests => 9;
2284af7e 13
14my $artist = $schema->resultset ('Artist')->first;
15
16my $genre = $schema->resultset ('Genre')
17 ->create ({ name => 'par excellence' });
18
19is ($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
29is ($genre->search_related( 'model_cd' )->count, 1, 'One cd');
30my $cd = $genre->find_related ('model_cd', {});
31is_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
49is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
50$cd = $genre->find_related ('model_cd', {});
51is_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
69is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
70$cd = $genre->find_related ('model_cd', {});
71is_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
5e8cb53c 83# (non-unique column only)
2284af7e 84$genre->update_or_create_related ('model_cd', {
85 year => 2011,
86});
87
88# re-fetch the cd, verify update
89is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
90$cd = $genre->find_related ('model_cd', {});
91is_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);