Commit | Line | Data |
70350518 |
1 | use strict; |
246fa39d |
2 | use warnings; |
70350518 |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
246fa39d |
7 | use DBIC::SqlMakerTest; |
8 | use DBIC::DebugObj; |
70350518 |
9 | |
a47e1233 |
10 | my $schema = DBICTest->init_schema(); |
87f0da6a |
11 | |
365d06b7 |
12 | # Check the defined unique constraints |
13 | is_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 | ); |
18 | is_deeply( |
19 | [ sort $schema->source('Producer')->unique_constraint_names ], |
20 | [ qw/primary prod_name/ ], |
21 | 'Producer source has a named unique constraint' |
22 | ); |
23 | is_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 | |
29 | my $artistid = 1; |
30 | my $title = 'UNIQUE Constraint'; |
31 | |
32 | my $cd1 = $schema->resultset('CD')->find_or_create({ |
33 | artist => $artistid, |
34 | title => $title, |
35 | year => 2005, |
36 | }); |
37 | |
38 | my $cd2 = $schema->resultset('CD')->find( |
39 | { |
40 | artist => $artistid, |
41 | title => $title, |
42 | }, |
368a5228 |
43 | { key => 'cd_artist_title' } |
87f0da6a |
44 | ); |
45 | |
46 | is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct'); |
47 | is($cd2->title, $cd1->title, 'title is correct'); |
48 | is($cd2->year, $cd1->year, 'year is correct'); |
49 | |
368a5228 |
50 | my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' }); |
c9bb4b2f |
51 | |
52 | is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct'); |
53 | is($cd3->title, $cd1->title, 'title is correct'); |
54 | is($cd3->year, $cd1->year, 'year is correct'); |
55 | |
56 | my $cd4 = $schema->resultset('CD')->update_or_create( |
87f0da6a |
57 | { |
58 | artist => $artistid, |
59 | title => $title, |
60 | year => 2007, |
61 | }, |
62 | ); |
63 | |
c9bb4b2f |
64 | ok(! $cd4->is_changed, 'update_or_create without key: row is clean'); |
65 | is($cd4->cdid, $cd2->cdid, 'cdid is correct'); |
66 | is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct'); |
67 | is($cd4->title, $cd2->title, 'title is correct'); |
68 | is($cd4->year, 2007, 'updated year is correct'); |
87f0da6a |
69 | |
c9bb4b2f |
70 | my $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 |
79 | ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean'); |
80 | is($cd5->cdid, $cd2->cdid, 'cdid is correct'); |
81 | is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct'); |
82 | is($cd5->title, $cd2->title, 'title is correct'); |
83 | is($cd5->year, 2007, 'updated year is correct'); |
87f0da6a |
84 | |
c9bb4b2f |
85 | my $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 |
95 | ok(! $cd6->is_changed, 'update_or_create by PK: row is clean'); |
96 | is($cd6->cdid, $cd2->cdid, 'cdid is correct'); |
97 | is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct'); |
98 | is($cd6->title, $cd2->title, 'title is correct'); |
99 | is($cd6->year, 2005, 'updated year is correct'); |
87f0da6a |
100 | |
c9bb4b2f |
101 | my $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 |
110 | is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct'); |
111 | is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); |
112 | is($cd7->title, $cd1->title, 'title is correct'); |
113 | is($cd7->year, $cd1->year, 'year is correct'); |
8dc40f3e |
114 | |
115 | my $artist = $schema->resultset('Artist')->find($artistid); |
c9bb4b2f |
116 | my $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 |
124 | is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct'); |
125 | is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); |
126 | is($cd8->title, $cd1->title, 'title is correct'); |
127 | is($cd8->year, $cd1->year, 'year is correct'); |
8dc40f3e |
128 | |
d180c0f3 |
129 | my $cd9 = $artist->cds->update_or_create( |
045120e6 |
130 | { |
d180c0f3 |
131 | cdid => $cd1->cdid, |
045120e6 |
132 | title => $title, |
133 | year => 2021, |
134 | }, |
368a5228 |
135 | { key => 'cd_artist_title' } |
045120e6 |
136 | ); |
137 | |
c9bb4b2f |
138 | ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean'); |
139 | is($cd9->cdid, $cd1->cdid, 'cdid is correct'); |
140 | is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); |
141 | is($cd9->title, $cd1->title, 'title is correct'); |
142 | is($cd9->year, 2021, 'year is correct'); |
045120e6 |
143 | |
dcfb635f |
144 | # Table with two unique constraints, and we're satisying one of them |
365d06b7 |
145 | my $track = $schema->resultset('Track')->find( |
146 | { |
147 | cd => 1, |
148 | position => 3, |
149 | }, |
150 | { order_by => 'position' } |
151 | ); |
152 | |
153 | is($track->get_column('cd'), 1, 'track cd is correct'); |
154 | is($track->get_column('position'), 3, 'track position is correct'); |
89034887 |
155 | |
156 | # Test a table with a unique constraint but no primary key |
157 | my $row = $schema->resultset('NoPrimaryKey')->update_or_create( |
158 | { |
159 | foo => 1, |
160 | bar => 2, |
161 | baz => 3, |
162 | }, |
163 | { key => 'foo_bar' } |
164 | ); |
d180c0f3 |
165 | |
8070a151 |
166 | ok(! $row->is_changed, 'update_or_create on table without primary key: row is clean'); |
89034887 |
167 | is($row->foo, 1, 'foo is correct'); |
168 | is($row->bar, 2, 'bar is correct'); |
169 | is($row->baz, 3, 'baz is correct'); |
d180c0f3 |
170 | |
171 | # Test a unique condition with extra information in the where attr |
172 | { |
173 | my $artist = $schema->resultset('Artist')->find({ artistid => 1 }); |
174 | my $cd = $artist->cds->find_or_new( |
175 | { |
176 | cdid => 1, |
177 | title => 'Not The Real Title', |
178 | year => 3000, |
179 | }, |
180 | { key => 'primary' } |
181 | ); |
182 | |
183 | ok($cd->in_storage, 'find correctly grepped the key across a relationship'); |
184 | is($cd->cdid, 1, 'cdid is correct'); |
185 | } |
9ffa8fd7 |
186 | |
187 | # Test update_or_new |
188 | { |
189 | my $cd1 = $schema->resultset('CD')->update_or_new( |
190 | { |
191 | artist => $artistid, |
192 | title => "SuperHits $$", |
193 | year => 2007, |
194 | }, |
195 | { key => 'cd_artist_title' } |
196 | ); |
197 | |
63bb9738 |
198 | is($cd1->in_storage, 0, 'CD is not in storage yet after update_or_new'); |
9ffa8fd7 |
199 | $cd1->insert; |
200 | ok($cd1->in_storage, 'CD got added to strage after update_or_new && insert'); |
201 | |
202 | my $cd2 = $schema->resultset('CD')->update_or_new( |
203 | { |
204 | artist => $artistid, |
205 | title => "SuperHits $$", |
206 | year => 2008, |
207 | }, |
208 | { key => 'cd_artist_title' } |
209 | ); |
210 | ok($cd2->in_storage, 'Updating year using update_or_new was successful'); |
211 | is($cd2->id, $cd1->id, 'Got the same CD using update_or_new'); |
246fa39d |
212 | } |
213 | |
214 | # make sure the ident condition is assembled sanely |
215 | { |
216 | my $artist = $schema->resultset('Artist')->next; |
217 | |
218 | my ($sql, @bind); |
219 | $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)), |
220 | $schema->storage->debug(1); |
221 | |
222 | $artist->discard_changes; |
223 | |
224 | is_same_sql_bind ( |
225 | $sql, |
226 | \@bind, |
227 | 'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?', |
228 | [qw/'1'/], |
229 | ); |
230 | |
231 | $schema->storage->debug(0); |
232 | $schema->storage->debugobj(undef); |
233 | } |
234 | |
235 | done_testing; |