Commit | Line | Data |
e8aef98d |
1 | ## ---------------------------------------------------------------------------- |
2 | ## Tests for the $resultset->populate method. |
3 | ## |
4 | ## GOALS: We need to test the method for both void and array context for all |
5 | ## the following relationship types: belongs_to, has_many. Additionally we |
8b280eee |
6 | ## need to test each of those for both specified PK's and autogenerated PK's |
e8aef98d |
7 | ## |
8 | ## Also need to test some stuff that should generate errors. |
9 | ## ---------------------------------------------------------------------------- |
10 | |
81ab7888 |
11 | use strict; |
12 | use warnings; |
13 | |
14 | use Test::More; |
15 | use lib qw(t/lib); |
16 | use DBICTest; |
17 | |
81ab7888 |
18 | |
e8aef98d |
19 | ## ---------------------------------------------------------------------------- |
20 | ## Get a Schema and some ResultSets we can play with. |
21 | ## ---------------------------------------------------------------------------- |
22 | |
d7f20fdf |
23 | my $schema = DBICTest->init_schema(); |
24 | my $art_rs = $schema->resultset('Artist'); |
25 | my $cd_rs = $schema->resultset('CD'); |
7ec05166 |
26 | |
8d005ad9 |
27 | my $restricted_art_rs = $art_rs->search({ -and => [ rank => 42, charfield => { '=', \['(SELECT MAX(artistid) FROM artist) + ?', 6] } ] }); |
e8aef98d |
28 | |
29 | ok( $schema, 'Got a Schema object'); |
30 | ok( $art_rs, 'Got Good Artist Resultset'); |
31 | ok( $cd_rs, 'Got Good CD Resultset'); |
32 | |
33 | |
34 | ## ---------------------------------------------------------------------------- |
8b93a938 |
35 | ## Schema populate Tests |
36 | ## ---------------------------------------------------------------------------- |
37 | |
7c325d53 |
38 | SCHEMA_POPULATE1: { |
8b93a938 |
39 | |
d7f20fdf |
40 | ## Test to make sure that the old $schema->populate is using the new method |
41 | ## for $resultset->populate when in void context and with sub objects. |
42 | |
43 | $schema->populate('Artist', [ |
44 | |
45 | [qw/name cds/], |
46 | ["001First Artist", [ |
47 | {title=>"001Title1", year=>2000}, |
48 | {title=>"001Title2", year=>2001}, |
49 | {title=>"001Title3", year=>2002}, |
50 | ]], |
51 | ["002Second Artist", []], |
52 | ["003Third Artist", [ |
53 | {title=>"003Title1", year=>2005}, |
54 | ]], |
55 | [undef, [ |
56 | {title=>"004Title1", year=>2010} |
57 | ]], |
58 | ]); |
59 | |
60 | isa_ok $schema, 'DBIx::Class::Schema'; |
61 | |
62 | my ($undef, $artist1, $artist2, $artist3 ) = $schema->resultset('Artist')->search({ |
63 | name=>["001First Artist","002Second Artist","003Third Artist", undef]}, |
64 | {order_by=>'name ASC'})->all; |
65 | |
66 | isa_ok $artist1, 'DBICTest::Artist'; |
67 | isa_ok $artist2, 'DBICTest::Artist'; |
68 | isa_ok $artist3, 'DBICTest::Artist'; |
8273e845 |
69 | isa_ok $undef, 'DBICTest::Artist'; |
d7f20fdf |
70 | |
71 | ok $artist1->name eq '001First Artist', "Got Expected Artist Name for Artist001"; |
72 | ok $artist2->name eq '002Second Artist', "Got Expected Artist Name for Artist002"; |
73 | ok $artist3->name eq '003Third Artist', "Got Expected Artist Name for Artist003"; |
8273e845 |
74 | ok !defined $undef->name, "Got Expected Artist Name for Artist004"; |
d7f20fdf |
75 | |
76 | ok $artist1->cds->count eq 3, "Got Right number of CDs for Artist1"; |
77 | ok $artist2->cds->count eq 0, "Got Right number of CDs for Artist2"; |
78 | ok $artist3->cds->count eq 1, "Got Right number of CDs for Artist3"; |
8273e845 |
79 | ok $undef->cds->count eq 1, "Got Right number of CDs for Artist4"; |
d7f20fdf |
80 | |
81 | ARTIST1CDS: { |
82 | |
83 | my ($cd1, $cd2, $cd3) = $artist1->cds->search(undef, {order_by=>'year ASC'}); |
84 | |
85 | isa_ok $cd1, 'DBICTest::CD'; |
86 | isa_ok $cd2, 'DBICTest::CD'; |
87 | isa_ok $cd3, 'DBICTest::CD'; |
88 | |
89 | ok $cd1->year == 2000; |
90 | ok $cd2->year == 2001; |
91 | ok $cd3->year == 2002; |
92 | |
93 | ok $cd1->title eq '001Title1'; |
94 | ok $cd2->title eq '001Title2'; |
95 | ok $cd3->title eq '001Title3'; |
96 | } |
97 | |
98 | ARTIST3CDS: { |
99 | |
100 | my ($cd1) = $artist3->cds->search(undef, {order_by=>'year ASC'}); |
101 | |
102 | isa_ok $cd1, 'DBICTest::CD'; |
103 | |
104 | ok $cd1->year == 2005; |
105 | ok $cd1->title eq '003Title1'; |
106 | } |
107 | |
108 | ARTIST4CDS: { |
109 | |
110 | my ($cd1) = $undef->cds->search(undef, {order_by=>'year ASC'}); |
111 | |
112 | isa_ok $cd1, 'DBICTest::CD'; |
113 | |
114 | ok $cd1->year == 2010; |
115 | ok $cd1->title eq '004Title1'; |
116 | } |
117 | |
118 | ## Need to do some cleanup so that later tests don't get borked |
119 | |
120 | $undef->delete; |
8b93a938 |
121 | } |
122 | |
123 | |
124 | ## ---------------------------------------------------------------------------- |
e8aef98d |
125 | ## Array context tests |
126 | ## ---------------------------------------------------------------------------- |
127 | |
128 | ARRAY_CONTEXT: { |
129 | |
d7f20fdf |
130 | ## These first set of tests are cake because array context just delegates |
3334d204 |
131 | ## all its processing to $resultset->create |
d7f20fdf |
132 | |
133 | HAS_MANY_NO_PKS: { |
134 | |
135 | ## This first group of tests checks to make sure we can call populate |
136 | ## with the parent having many children and let the keys be automatic |
137 | |
138 | my $artists = [ |
139 | { |
140 | name => 'Angsty-Whiny Girl', |
141 | cds => [ |
142 | { title => 'My First CD', year => 2006 }, |
143 | { title => 'Yet More Tweeny-Pop crap', year => 2007 }, |
144 | ], |
145 | }, |
146 | { |
147 | name => 'Manufactured Crap', |
148 | }, |
149 | { |
150 | name => 'Like I Give a Damn', |
151 | cds => [ |
152 | { title => 'My parents sold me to a record company' ,year => 2005 }, |
153 | { title => 'Why Am I So Ugly?', year => 2006 }, |
154 | { title => 'I Got Surgery and am now Popular', year => 2007 } |
155 | ], |
156 | }, |
157 | { |
158 | name => 'Formerly Named', |
159 | cds => [ |
160 | { title => 'One Hit Wonder', year => 2006 }, |
161 | ], |
162 | }, |
163 | ]; |
164 | |
165 | ## Get the result row objects. |
166 | |
167 | my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists); |
168 | |
169 | ## Do we have the right object? |
170 | |
171 | isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'"); |
172 | isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'"); |
8273e845 |
173 | isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'"); |
d7f20fdf |
174 | isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'"); |
175 | |
176 | ## Find the expected information? |
177 | |
178 | ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object"); |
179 | ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object"); |
180 | ok( $damn->name eq 'Like I Give a Damn', "Got Correct name for result object"); |
181 | ok( $formerly->name eq 'Formerly Named', "Got Correct name for result object"); |
182 | |
183 | ## Create the expected children sub objects? |
184 | |
185 | ok( $crap->cds->count == 0, "got Expected Number of Cds"); |
186 | ok( $girl->cds->count == 2, "got Expected Number of Cds"); |
187 | ok( $damn->cds->count == 3, "got Expected Number of Cds"); |
188 | ok( $formerly->cds->count == 1, "got Expected Number of Cds"); |
189 | |
190 | ## Did the cds get expected information? |
191 | |
192 | my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year'}); |
193 | |
194 | ok( $cd1->title eq "My First CD", "Got Expected CD Title"); |
195 | ok( $cd2->title eq "Yet More Tweeny-Pop crap", "Got Expected CD Title"); |
196 | } |
197 | |
198 | HAS_MANY_WITH_PKS: { |
199 | |
200 | ## This group tests the ability to specify the PK in the parent and let |
201 | ## DBIC transparently pass the PK down to the Child and also let's the |
202 | ## child create any other needed PK's for itself. |
203 | |
204 | my $aid = $art_rs->get_column('artistid')->max || 0; |
205 | |
206 | my $first_aid = ++$aid; |
207 | |
208 | my $artists = [ |
209 | { |
210 | artistid => $first_aid, |
211 | name => 'PK_Angsty-Whiny Girl', |
212 | cds => [ |
213 | { artist => $first_aid, title => 'PK_My First CD', year => 2006 }, |
214 | { artist => $first_aid, title => 'PK_Yet More Tweeny-Pop crap', year => 2007 }, |
215 | ], |
216 | }, |
217 | { |
218 | artistid => ++$aid, |
219 | name => 'PK_Manufactured Crap', |
220 | }, |
221 | { |
222 | artistid => ++$aid, |
223 | name => 'PK_Like I Give a Damn', |
224 | cds => [ |
225 | { title => 'PK_My parents sold me to a record company' ,year => 2005 }, |
226 | { title => 'PK_Why Am I So Ugly?', year => 2006 }, |
227 | { title => 'PK_I Got Surgery and am now Popular', year => 2007 } |
228 | ], |
229 | }, |
230 | { |
231 | artistid => ++$aid, |
232 | name => 'PK_Formerly Named', |
233 | cds => [ |
234 | { title => 'PK_One Hit Wonder', year => 2006 }, |
235 | ], |
236 | }, |
237 | ]; |
238 | |
239 | ## Get the result row objects. |
240 | |
241 | my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists); |
242 | |
243 | ## Do we have the right object? |
244 | |
245 | isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'"); |
246 | isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'"); |
8273e845 |
247 | isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'"); |
d7f20fdf |
248 | isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'"); |
249 | |
250 | ## Find the expected information? |
251 | |
252 | ok( $crap->name eq 'PK_Manufactured Crap', "Got Correct name for result object"); |
253 | ok( $girl->name eq 'PK_Angsty-Whiny Girl', "Got Correct name for result object"); |
254 | ok( $girl->artistid == $first_aid, "Got Correct artist PK for result object"); |
255 | ok( $damn->name eq 'PK_Like I Give a Damn', "Got Correct name for result object"); |
256 | ok( $formerly->name eq 'PK_Formerly Named', "Got Correct name for result object"); |
257 | |
258 | ## Create the expected children sub objects? |
259 | |
260 | ok( $crap->cds->count == 0, "got Expected Number of Cds"); |
8273e845 |
261 | ok( $girl->cds->count == 2, "got Expected Number of Cds"); |
d7f20fdf |
262 | ok( $damn->cds->count == 3, "got Expected Number of Cds"); |
263 | ok( $formerly->cds->count == 1, "got Expected Number of Cds"); |
264 | |
265 | ## Did the cds get expected information? |
266 | |
267 | my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'}); |
268 | |
269 | ok( $cd1->title eq "PK_My First CD", "Got Expected CD Title"); |
270 | ok( $cd2->title eq "PK_Yet More Tweeny-Pop crap", "Got Expected CD Title"); |
271 | } |
272 | |
273 | BELONGS_TO_NO_PKs: { |
274 | |
8273e845 |
275 | ## Test from a belongs_to perspective, should create artist first, |
d7f20fdf |
276 | ## then CD with artistid. This test we let the system automatically |
277 | ## create the PK's. Chances are good you'll use it this way mostly. |
278 | |
279 | my $cds = [ |
280 | { |
281 | title => 'Some CD3', |
282 | year => '1997', |
283 | artist => { name => 'Fred BloggsC'}, |
284 | }, |
285 | { |
286 | title => 'Some CD4', |
287 | year => '1997', |
288 | artist => { name => 'Fred BloggsD'}, |
8273e845 |
289 | }, |
d7f20fdf |
290 | ]; |
291 | |
292 | my ($cdA, $cdB) = $cd_rs->populate($cds); |
293 | |
294 | |
295 | isa_ok($cdA, 'DBICTest::CD', 'Created CD'); |
296 | isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist'); |
297 | is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC'); |
298 | |
299 | |
300 | isa_ok($cdB, 'DBICTest::CD', 'Created CD'); |
301 | isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist'); |
302 | is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD'); |
303 | } |
304 | |
305 | BELONGS_TO_WITH_PKs: { |
306 | |
8273e845 |
307 | ## Test from a belongs_to perspective, should create artist first, |
d7f20fdf |
308 | ## then CD with artistid. This time we try setting the PK's |
309 | |
310 | my $aid = $art_rs->get_column('artistid')->max || 0; |
311 | |
312 | my $cds = [ |
313 | { |
314 | title => 'Some CD3', |
315 | year => '1997', |
84f7e8a1 |
316 | artist => { artistid=> ++$aid, name => 'Fred BloggsE'}, |
d7f20fdf |
317 | }, |
318 | { |
319 | title => 'Some CD4', |
320 | year => '1997', |
84f7e8a1 |
321 | artist => { artistid=> ++$aid, name => 'Fred BloggsF'}, |
8273e845 |
322 | }, |
d7f20fdf |
323 | ]; |
324 | |
325 | my ($cdA, $cdB) = $cd_rs->populate($cds); |
326 | |
327 | isa_ok($cdA, 'DBICTest::CD', 'Created CD'); |
328 | isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist'); |
84f7e8a1 |
329 | is($cdA->artist->name, 'Fred BloggsE', 'Set Artist to FredE'); |
d7f20fdf |
330 | |
331 | isa_ok($cdB, 'DBICTest::CD', 'Created CD'); |
332 | isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist'); |
84f7e8a1 |
333 | is($cdB->artist->name, 'Fred BloggsF', 'Set Artist to FredF'); |
d7f20fdf |
334 | ok($cdB->artist->artistid == $aid, "Got Expected Artist ID"); |
335 | } |
7ec05166 |
336 | |
337 | WITH_COND_FROM_RS: { |
d7f20fdf |
338 | |
7ec05166 |
339 | my ($more_crap) = $restricted_art_rs->populate([ |
340 | { |
341 | name => 'More Manufactured Crap', |
342 | }, |
343 | ]); |
d7f20fdf |
344 | |
7ec05166 |
345 | ## Did it use the condition in the resultset? |
8d005ad9 |
346 | $more_crap->discard_changes; |
7ec05166 |
347 | cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object"); |
8d005ad9 |
348 | cmp_ok( $more_crap->charfield, '==', $more_crap->id + 5, "Got Correct charfield for result object"); |
8273e845 |
349 | } |
e8aef98d |
350 | } |
71d496fe |
351 | |
71d496fe |
352 | |
e8aef98d |
353 | ## ---------------------------------------------------------------------------- |
354 | ## Void context tests |
355 | ## ---------------------------------------------------------------------------- |
71d496fe |
356 | |
e8aef98d |
357 | VOID_CONTEXT: { |
71d496fe |
358 | |
8273e845 |
359 | ## All these tests check the ability to use populate without asking for |
d7f20fdf |
360 | ## any returned resultsets. This uses bulk_insert as much as possible |
361 | ## in order to increase speed. |
362 | |
363 | HAS_MANY_WITH_PKS: { |
364 | |
365 | ## This first group of tests checks to make sure we can call populate |
366 | ## with the parent having many children and the parent PK is set |
367 | |
368 | my $aid = $art_rs->get_column('artistid')->max || 0; |
369 | |
370 | my $first_aid = ++$aid; |
371 | |
372 | my $artists = [ |
373 | { |
374 | artistid => $first_aid, |
375 | name => 'VOID_PK_Angsty-Whiny Girl', |
376 | cds => [ |
377 | { artist => $first_aid, title => 'VOID_PK_My First CD', year => 2006 }, |
378 | { artist => $first_aid, title => 'VOID_PK_Yet More Tweeny-Pop crap', year => 2007 }, |
379 | ], |
380 | }, |
381 | { |
382 | artistid => ++$aid, |
383 | name => 'VOID_PK_Manufactured Crap', |
384 | }, |
385 | { |
386 | artistid => ++$aid, |
387 | name => 'VOID_PK_Like I Give a Damn', |
388 | cds => [ |
389 | { title => 'VOID_PK_My parents sold me to a record company' ,year => 2005 }, |
390 | { title => 'VOID_PK_Why Am I So Ugly?', year => 2006 }, |
8273e845 |
391 | { title => 'VOID_PK_I Got Surgery and am now Popular', year => 2007 } |
d7f20fdf |
392 | ], |
393 | }, |
394 | { |
395 | artistid => ++$aid, |
396 | name => 'VOID_PK_Formerly Named', |
397 | cds => [ |
398 | { title => 'VOID_PK_One Hit Wonder', year => 2006 }, |
399 | ], |
400 | }, |
401 | { |
402 | artistid => ++$aid, |
403 | name => undef, |
404 | cds => [ |
405 | { title => 'VOID_PK_Zundef test', year => 2006 }, |
406 | ], |
407 | }, |
408 | ]; |
409 | |
410 | ## Get the result row objects. |
411 | |
412 | $art_rs->populate($artists); |
413 | |
414 | my ($undef, $girl, $formerly, $damn, $crap) = $art_rs->search( |
415 | |
416 | {name=>[ map { $_->{name} } @$artists]}, |
417 | {order_by=>'name ASC'}, |
418 | ); |
419 | |
420 | ## Do we have the right object? |
421 | |
422 | isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'"); |
423 | isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'"); |
8273e845 |
424 | isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'"); |
425 | isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'"); |
426 | isa_ok( $undef, 'DBICTest::Artist', "Got 'Artist'"); |
d7f20fdf |
427 | |
428 | ## Find the expected information? |
429 | |
430 | ok( $crap->name eq 'VOID_PK_Manufactured Crap', "Got Correct name 'VOID_PK_Manufactured Crap' for result object"); |
431 | ok( $girl->name eq 'VOID_PK_Angsty-Whiny Girl', "Got Correct name for result object"); |
8273e845 |
432 | ok( $damn->name eq 'VOID_PK_Like I Give a Damn', "Got Correct name for result object"); |
d7f20fdf |
433 | ok( $formerly->name eq 'VOID_PK_Formerly Named', "Got Correct name for result object"); |
8273e845 |
434 | ok( !defined $undef->name, "Got Correct name 'is undef' for result object"); |
d7f20fdf |
435 | |
436 | ## Create the expected children sub objects? |
437 | ok( $crap->can('cds'), "Has cds relationship"); |
438 | ok( $girl->can('cds'), "Has cds relationship"); |
439 | ok( $damn->can('cds'), "Has cds relationship"); |
440 | ok( $formerly->can('cds'), "Has cds relationship"); |
8273e845 |
441 | ok( $undef->can('cds'), "Has cds relationship"); |
d7f20fdf |
442 | |
443 | ok( $crap->cds->count == 0, "got Expected Number of Cds"); |
8273e845 |
444 | ok( $girl->cds->count == 2, "got Expected Number of Cds"); |
d7f20fdf |
445 | ok( $damn->cds->count == 3, "got Expected Number of Cds"); |
446 | ok( $formerly->cds->count == 1, "got Expected Number of Cds"); |
447 | ok( $undef->cds->count == 1, "got Expected Number of Cds"); |
448 | |
449 | ## Did the cds get expected information? |
450 | |
451 | my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'}); |
452 | |
453 | ok( $cd1->title eq "VOID_PK_My First CD", "Got Expected CD Title"); |
454 | ok( $cd2->title eq "VOID_PK_Yet More Tweeny-Pop crap", "Got Expected CD Title"); |
455 | } |
456 | |
457 | |
458 | BELONGS_TO_WITH_PKs: { |
459 | |
8273e845 |
460 | ## Test from a belongs_to perspective, should create artist first, |
d7f20fdf |
461 | ## then CD with artistid. This time we try setting the PK's |
462 | |
463 | my $aid = $art_rs->get_column('artistid')->max || 0; |
464 | |
465 | my $cds = [ |
466 | { |
467 | title => 'Some CD3B', |
468 | year => '1997', |
469 | artist => { artistid=> ++$aid, name => 'Fred BloggsCB'}, |
470 | }, |
471 | { |
472 | title => 'Some CD4B', |
473 | year => '1997', |
474 | artist => { artistid=> ++$aid, name => 'Fred BloggsDB'}, |
475 | }, |
476 | ]; |
477 | |
478 | $cd_rs->populate($cds); |
479 | |
480 | my ($cdA, $cdB) = $cd_rs->search( |
481 | {title=>[sort map {$_->{title}} @$cds]}, |
482 | {order_by=>'title ASC'}, |
483 | ); |
484 | |
485 | isa_ok($cdA, 'DBICTest::CD', 'Created CD'); |
486 | isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist'); |
487 | is($cdA->artist->name, 'Fred BloggsCB', 'Set Artist to FredCB'); |
488 | |
489 | isa_ok($cdB, 'DBICTest::CD', 'Created CD'); |
490 | isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist'); |
491 | is($cdB->artist->name, 'Fred BloggsDB', 'Set Artist to FredDB'); |
492 | ok($cdB->artist->artistid == $aid, "Got Expected Artist ID"); |
493 | } |
494 | |
495 | BELONGS_TO_NO_PKs: { |
496 | |
8273e845 |
497 | ## Test from a belongs_to perspective, should create artist first, |
d7f20fdf |
498 | ## then CD with artistid. |
499 | |
500 | my $cds = [ |
501 | { |
502 | title => 'Some CD3BB', |
503 | year => '1997', |
504 | artist => { name => 'Fred BloggsCBB'}, |
505 | }, |
506 | { |
507 | title => 'Some CD4BB', |
508 | year => '1997', |
509 | artist => { name => 'Fred BloggsDBB'}, |
510 | }, |
511 | { |
512 | title => 'Some CD5BB', |
513 | year => '1997', |
514 | artist => { name => undef}, |
8273e845 |
515 | }, |
d7f20fdf |
516 | ]; |
517 | |
518 | $cd_rs->populate($cds); |
519 | |
520 | my ($cdA, $cdB, $cdC) = $cd_rs->search( |
521 | {title=>[sort map {$_->{title}} @$cds]}, |
522 | {order_by=>'title ASC'}, |
523 | ); |
524 | |
525 | isa_ok($cdA, 'DBICTest::CD', 'Created CD'); |
526 | isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist'); |
527 | is($cdA->title, 'Some CD3BB', 'Found Expected title'); |
528 | is($cdA->artist->name, 'Fred BloggsCBB', 'Set Artist to FredCBB'); |
529 | |
530 | isa_ok($cdB, 'DBICTest::CD', 'Created CD'); |
531 | isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist'); |
532 | is($cdB->title, 'Some CD4BB', 'Found Expected title'); |
533 | is($cdB->artist->name, 'Fred BloggsDBB', 'Set Artist to FredDBB'); |
534 | |
535 | isa_ok($cdC, 'DBICTest::CD', 'Created CD'); |
536 | isa_ok($cdC->artist, 'DBICTest::Artist', 'Set Artist'); |
537 | is($cdC->title, 'Some CD5BB', 'Found Expected title'); |
538 | is( $cdC->artist->name, undef, 'Set Artist to something undefined'); |
539 | } |
540 | |
541 | |
542 | HAS_MANY_NO_PKS: { |
543 | |
544 | ## This first group of tests checks to make sure we can call populate |
545 | ## with the parent having many children and let the keys be automatic |
546 | |
547 | my $artists = [ |
8273e845 |
548 | { |
d7f20fdf |
549 | name => 'VOID_Angsty-Whiny Girl', |
550 | cds => [ |
551 | { title => 'VOID_My First CD', year => 2006 }, |
552 | { title => 'VOID_Yet More Tweeny-Pop crap', year => 2007 }, |
8273e845 |
553 | ], |
554 | }, |
d7f20fdf |
555 | { |
556 | name => 'VOID_Manufactured Crap', |
557 | }, |
558 | { |
559 | name => 'VOID_Like I Give a Damn', |
560 | cds => [ |
561 | { title => 'VOID_My parents sold me to a record company' ,year => 2005 }, |
562 | { title => 'VOID_Why Am I So Ugly?', year => 2006 }, |
8273e845 |
563 | { title => 'VOID_I Got Surgery and am now Popular', year => 2007 } |
d7f20fdf |
564 | ], |
565 | }, |
8273e845 |
566 | { |
d7f20fdf |
567 | name => 'VOID_Formerly Named', |
568 | cds => [ |
569 | { title => 'VOID_One Hit Wonder', year => 2006 }, |
8273e845 |
570 | ], |
571 | }, |
d7f20fdf |
572 | ]; |
573 | |
574 | ## Get the result row objects. |
575 | |
576 | $art_rs->populate($artists); |
577 | |
578 | my ($girl, $formerly, $damn, $crap) = $art_rs->search( |
579 | {name=>[sort map {$_->{name}} @$artists]}, |
580 | {order_by=>'name ASC'}, |
581 | ); |
582 | |
583 | ## Do we have the right object? |
584 | |
585 | isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'"); |
586 | isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'"); |
8273e845 |
587 | isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'"); |
588 | isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'"); |
d7f20fdf |
589 | |
590 | ## Find the expected information? |
591 | |
592 | ok( $crap->name eq 'VOID_Manufactured Crap', "Got Correct name for result object"); |
593 | ok( $girl->name eq 'VOID_Angsty-Whiny Girl', "Got Correct name for result object"); |
8273e845 |
594 | ok( $damn->name eq 'VOID_Like I Give a Damn', "Got Correct name for result object"); |
d7f20fdf |
595 | ok( $formerly->name eq 'VOID_Formerly Named', "Got Correct name for result object"); |
596 | |
597 | ## Create the expected children sub objects? |
598 | ok( $crap->can('cds'), "Has cds relationship"); |
599 | ok( $girl->can('cds'), "Has cds relationship"); |
600 | ok( $damn->can('cds'), "Has cds relationship"); |
601 | ok( $formerly->can('cds'), "Has cds relationship"); |
602 | |
603 | ok( $crap->cds->count == 0, "got Expected Number of Cds"); |
8273e845 |
604 | ok( $girl->cds->count == 2, "got Expected Number of Cds"); |
d7f20fdf |
605 | ok( $damn->cds->count == 3, "got Expected Number of Cds"); |
606 | ok( $formerly->cds->count == 1, "got Expected Number of Cds"); |
607 | |
608 | ## Did the cds get expected information? |
609 | |
610 | my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'}); |
611 | |
612 | ok($cd1, "Got a got CD"); |
613 | ok($cd2, "Got a got CD"); |
614 | ok( $cd1->title eq "VOID_My First CD", "Got Expected CD Title"); |
615 | ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title"); |
616 | } |
d21161f5 |
617 | |
7ec05166 |
618 | WITH_COND_FROM_RS: { |
d7f20fdf |
619 | |
7ec05166 |
620 | $restricted_art_rs->populate([ |
621 | { |
622 | name => 'VOID More Manufactured Crap', |
623 | }, |
624 | ]); |
625 | |
626 | my $more_crap = $art_rs->search({ |
627 | name => 'VOID More Manufactured Crap' |
628 | })->first; |
d7f20fdf |
629 | |
7ec05166 |
630 | ## Did it use the condition in the resultset? |
8d005ad9 |
631 | $more_crap->discard_changes; |
7ec05166 |
632 | cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object"); |
8d005ad9 |
633 | cmp_ok( $more_crap->charfield, '==', $more_crap->id + 5, "Got Correct charfield for result object"); |
8273e845 |
634 | } |
c4e67d31 |
635 | } |
636 | |
637 | ARRAYREF_OF_ARRAYREF_STYLE: { |
7ec05166 |
638 | $art_rs->populate([ |
c4e67d31 |
639 | [qw/artistid name/], |
640 | [1000, 'A Formally Unknown Singer'], |
641 | [1001, 'A singer that jumped the shark two albums ago'], |
642 | [1002, 'An actually cool singer.'], |
643 | ]); |
d7f20fdf |
644 | |
c4e67d31 |
645 | ok my $unknown = $art_rs->find(1000), "got Unknown"; |
646 | ok my $jumped = $art_rs->find(1001), "got Jumped"; |
647 | ok my $cool = $art_rs->find(1002), "got Cool"; |
d7f20fdf |
648 | |
c4e67d31 |
649 | is $unknown->name, 'A Formally Unknown Singer', 'Correct Name'; |
650 | is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name'; |
651 | is $cool->name, 'An actually cool singer.', 'Correct Name'; |
d7f20fdf |
652 | |
fa7a51af |
653 | my ($cooler, $lamer) = $restricted_art_rs->populate([ |
c4e67d31 |
654 | [qw/artistid name/], |
655 | [1003, 'Cooler'], |
8273e845 |
656 | [1004, 'Lamer'], |
c4e67d31 |
657 | ]); |
d7f20fdf |
658 | |
c4e67d31 |
659 | is $cooler->name, 'Cooler', 'Correct Name'; |
8273e845 |
660 | is $lamer->name, 'Lamer', 'Correct Name'; |
af928332 |
661 | |
8d005ad9 |
662 | for ($cooler, $lamer) { |
663 | $_->discard_changes; |
664 | cmp_ok( $_->rank, '==', 42, "Got Correct rank for result object"); |
665 | cmp_ok( $_->charfield, '==', $_->id + 5, "Got Correct charfield for result object"); |
666 | } |
7ec05166 |
667 | |
668 | ARRAY_CONTEXT_WITH_COND_FROM_RS: { |
d7f20fdf |
669 | |
7ec05166 |
670 | my ($mega_lamer) = $restricted_art_rs->populate([ |
671 | { |
672 | name => 'Mega Lamer', |
673 | }, |
674 | ]); |
675 | |
676 | ## Did it use the condition in the resultset? |
8d005ad9 |
677 | $mega_lamer->discard_changes; |
7ec05166 |
678 | cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object"); |
8d005ad9 |
679 | cmp_ok( $mega_lamer->charfield, '==', $mega_lamer->id + 5, "Got Correct charfield for result object"); |
8273e845 |
680 | } |
7ec05166 |
681 | |
682 | VOID_CONTEXT_WITH_COND_FROM_RS: { |
d7f20fdf |
683 | |
7ec05166 |
684 | $restricted_art_rs->populate([ |
685 | { |
686 | name => 'VOID Mega Lamer', |
687 | }, |
688 | ]); |
689 | |
690 | my $mega_lamer = $art_rs->search({ |
691 | name => 'VOID Mega Lamer' |
692 | })->first; |
d7f20fdf |
693 | |
7ec05166 |
694 | ## Did it use the condition in the resultset? |
695 | cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object"); |
8d005ad9 |
696 | cmp_ok( $mega_lamer->charfield, '==', $mega_lamer->id + 5, "Got Correct charfield for result object"); |
d7f20fdf |
697 | } |
af928332 |
698 | } |
fa238f1f |
699 | |
ce855fff |
700 | ok(eval { $art_rs->populate([]); 1 }, "Empty populate runs but does nothing"); |
701 | |
fa238f1f |
702 | done_testing; |