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