Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[dbsrgits/DBIx-Class.git] / t / 80unique.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Test::Warn;
7 use lib qw(t/lib);
8 use DBICTest;
9 use DBIC::SqlMakerTest;
10 use DBIC::DebugObj;
11
12 my $schema = DBICTest->init_schema();
13
14 # Check the defined unique constraints
15 is_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 );
20 is_deeply(
21   [ sort $schema->source('Producer')->unique_constraint_names ],
22   [ qw/primary prod_name/ ],
23   'Producer source has a named unique constraint'
24 );
25 is_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 );
30 is_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 );
35
36 my $artistid = 1;
37 my $title    = 'UNIQUE Constraint';
38
39 my $cd1 = $schema->resultset('CD')->find_or_create({
40   artist => $artistid,
41   title  => $title,
42   year   => 2005,
43 });
44
45 my $cd2 = $schema->resultset('CD')->find(
46   {
47     artist => $artistid,
48     title  => $title,
49   },
50   { key => 'cd_artist_title' }
51 );
52
53 is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
54 is($cd2->title, $cd1->title, 'title is correct');
55 is($cd2->year, $cd1->year, 'year is correct');
56
57 my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' });
58
59 is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
60 is($cd3->title, $cd1->title, 'title is correct');
61 is($cd3->year, $cd1->year, 'year is correct');
62
63 my $cd4 = $schema->resultset('CD')->update_or_create(
64   {
65     artist => $artistid,
66     title  => $title,
67     year   => 2007,
68   },
69 );
70
71 ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
72 is($cd4->cdid, $cd2->cdid, 'cdid is correct');
73 is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
74 is($cd4->title, $cd2->title, 'title is correct');
75 is($cd4->year, 2007, 'updated year is correct');
76
77 my $cd5 = $schema->resultset('CD')->update_or_create(
78   {
79     artist => $artistid,
80     title  => $title,
81     year   => 2007,
82   },
83   { key => 'cd_artist_title' }
84 );
85
86 ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
87 is($cd5->cdid, $cd2->cdid, 'cdid is correct');
88 is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
89 is($cd5->title, $cd2->title, 'title is correct');
90 is($cd5->year, 2007, 'updated year is correct');
91
92 my $cd6 = $schema->resultset('CD')->update_or_create(
93   {
94     cdid   => $cd2->cdid,
95     artist => 1,
96     title  => $cd2->title,
97     year   => 2005,
98   },
99   { key => 'primary' }
100 );
101
102 ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
103 is($cd6->cdid, $cd2->cdid, 'cdid is correct');
104 is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
105 is($cd6->title, $cd2->title, 'title is correct');
106 is($cd6->year, 2005, 'updated year is correct');
107
108 my $cd7 = $schema->resultset('CD')->find_or_create(
109   {
110     artist => $artistid,
111     title  => $title,
112     year   => 2010,
113   },
114   { key => 'cd_artist_title' }
115 );
116
117 is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
118 is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
119 is($cd7->title, $cd1->title, 'title is correct');
120 is($cd7->year, $cd1->year, 'year is correct');
121
122 my $artist = $schema->resultset('Artist')->find($artistid);
123 my $cd8 = $artist->find_or_create_related('cds',
124   {
125     title  => $title,
126     year   => 2020,
127   },
128   { key => 'cd_artist_title' }
129 );
130
131 is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
132 is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
133 is($cd8->title, $cd1->title, 'title is correct');
134 is($cd8->year, $cd1->year, 'year is correct');
135
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 });
142 my $cd9 = $artist->cds->update_or_create(
143   {
144     cdid   => $cd1->cdid,
145     title  => $title,
146     year   => 2021,
147   },
148   { key => 'cd_artist_title' }
149 );
150
151 ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
152 is($cd9->cdid, $cd1->cdid, 'cdid is correct');
153 is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
154 is($cd9->title, $cd1->title, 'title is correct');
155 is($cd9->year, 2021, 'year is correct');
156
157 # Table with two unique constraints, and we're satisying one of them
158 my $track = $schema->resultset('Track')->find(
159   {
160     cd       => 1,
161     position => 3,
162   },
163   { order_by => 'position' }
164 );
165
166 is($track->get_column('cd'), 1, 'track cd is correct');
167 is($track->get_column('position'), 3, 'track position is correct');
168
169 # Test a table with a unique constraint but no primary key
170 my $row = $schema->resultset('NoPrimaryKey')->update_or_create(
171   {
172     foo => 1,
173     bar => 2,
174     baz => 3,
175   },
176   { key => 'foo_bar' }
177 );
178
179 ok(! $row->is_changed, 'update_or_create on table without primary key: row is clean');
180 is($row->foo, 1, 'foo is correct');
181 is($row->bar, 2, 'bar is correct');
182 is($row->baz, 3, 'baz is correct');
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 }
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
211     is($cd1->in_storage, 0, 'CD is not in storage yet after update_or_new');
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');
225 }
226
227 # make sure the ident condition is assembled sanely
228 {
229   my $artist = $schema->resultset('Artist')->next;
230
231   my ($sql, @bind);
232   my $old_debugobj = $schema->storage->debugobj;
233   my $old_debug = $schema->storage->debug;
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
246   $schema->storage->debug($old_debug);
247   $schema->storage->debugobj($old_debugobj);
248 }
249
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;
268 MOD
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 }
272 # make sure NULL is not considered condition-deterministic
273 my $art_rs = $schema->resultset('Artist')->search({}, { order_by => 'artistid' });
274 $art_rs->create ({ artistid => $_ + 640, name => "Outranked $_" }) for (1..2);
275 warnings_are {
276   is(
277     $art_rs->find ({ artistid => 642, rank => 13, charfield => undef })->name,
278     'Outranked 2',
279     'Correct artist retrieved with find'
280   );
281
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';
288
289 done_testing;
290