Fix find() with an explicit constraint name (... { key => $cname } )
[dbsrgits/DBIx-Class.git] / t / 80unique.t
CommitLineData
70350518 1use strict;
246fa39d 2use warnings;
70350518 3
4use Test::More;
034d0be4 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
246fa39d 8use DBIC::SqlMakerTest;
9use DBIC::DebugObj;
70350518 10
a47e1233 11my $schema = DBICTest->init_schema();
87f0da6a 12
365d06b7 13# Check the defined unique constraints
14is_deeply(
15 [ sort $schema->source('CD')->unique_constraint_names ],
16 [ qw/cd_artist_title primary/ ],
17 'CD source has an automatically named unique constraint'
18);
19is_deeply(
20 [ sort $schema->source('Producer')->unique_constraint_names ],
21 [ qw/primary prod_name/ ],
22 'Producer source has a named unique constraint'
23);
24is_deeply(
25 [ sort $schema->source('Track')->unique_constraint_names ],
26 [ qw/primary track_cd_position track_cd_title/ ],
27 'Track source has three unique constraints'
28);
034d0be4 29is_deeply(
30 [ sort $schema->source('Tag')->unique_constraint_names ],
31 [ qw/primary tagid_cd tagid_cd_tag tags_tagid_tag tags_tagid_tag_cd/ ],
32 'Tag source has five unique constraints (from add_unique_constraings)'
33);
87f0da6a 34
35my $artistid = 1;
36my $title = 'UNIQUE Constraint';
37
38my $cd1 = $schema->resultset('CD')->find_or_create({
39 artist => $artistid,
40 title => $title,
41 year => 2005,
42});
43
44my $cd2 = $schema->resultset('CD')->find(
45 {
46 artist => $artistid,
47 title => $title,
48 },
368a5228 49 { key => 'cd_artist_title' }
87f0da6a 50);
51
52is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
53is($cd2->title, $cd1->title, 'title is correct');
54is($cd2->year, $cd1->year, 'year is correct');
55
368a5228 56my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' });
c9bb4b2f 57
58is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
59is($cd3->title, $cd1->title, 'title is correct');
60is($cd3->year, $cd1->year, 'year is correct');
61
62my $cd4 = $schema->resultset('CD')->update_or_create(
87f0da6a 63 {
64 artist => $artistid,
65 title => $title,
66 year => 2007,
67 },
68);
69
c9bb4b2f 70ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
71is($cd4->cdid, $cd2->cdid, 'cdid is correct');
72is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
73is($cd4->title, $cd2->title, 'title is correct');
74is($cd4->year, 2007, 'updated year is correct');
87f0da6a 75
c9bb4b2f 76my $cd5 = $schema->resultset('CD')->update_or_create(
87f0da6a 77 {
78 artist => $artistid,
79 title => $title,
80 year => 2007,
81 },
368a5228 82 { key => 'cd_artist_title' }
87f0da6a 83);
84
c9bb4b2f 85ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
86is($cd5->cdid, $cd2->cdid, 'cdid is correct');
87is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
88is($cd5->title, $cd2->title, 'title is correct');
89is($cd5->year, 2007, 'updated year is correct');
87f0da6a 90
c9bb4b2f 91my $cd6 = $schema->resultset('CD')->update_or_create(
87f0da6a 92 {
93 cdid => $cd2->cdid,
94 artist => 1,
95 title => $cd2->title,
96 year => 2005,
97 },
98 { key => 'primary' }
99);
100
c9bb4b2f 101ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
102is($cd6->cdid, $cd2->cdid, 'cdid is correct');
103is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
104is($cd6->title, $cd2->title, 'title is correct');
105is($cd6->year, 2005, 'updated year is correct');
87f0da6a 106
c9bb4b2f 107my $cd7 = $schema->resultset('CD')->find_or_create(
8dc40f3e 108 {
109 artist => $artistid,
110 title => $title,
111 year => 2010,
112 },
368a5228 113 { key => 'cd_artist_title' }
8dc40f3e 114);
115
c9bb4b2f 116is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
117is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
118is($cd7->title, $cd1->title, 'title is correct');
119is($cd7->year, $cd1->year, 'year is correct');
8dc40f3e 120
121my $artist = $schema->resultset('Artist')->find($artistid);
c9bb4b2f 122my $cd8 = $artist->find_or_create_related('cds',
8dc40f3e 123 {
8dc40f3e 124 title => $title,
125 year => 2020,
126 },
368a5228 127 { key => 'cd_artist_title' }
8dc40f3e 128);
129
c9bb4b2f 130is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
131is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
132is($cd8->title, $cd1->title, 'title is correct');
133is($cd8->year, $cd1->year, 'year is correct');
8dc40f3e 134
b7743dab 135# Add an extra row to potentially confuse the query
136$schema->resultset('CD')->create ({
137 artist => 2,
138 title => $title,
139 year => 2022,
140});
d180c0f3 141my $cd9 = $artist->cds->update_or_create(
045120e6 142 {
d180c0f3 143 cdid => $cd1->cdid,
045120e6 144 title => $title,
145 year => 2021,
146 },
368a5228 147 { key => 'cd_artist_title' }
045120e6 148);
149
c9bb4b2f 150ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
151is($cd9->cdid, $cd1->cdid, 'cdid is correct');
152is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
153is($cd9->title, $cd1->title, 'title is correct');
154is($cd9->year, 2021, 'year is correct');
045120e6 155
dcfb635f 156# Table with two unique constraints, and we're satisying one of them
365d06b7 157my $track = $schema->resultset('Track')->find(
158 {
159 cd => 1,
160 position => 3,
161 },
162 { order_by => 'position' }
163);
164
165is($track->get_column('cd'), 1, 'track cd is correct');
166is($track->get_column('position'), 3, 'track position is correct');
89034887 167
168# Test a table with a unique constraint but no primary key
169my $row = $schema->resultset('NoPrimaryKey')->update_or_create(
170 {
171 foo => 1,
172 bar => 2,
173 baz => 3,
174 },
175 { key => 'foo_bar' }
176);
d180c0f3 177
8070a151 178ok(! $row->is_changed, 'update_or_create on table without primary key: row is clean');
89034887 179is($row->foo, 1, 'foo is correct');
180is($row->bar, 2, 'bar is correct');
181is($row->baz, 3, 'baz is correct');
d180c0f3 182
183# Test a unique condition with extra information in the where attr
184{
185 my $artist = $schema->resultset('Artist')->find({ artistid => 1 });
186 my $cd = $artist->cds->find_or_new(
187 {
188 cdid => 1,
189 title => 'Not The Real Title',
190 year => 3000,
191 },
192 { key => 'primary' }
193 );
194
195 ok($cd->in_storage, 'find correctly grepped the key across a relationship');
196 is($cd->cdid, 1, 'cdid is correct');
197}
9ffa8fd7 198
199# Test update_or_new
200{
201 my $cd1 = $schema->resultset('CD')->update_or_new(
202 {
203 artist => $artistid,
204 title => "SuperHits $$",
205 year => 2007,
206 },
207 { key => 'cd_artist_title' }
208 );
209
63bb9738 210 is($cd1->in_storage, 0, 'CD is not in storage yet after update_or_new');
9ffa8fd7 211 $cd1->insert;
212 ok($cd1->in_storage, 'CD got added to strage after update_or_new && insert');
213
214 my $cd2 = $schema->resultset('CD')->update_or_new(
215 {
216 artist => $artistid,
217 title => "SuperHits $$",
218 year => 2008,
219 },
220 { key => 'cd_artist_title' }
221 );
222 ok($cd2->in_storage, 'Updating year using update_or_new was successful');
223 is($cd2->id, $cd1->id, 'Got the same CD using update_or_new');
246fa39d 224}
225
226# make sure the ident condition is assembled sanely
227{
228 my $artist = $schema->resultset('Artist')->next;
229
230 my ($sql, @bind);
231 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
232 $schema->storage->debug(1);
233
234 $artist->discard_changes;
235
236 is_same_sql_bind (
237 $sql,
238 \@bind,
239 'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?',
240 [qw/'1'/],
241 );
242
243 $schema->storage->debug(0);
244 $schema->storage->debugobj(undef);
245}
246
034d0be4 247{
248 throws_ok {
249 eval <<'MOD' or die $@;
250 package # hide from PAUSE
251 DBICTest::Schema::UniqueConstraintWarningTest;
252
253 use base qw/DBIx::Class::Core/;
254
255 __PACKAGE__->table('dummy');
256
257 __PACKAGE__->add_column(qw/ foo bar /);
258
259 __PACKAGE__->add_unique_constraint(
260 constraint1 => [qw/ foo /],
261 constraint2 => [qw/ bar /],
262 );
263
264 1;
265MOD
266 } qr/\Qadd_unique_constraint() does not accept multiple constraints, use add_unique_constraints() instead\E/,
267 'add_unique_constraint throws when more than one constraint specified';
268}
269
270
246fa39d 271done_testing;
034d0be4 272