Merge 'trunk' into 'sybase'
[dbsrgits/DBIx-Class-Historic.git] / t / relationship / update_or_create_single.t
CommitLineData
2284af7e 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
8my $schema = DBICTest->init_schema();
9
10#plan tests => 4;
11plan 'no_plan';
12
13my $artist = $schema->resultset ('Artist')->first;
14
15my $genre = $schema->resultset ('Genre')
16 ->create ({ name => 'par excellence' });
17
18is ($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
28is ($genre->search_related( 'model_cd' )->count, 1, 'One cd');
29my $cd = $genre->find_related ('model_cd', {});
30is_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
48is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
49$cd = $genre->find_related ('model_cd', {});
50is_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
68is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
69$cd = $genre->find_related ('model_cd', {});
70is_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
88is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
89$cd = $genre->find_related ('model_cd', {});
90is_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