Deprecate emulate_limit() - can not be sanely supported by DQ
[dbsrgits/DBIx-Class.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
a2287768 10plan tests => 9;
2284af7e 11
12my $artist = $schema->resultset ('Artist')->first;
13
14my $genre = $schema->resultset ('Genre')
15 ->create ({ name => 'par excellence' });
16
17is ($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
27is ($genre->search_related( 'model_cd' )->count, 1, 'One cd');
28my $cd = $genre->find_related ('model_cd', {});
29is_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
47is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
48$cd = $genre->find_related ('model_cd', {});
49is_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
67is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
68$cd = $genre->find_related ('model_cd', {});
69is_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
5e8cb53c 81# (non-unique column only)
2284af7e 82$genre->update_or_create_related ('model_cd', {
83 year => 2011,
84});
85
86# re-fetch the cd, verify update
87is ($genre->search_related( 'model_cd' )->count, 1, 'Still one cd');
88$cd = $genre->find_related ('model_cd', {});
89is_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);