1 ## ----------------------------------------------------------------------------
2 ## Tests for the $resultset->populate method.
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
6 ## need to test each of those for both specified PK's and autogenerated PK's
8 ## Also need to test some stuff that should generate errors.
9 ## ----------------------------------------------------------------------------
20 ## ----------------------------------------------------------------------------
21 ## Get a Schema and some ResultSets we can play with.
22 ## ----------------------------------------------------------------------------
24 my $schema = DBICTest->init_schema();
25 my $art_rs = $schema->resultset('Artist');
26 my $cd_rs = $schema->resultset('CD');
28 my $restricted_art_rs = $art_rs->search({ -and => [ rank => 42, charfield => { '=', \['(SELECT MAX(artistid) FROM artist) + ?', 6] } ] });
30 ok( $schema, 'Got a Schema object');
31 ok( $art_rs, 'Got Good Artist Resultset');
32 ok( $cd_rs, 'Got Good CD Resultset');
35 ## ----------------------------------------------------------------------------
36 ## Schema populate Tests
37 ## ----------------------------------------------------------------------------
41 # throw a monkey wrench
42 my $post_jnap_monkeywrench = $schema->resultset('Artist')->find(1)->update({ name => undef });
44 warnings_exist { $schema->populate('Artist', [
48 {title=>"001Title1", year=>2000},
49 {title=>"001Title2", year=>2001},
50 {title=>"001Title3", year=>2002},
52 ["002Second Artist", []],
54 {title=>"003Title1", year=>2005},
57 {title=>"004Title1", year=>2010}
59 ]) } qr/\QFast-path populate() of non-uniquely identifiable rows with related data is not possible/;
61 isa_ok $schema, 'DBIx::Class::Schema';
63 my ( $preexisting_undef, $artist1, $artist2, $artist3, $undef ) = $schema->resultset('Artist')->search({
64 name=>["001First Artist","002Second Artist","003Third Artist", undef]},
65 {order_by => { -asc => 'artistid' }})->all;
67 isa_ok $artist1, 'DBICTest::Artist';
68 isa_ok $artist2, 'DBICTest::Artist';
69 isa_ok $artist3, 'DBICTest::Artist';
70 isa_ok $undef, 'DBICTest::Artist';
72 ok $artist1->name eq '001First Artist', "Got Expected Artist Name for Artist001";
73 ok $artist2->name eq '002Second Artist', "Got Expected Artist Name for Artist002";
74 ok $artist3->name eq '003Third Artist', "Got Expected Artist Name for Artist003";
75 ok !defined $undef->name, "Got Expected Artist Name for Artist004";
77 ok $artist1->cds->count eq 3, "Got Right number of CDs for Artist1";
78 ok $artist2->cds->count eq 0, "Got Right number of CDs for Artist2";
79 ok $artist3->cds->count eq 1, "Got Right number of CDs for Artist3";
80 ok $undef->cds->count eq 1, "Got Right number of CDs for Artist4";
82 $post_jnap_monkeywrench->delete;
86 my ($cd1, $cd2, $cd3) = $artist1->cds->search(undef, {order_by=>'year ASC'});
88 isa_ok $cd1, 'DBICTest::CD';
89 isa_ok $cd2, 'DBICTest::CD';
90 isa_ok $cd3, 'DBICTest::CD';
92 ok $cd1->year == 2000;
93 ok $cd2->year == 2001;
94 ok $cd3->year == 2002;
96 ok $cd1->title eq '001Title1';
97 ok $cd2->title eq '001Title2';
98 ok $cd3->title eq '001Title3';
103 my ($cd1) = $artist3->cds->search(undef, {order_by=>'year ASC'});
105 isa_ok $cd1, 'DBICTest::CD';
107 ok $cd1->year == 2005;
108 ok $cd1->title eq '003Title1';
113 my ($cd1) = $undef->cds->search(undef, {order_by=>'year ASC'});
115 isa_ok $cd1, 'DBICTest::CD';
117 ok $cd1->year == 2010;
118 ok $cd1->title eq '004Title1';
121 ## Need to do some cleanup so that later tests don't get borked
127 ## ----------------------------------------------------------------------------
128 ## Array context tests
129 ## ----------------------------------------------------------------------------
133 ## These first set of tests are cake because array context just delegates
134 ## all its processing to $resultset->create
138 ## This first group of tests checks to make sure we can call populate
139 ## with the parent having many children and let the keys be automatic
143 name => 'Angsty-Whiny Girl',
145 { title => 'My First CD', year => 2006 },
146 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
150 name => 'Manufactured Crap',
153 name => 'Like I Give a Damn',
155 { title => 'My parents sold me to a record company' ,year => 2005 },
156 { title => 'Why Am I So Ugly?', year => 2006 },
157 { title => 'I Got Surgery and am now Popular', year => 2007 }
161 name => 'Formerly Named',
163 { title => 'One Hit Wonder', year => 2006 },
168 ## Get the result row objects.
170 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
172 ## Do we have the right object?
174 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
175 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
176 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
177 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
179 ## Find the expected information?
181 ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
182 ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
183 ok( $damn->name eq 'Like I Give a Damn', "Got Correct name for result object");
184 ok( $formerly->name eq 'Formerly Named', "Got Correct name for result object");
186 ## Create the expected children sub objects?
188 ok( $crap->cds->count == 0, "got Expected Number of Cds");
189 ok( $girl->cds->count == 2, "got Expected Number of Cds");
190 ok( $damn->cds->count == 3, "got Expected Number of Cds");
191 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
193 ## Did the cds get expected information?
195 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year'});
197 ok( $cd1->title eq "My First CD", "Got Expected CD Title");
198 ok( $cd2->title eq "Yet More Tweeny-Pop crap", "Got Expected CD Title");
203 ## This group tests the ability to specify the PK in the parent and let
204 ## DBIC transparently pass the PK down to the Child and also let's the
205 ## child create any other needed PK's for itself.
207 my $aid = $art_rs->get_column('artistid')->max || 0;
209 my $first_aid = ++$aid;
213 artistid => $first_aid,
214 name => 'PK_Angsty-Whiny Girl',
216 { artist => $first_aid, title => 'PK_My First CD', year => 2006 },
217 { artist => $first_aid, title => 'PK_Yet More Tweeny-Pop crap', year => 2007 },
222 name => 'PK_Manufactured Crap',
226 name => 'PK_Like I Give a Damn',
228 { title => 'PK_My parents sold me to a record company' ,year => 2005 },
229 { title => 'PK_Why Am I So Ugly?', year => 2006 },
230 { title => 'PK_I Got Surgery and am now Popular', year => 2007 }
235 name => 'PK_Formerly Named',
237 { title => 'PK_One Hit Wonder', year => 2006 },
242 ## Get the result row objects.
244 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
246 ## Do we have the right object?
248 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
249 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
250 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
251 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
253 ## Find the expected information?
255 ok( $crap->name eq 'PK_Manufactured Crap', "Got Correct name for result object");
256 ok( $girl->name eq 'PK_Angsty-Whiny Girl', "Got Correct name for result object");
257 ok( $girl->artistid == $first_aid, "Got Correct artist PK for result object");
258 ok( $damn->name eq 'PK_Like I Give a Damn', "Got Correct name for result object");
259 ok( $formerly->name eq 'PK_Formerly Named', "Got Correct name for result object");
261 ## Create the expected children sub objects?
263 ok( $crap->cds->count == 0, "got Expected Number of Cds");
264 ok( $girl->cds->count == 2, "got Expected Number of Cds");
265 ok( $damn->cds->count == 3, "got Expected Number of Cds");
266 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
268 ## Did the cds get expected information?
270 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
272 ok( $cd1->title eq "PK_My First CD", "Got Expected CD Title");
273 ok( $cd2->title eq "PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
278 ## Test from a belongs_to perspective, should create artist first,
279 ## then CD with artistid. This test we let the system automatically
280 ## create the PK's. Chances are good you'll use it this way mostly.
286 artist => { name => 'Fred BloggsC'},
291 artist => { name => 'Fred BloggsD'},
295 my ($cdA, $cdB) = $cd_rs->populate($cds);
298 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
299 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
300 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
303 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
304 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
305 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
308 BELONGS_TO_WITH_PKs: {
310 ## Test from a belongs_to perspective, should create artist first,
311 ## then CD with artistid. This time we try setting the PK's
313 my $aid = $art_rs->get_column('artistid')->max || 0;
319 artist => { artistid=> ++$aid, name => 'Fred BloggsE'},
324 artist => { artistid=> ++$aid, name => 'Fred BloggsF'},
328 my ($cdA, $cdB) = $cd_rs->populate($cds);
330 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
331 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
332 is($cdA->artist->name, 'Fred BloggsE', 'Set Artist to FredE');
334 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
335 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
336 is($cdB->artist->name, 'Fred BloggsF', 'Set Artist to FredF');
337 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
342 my ($more_crap) = $restricted_art_rs->populate([
344 name => 'More Manufactured Crap',
348 ## Did it use the condition in the resultset?
349 $more_crap->discard_changes;
350 cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object");
351 cmp_ok( $more_crap->charfield, '==', $more_crap->id + 5, "Got Correct charfield for result object");
356 ## ----------------------------------------------------------------------------
357 ## Void context tests
358 ## ----------------------------------------------------------------------------
362 ## All these tests check the ability to use populate without asking for
363 ## any returned resultsets. This uses bulk_insert as much as possible
364 ## in order to increase speed.
368 ## This first group of tests checks to make sure we can call populate
369 ## with the parent having many children and the parent PK is set
371 my $aid = $art_rs->get_column('artistid')->max || 0;
373 my $first_aid = ++$aid;
377 artistid => $first_aid,
378 name => 'VOID_PK_Angsty-Whiny Girl',
380 { artist => $first_aid, title => 'VOID_PK_My First CD', year => 2006 },
381 { artist => $first_aid, title => 'VOID_PK_Yet More Tweeny-Pop crap', year => 2007 },
386 name => 'VOID_PK_Manufactured Crap',
390 name => 'VOID_PK_Like I Give a Damn',
392 { title => 'VOID_PK_My parents sold me to a record company' ,year => 2005 },
393 { title => 'VOID_PK_Why Am I So Ugly?', year => 2006 },
394 { title => 'VOID_PK_I Got Surgery and am now Popular', year => 2007 }
399 name => 'VOID_PK_Formerly Named',
401 { title => 'VOID_PK_One Hit Wonder', year => 2006 },
408 { title => 'VOID_PK_Zundef test', year => 2006 },
413 ## Get the result row objects.
415 $art_rs->populate($artists);
417 my ($undef, $girl, $formerly, $damn, $crap) = $art_rs->search(
419 {name=>[ map { $_->{name} } @$artists]},
420 {order_by=>'name ASC'},
423 ## Do we have the right object?
425 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
426 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
427 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
428 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
429 isa_ok( $undef, 'DBICTest::Artist', "Got 'Artist'");
431 ## Find the expected information?
433 ok( $crap->name eq 'VOID_PK_Manufactured Crap', "Got Correct name 'VOID_PK_Manufactured Crap' for result object");
434 ok( $girl->name eq 'VOID_PK_Angsty-Whiny Girl', "Got Correct name for result object");
435 ok( $damn->name eq 'VOID_PK_Like I Give a Damn', "Got Correct name for result object");
436 ok( $formerly->name eq 'VOID_PK_Formerly Named', "Got Correct name for result object");
437 ok( !defined $undef->name, "Got Correct name 'is undef' for result object");
439 ## Create the expected children sub objects?
440 ok( $crap->can('cds'), "Has cds relationship");
441 ok( $girl->can('cds'), "Has cds relationship");
442 ok( $damn->can('cds'), "Has cds relationship");
443 ok( $formerly->can('cds'), "Has cds relationship");
444 ok( $undef->can('cds'), "Has cds relationship");
446 ok( $crap->cds->count == 0, "got Expected Number of Cds");
447 ok( $girl->cds->count == 2, "got Expected Number of Cds");
448 ok( $damn->cds->count == 3, "got Expected Number of Cds");
449 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
450 ok( $undef->cds->count == 1, "got Expected Number of Cds");
452 ## Did the cds get expected information?
454 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
456 ok( $cd1->title eq "VOID_PK_My First CD", "Got Expected CD Title");
457 ok( $cd2->title eq "VOID_PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
461 BELONGS_TO_WITH_PKs: {
463 ## Test from a belongs_to perspective, should create artist first,
464 ## then CD with artistid. This time we try setting the PK's
466 my $aid = $art_rs->get_column('artistid')->max || 0;
470 title => 'Some CD3B',
472 artist => { artistid=> ++$aid, name => 'Fred BloggsCB'},
475 title => 'Some CD4B',
477 artist => { artistid=> ++$aid, name => 'Fred BloggsDB'},
482 $cd_rs->populate($cds)
483 } qr/\QFast-path populate() of belongs_to relationship data is not possible/;
485 my ($cdA, $cdB) = $cd_rs->search(
486 {title=>[sort map {$_->{title}} @$cds]},
487 {order_by=>'title ASC'},
490 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
491 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
492 is($cdA->artist->name, 'Fred BloggsCB', 'Set Artist to FredCB');
494 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
495 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
496 is($cdB->artist->name, 'Fred BloggsDB', 'Set Artist to FredDB');
497 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
502 ## Test from a belongs_to perspective, should create artist first,
503 ## then CD with artistid.
507 title => 'Some CD3BB',
509 artist => { name => 'Fred BloggsCBB'},
512 title => 'Some CD4BB',
514 artist => { name => 'Fred BloggsDBB'},
517 title => 'Some CD5BB',
519 artist => { name => undef},
524 $cd_rs->populate($cds);
525 } qr/\QFast-path populate() of belongs_to relationship data is not possible/;
527 my ($cdA, $cdB, $cdC) = $cd_rs->search(
528 {title=>[sort map {$_->{title}} @$cds]},
529 {order_by=>'title ASC'},
532 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
533 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
534 is($cdA->title, 'Some CD3BB', 'Found Expected title');
535 is($cdA->artist->name, 'Fred BloggsCBB', 'Set Artist to FredCBB');
537 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
538 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
539 is($cdB->title, 'Some CD4BB', 'Found Expected title');
540 is($cdB->artist->name, 'Fred BloggsDBB', 'Set Artist to FredDBB');
542 isa_ok($cdC, 'DBICTest::CD', 'Created CD');
543 isa_ok($cdC->artist, 'DBICTest::Artist', 'Set Artist');
544 is($cdC->title, 'Some CD5BB', 'Found Expected title');
545 is( $cdC->artist->name, undef, 'Set Artist to something undefined');
551 ## This first group of tests checks to make sure we can call populate
552 ## with the parent having many children and let the keys be automatic
556 name => 'VOID_Angsty-Whiny Girl',
558 { title => 'VOID_My First CD', year => 2006 },
559 { title => 'VOID_Yet More Tweeny-Pop crap', year => 2007 },
563 name => 'VOID_Manufactured Crap',
566 name => 'VOID_Like I Give a Damn',
568 { title => 'VOID_My parents sold me to a record company' ,year => 2005 },
569 { title => 'VOID_Why Am I So Ugly?', year => 2006 },
570 { title => 'VOID_I Got Surgery and am now Popular', year => 2007 }
574 name => 'VOID_Formerly Named',
576 { title => 'VOID_One Hit Wonder', year => 2006 },
581 ## Get the result row objects.
583 $art_rs->populate($artists);
585 my ($girl, $formerly, $damn, $crap) = $art_rs->search(
586 {name=>[sort map {$_->{name}} @$artists]},
587 {order_by=>'name ASC'},
590 ## Do we have the right object?
592 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
593 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
594 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
595 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
597 ## Find the expected information?
599 ok( $crap->name eq 'VOID_Manufactured Crap', "Got Correct name for result object");
600 ok( $girl->name eq 'VOID_Angsty-Whiny Girl', "Got Correct name for result object");
601 ok( $damn->name eq 'VOID_Like I Give a Damn', "Got Correct name for result object");
602 ok( $formerly->name eq 'VOID_Formerly Named', "Got Correct name for result object");
604 ## Create the expected children sub objects?
605 ok( $crap->can('cds'), "Has cds relationship");
606 ok( $girl->can('cds'), "Has cds relationship");
607 ok( $damn->can('cds'), "Has cds relationship");
608 ok( $formerly->can('cds'), "Has cds relationship");
610 ok( $crap->cds->count == 0, "got Expected Number of Cds");
611 ok( $girl->cds->count == 2, "got Expected Number of Cds");
612 ok( $damn->cds->count == 3, "got Expected Number of Cds");
613 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
615 ## Did the cds get expected information?
617 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
619 ok($cd1, "Got a got CD");
620 ok($cd2, "Got a got CD");
621 ok( $cd1->title eq "VOID_My First CD", "Got Expected CD Title");
622 ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
627 $restricted_art_rs->populate([
629 name => 'VOID More Manufactured Crap',
633 my $more_crap = $art_rs->search({
634 name => 'VOID More Manufactured Crap'
637 ## Did it use the condition in the resultset?
638 $more_crap->discard_changes;
639 cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object");
640 cmp_ok( $more_crap->charfield, '==', $more_crap->id + 5, "Got Correct charfield for result object");
644 ARRAYREF_OF_ARRAYREF_STYLE: {
647 [1000, 'A Formally Unknown Singer'],
648 [1001, 'A singer that jumped the shark two albums ago'],
649 [1002, 'An actually cool singer.'],
652 ok my $unknown = $art_rs->find(1000), "got Unknown";
653 ok my $jumped = $art_rs->find(1001), "got Jumped";
654 ok my $cool = $art_rs->find(1002), "got Cool";
656 is $unknown->name, 'A Formally Unknown Singer', 'Correct Name';
657 is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name';
658 is $cool->name, 'An actually cool singer.', 'Correct Name';
660 my ($cooler, $lamer) = $restricted_art_rs->populate([
666 is $cooler->name, 'Cooler', 'Correct Name';
667 is $lamer->name, 'Lamer', 'Correct Name';
669 for ($cooler, $lamer) {
671 cmp_ok( $_->rank, '==', 42, "Got Correct rank for result object");
672 cmp_ok( $_->charfield, '==', $_->id + 5, "Got Correct charfield for result object");
675 ARRAY_CONTEXT_WITH_COND_FROM_RS: {
677 my ($mega_lamer) = $restricted_art_rs->populate([
679 name => 'Mega Lamer',
683 ## Did it use the condition in the resultset?
684 $mega_lamer->discard_changes;
685 cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object");
686 cmp_ok( $mega_lamer->charfield, '==', $mega_lamer->id + 5, "Got Correct charfield for result object");
689 VOID_CONTEXT_WITH_COND_FROM_RS: {
691 $restricted_art_rs->populate([
693 name => 'VOID Mega Lamer',
697 my $mega_lamer = $art_rs->search({
698 name => 'VOID Mega Lamer'
701 ## Did it use the condition in the resultset?
702 cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object");
703 cmp_ok( $mega_lamer->charfield, '==', $mega_lamer->id + 5, "Got Correct charfield for result object");
707 ok(eval { $art_rs->populate([]); 1 }, "Empty populate runs but does nothing");