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