Pass attrs to find from update_or_create (reported by Nathan Kurz)
[dbsrgits/DBIx-Class-Historic.git] / t / 80unique.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
a47e1233 8my $schema = DBICTest->init_schema();
87f0da6a 9
89034887 10plan tests => 43;
368a5228 11
365d06b7 12# Check the defined unique constraints
13is_deeply(
14 [ sort $schema->source('CD')->unique_constraint_names ],
15 [ qw/cd_artist_title primary/ ],
16 'CD source has an automatically named unique constraint'
17);
18is_deeply(
19 [ sort $schema->source('Producer')->unique_constraint_names ],
20 [ qw/primary prod_name/ ],
21 'Producer source has a named unique constraint'
22);
23is_deeply(
24 [ sort $schema->source('Track')->unique_constraint_names ],
25 [ qw/primary track_cd_position track_cd_title/ ],
26 'Track source has three unique constraints'
27);
87f0da6a 28
29my $artistid = 1;
30my $title = 'UNIQUE Constraint';
31
32my $cd1 = $schema->resultset('CD')->find_or_create({
33 artist => $artistid,
34 title => $title,
35 year => 2005,
36});
37
38my $cd2 = $schema->resultset('CD')->find(
39 {
40 artist => $artistid,
41 title => $title,
42 },
368a5228 43 { key => 'cd_artist_title' }
87f0da6a 44);
45
46is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
47is($cd2->title, $cd1->title, 'title is correct');
48is($cd2->year, $cd1->year, 'year is correct');
49
368a5228 50my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' });
c9bb4b2f 51
52is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
53is($cd3->title, $cd1->title, 'title is correct');
54is($cd3->year, $cd1->year, 'year is correct');
55
56my $cd4 = $schema->resultset('CD')->update_or_create(
87f0da6a 57 {
58 artist => $artistid,
59 title => $title,
60 year => 2007,
61 },
62);
63
c9bb4b2f 64ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
65is($cd4->cdid, $cd2->cdid, 'cdid is correct');
66is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
67is($cd4->title, $cd2->title, 'title is correct');
68is($cd4->year, 2007, 'updated year is correct');
87f0da6a 69
c9bb4b2f 70my $cd5 = $schema->resultset('CD')->update_or_create(
87f0da6a 71 {
72 artist => $artistid,
73 title => $title,
74 year => 2007,
75 },
368a5228 76 { key => 'cd_artist_title' }
87f0da6a 77);
78
c9bb4b2f 79ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
80is($cd5->cdid, $cd2->cdid, 'cdid is correct');
81is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
82is($cd5->title, $cd2->title, 'title is correct');
83is($cd5->year, 2007, 'updated year is correct');
87f0da6a 84
c9bb4b2f 85my $cd6 = $schema->resultset('CD')->update_or_create(
87f0da6a 86 {
87 cdid => $cd2->cdid,
88 artist => 1,
89 title => $cd2->title,
90 year => 2005,
91 },
92 { key => 'primary' }
93);
94
c9bb4b2f 95ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
96is($cd6->cdid, $cd2->cdid, 'cdid is correct');
97is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
98is($cd6->title, $cd2->title, 'title is correct');
99is($cd6->year, 2005, 'updated year is correct');
87f0da6a 100
c9bb4b2f 101my $cd7 = $schema->resultset('CD')->find_or_create(
8dc40f3e 102 {
103 artist => $artistid,
104 title => $title,
105 year => 2010,
106 },
368a5228 107 { key => 'cd_artist_title' }
8dc40f3e 108);
109
c9bb4b2f 110is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
111is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
112is($cd7->title, $cd1->title, 'title is correct');
113is($cd7->year, $cd1->year, 'year is correct');
8dc40f3e 114
115my $artist = $schema->resultset('Artist')->find($artistid);
c9bb4b2f 116my $cd8 = $artist->find_or_create_related('cds',
8dc40f3e 117 {
8dc40f3e 118 title => $title,
119 year => 2020,
120 },
368a5228 121 { key => 'cd_artist_title' }
8dc40f3e 122);
123
c9bb4b2f 124is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
125is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
126is($cd8->title, $cd1->title, 'title is correct');
127is($cd8->year, $cd1->year, 'year is correct');
8dc40f3e 128
c9bb4b2f 129my $cd9 = $artist->update_or_create_related('cds',
045120e6 130 {
045120e6 131 title => $title,
132 year => 2021,
133 },
368a5228 134 { key => 'cd_artist_title' }
045120e6 135);
136
c9bb4b2f 137ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
138is($cd9->cdid, $cd1->cdid, 'cdid is correct');
139is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
140is($cd9->title, $cd1->title, 'title is correct');
141is($cd9->year, 2021, 'year is correct');
045120e6 142
dcfb635f 143# Table with two unique constraints, and we're satisying one of them
365d06b7 144my $track = $schema->resultset('Track')->find(
145 {
146 cd => 1,
147 position => 3,
148 },
149 { order_by => 'position' }
150);
151
152is($track->get_column('cd'), 1, 'track cd is correct');
153is($track->get_column('position'), 3, 'track position is correct');
89034887 154
155# Test a table with a unique constraint but no primary key
156my $row = $schema->resultset('NoPrimaryKey')->update_or_create(
157 {
158 foo => 1,
159 bar => 2,
160 baz => 3,
161 },
162 { key => 'foo_bar' }
163);
164is(! $row->is_changed, 1, 'update_or_create on table without primary key: row is clean');
165is($row->foo, 1, 'foo is correct');
166is($row->bar, 2, 'bar is correct');
167is($row->baz, 3, 'baz is correct');