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 ## ----------------------------------------------------------------------------
21 ## ----------------------------------------------------------------------------
22 ## Get a Schema and some ResultSets we can play with.
23 ## ----------------------------------------------------------------------------
25 my $schema = DBICTest->init_schema();
26 my $art_rs = $schema->resultset('Artist');
27 my $cd_rs = $schema->resultset('CD');
29 my $restricted_art_rs = $art_rs->search({ -and => [ rank => 42, charfield => { '=', \['(SELECT MAX(artistid) FROM artist) + ?', 6] } ] });
31 ok( $schema, 'Got a Schema object');
32 ok( $art_rs, 'Got Good Artist Resultset');
33 ok( $cd_rs, 'Got Good CD Resultset');
36 ## ----------------------------------------------------------------------------
37 ## Schema populate Tests
38 ## ----------------------------------------------------------------------------
42 # throw a monkey wrench
43 my $post_jnap_monkeywrench = $schema->resultset('Artist')->find(1)->update({ name => undef });
45 warnings_exist { $schema->populate('Artist', [
49 {title=>"001Title1", year=>2000},
50 {title=>"001Title2", year=>2001},
51 {title=>"001Title3", year=>2002},
53 ["002Second Artist", []],
55 {title=>"003Title1", year=>2005},
58 {title=>"004Title1", year=>2010}
60 ]) } qr/\QFast-path populate() of non-uniquely identifiable rows with related data is not possible/;
62 isa_ok $schema, 'DBIx::Class::Schema';
64 my ( $preexisting_undef, $artist1, $artist2, $artist3, $undef ) = $schema->resultset('Artist')->search({
65 name=>["001First Artist","002Second Artist","003Third Artist", undef]},
66 {order_by => { -asc => 'artistid' }})->all;
68 isa_ok $artist1, 'DBICTest::Artist';
69 isa_ok $artist2, 'DBICTest::Artist';
70 isa_ok $artist3, 'DBICTest::Artist';
71 isa_ok $undef, 'DBICTest::Artist';
73 ok $artist1->name eq '001First Artist', "Got Expected Artist Name for Artist001";
74 ok $artist2->name eq '002Second Artist', "Got Expected Artist Name for Artist002";
75 ok $artist3->name eq '003Third Artist', "Got Expected Artist Name for Artist003";
76 ok !defined $undef->name, "Got Expected Artist Name for Artist004";
78 ok $artist1->cds->count eq 3, "Got Right number of CDs for Artist1";
79 ok $artist2->cds->count eq 0, "Got Right number of CDs for Artist2";
80 ok $artist3->cds->count eq 1, "Got Right number of CDs for Artist3";
81 ok $undef->cds->count eq 1, "Got Right number of CDs for Artist4";
83 $post_jnap_monkeywrench->delete;
87 my ($cd1, $cd2, $cd3) = $artist1->cds->search(undef, {order_by=>'year ASC'});
89 isa_ok $cd1, 'DBICTest::CD';
90 isa_ok $cd2, 'DBICTest::CD';
91 isa_ok $cd3, 'DBICTest::CD';
93 ok $cd1->year == 2000;
94 ok $cd2->year == 2001;
95 ok $cd3->year == 2002;
97 ok $cd1->title eq '001Title1';
98 ok $cd2->title eq '001Title2';
99 ok $cd3->title eq '001Title3';
104 my ($cd1) = $artist3->cds->search(undef, {order_by=>'year ASC'});
106 isa_ok $cd1, 'DBICTest::CD';
108 ok $cd1->year == 2005;
109 ok $cd1->title eq '003Title1';
114 my ($cd1) = $undef->cds->search(undef, {order_by=>'year ASC'});
116 isa_ok $cd1, 'DBICTest::CD';
118 ok $cd1->year == 2010;
119 ok $cd1->title eq '004Title1';
122 ## Need to do some cleanup so that later tests don't get borked
128 ## ----------------------------------------------------------------------------
129 ## Array context tests
130 ## ----------------------------------------------------------------------------
134 ## These first set of tests are cake because array context just delegates
135 ## all its processing to $resultset->create
139 ## This first group of tests checks to make sure we can call populate
140 ## with the parent having many children and let the keys be automatic
144 name => 'Angsty-Whiny Girl',
146 { title => 'My First CD', year => 2006 },
147 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
151 name => 'Manufactured Crap',
154 name => 'Like I Give a Damn',
156 { title => 'My parents sold me to a record company' ,year => 2005 },
157 { title => 'Why Am I So Ugly?', year => 2006 },
158 { title => 'I Got Surgery and am now Popular', year => 2007 }
162 name => 'Formerly Named',
164 { title => 'One Hit Wonder', year => 2006 },
169 ## Get the result row objects.
171 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
173 ## Do we have the right object?
175 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
176 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
177 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
178 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
180 ## Find the expected information?
182 ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
183 ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
184 ok( $damn->name eq 'Like I Give a Damn', "Got Correct name for result object");
185 ok( $formerly->name eq 'Formerly Named', "Got Correct name for result object");
187 ## Create the expected children sub objects?
189 ok( $crap->cds->count == 0, "got Expected Number of Cds");
190 ok( $girl->cds->count == 2, "got Expected Number of Cds");
191 ok( $damn->cds->count == 3, "got Expected Number of Cds");
192 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
194 ## Did the cds get expected information?
196 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year'});
198 ok( $cd1->title eq "My First CD", "Got Expected CD Title");
199 ok( $cd2->title eq "Yet More Tweeny-Pop crap", "Got Expected CD Title");
204 ## This group tests the ability to specify the PK in the parent and let
205 ## DBIC transparently pass the PK down to the Child and also let's the
206 ## child create any other needed PK's for itself.
208 my $aid = $art_rs->get_column('artistid')->max || 0;
210 my $first_aid = ++$aid;
214 artistid => $first_aid,
215 name => 'PK_Angsty-Whiny Girl',
217 { artist => $first_aid, title => 'PK_My First CD', year => 2006 },
218 { artist => $first_aid, title => 'PK_Yet More Tweeny-Pop crap', year => 2007 },
223 name => 'PK_Manufactured Crap',
227 name => 'PK_Like I Give a Damn',
229 { title => 'PK_My parents sold me to a record company' ,year => 2005 },
230 { title => 'PK_Why Am I So Ugly?', year => 2006 },
231 { title => 'PK_I Got Surgery and am now Popular', year => 2007 }
236 name => 'PK_Formerly Named',
238 { title => 'PK_One Hit Wonder', year => 2006 },
243 ## Get the result row objects.
245 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
247 ## Do we have the right object?
249 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
250 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
251 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
252 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
254 ## Find the expected information?
256 ok( $crap->name eq 'PK_Manufactured Crap', "Got Correct name for result object");
257 ok( $girl->name eq 'PK_Angsty-Whiny Girl', "Got Correct name for result object");
258 ok( $girl->artistid == $first_aid, "Got Correct artist PK for result object");
259 ok( $damn->name eq 'PK_Like I Give a Damn', "Got Correct name for result object");
260 ok( $formerly->name eq 'PK_Formerly Named', "Got Correct name for result object");
262 ## Create the expected children sub objects?
264 ok( $crap->cds->count == 0, "got Expected Number of Cds");
265 ok( $girl->cds->count == 2, "got Expected Number of Cds");
266 ok( $damn->cds->count == 3, "got Expected Number of Cds");
267 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
269 ## Did the cds get expected information?
271 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
273 ok( $cd1->title eq "PK_My First CD", "Got Expected CD Title");
274 ok( $cd2->title eq "PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
279 ## Test from a belongs_to perspective, should create artist first,
280 ## then CD with artistid. This test we let the system automatically
281 ## create the PK's. Chances are good you'll use it this way mostly.
287 artist => { name => 'Fred BloggsC'},
292 artist => { name => 'Fred BloggsD'},
296 my ($cdA, $cdB) = $cd_rs->populate($cds);
299 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
300 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
301 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
304 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
305 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
306 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
309 BELONGS_TO_WITH_PKs: {
311 ## Test from a belongs_to perspective, should create artist first,
312 ## then CD with artistid. This time we try setting the PK's
314 my $aid = $art_rs->get_column('artistid')->max || 0;
320 artist => { artistid=> ++$aid, name => 'Fred BloggsE'},
325 artist => { artistid=> ++$aid, name => 'Fred BloggsF'},
329 my ($cdA, $cdB) = $cd_rs->populate($cds);
331 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
332 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
333 is($cdA->artist->name, 'Fred BloggsE', 'Set Artist to FredE');
335 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
336 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
337 is($cdB->artist->name, 'Fred BloggsF', 'Set Artist to FredF');
338 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
343 my ($more_crap) = $restricted_art_rs->populate([
345 name => 'More Manufactured Crap',
349 ## Did it use the condition in the resultset?
350 $more_crap->discard_changes;
351 cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object");
352 cmp_ok( $more_crap->charfield, '==', $more_crap->id + 5, "Got Correct charfield for result object");
357 ## ----------------------------------------------------------------------------
358 ## Void context tests
359 ## ----------------------------------------------------------------------------
363 ## All these tests check the ability to use populate without asking for
364 ## any returned resultsets. This uses bulk_insert as much as possible
365 ## in order to increase speed.
369 ## This first group of tests checks to make sure we can call populate
370 ## with the parent having many children and the parent PK is set
372 my $aid = $art_rs->get_column('artistid')->max || 0;
374 my $first_aid = ++$aid;
378 artistid => $first_aid,
379 name => 'VOID_PK_Angsty-Whiny Girl',
381 { artist => $first_aid, title => 'VOID_PK_My First CD', year => 2006 },
382 { artist => $first_aid, title => 'VOID_PK_Yet More Tweeny-Pop crap', year => 2007 },
387 name => 'VOID_PK_Manufactured Crap',
391 name => 'VOID_PK_Like I Give a Damn',
393 { title => 'VOID_PK_My parents sold me to a record company' ,year => 2005 },
394 { title => 'VOID_PK_Why Am I So Ugly?', year => 2006 },
395 { title => 'VOID_PK_I Got Surgery and am now Popular', year => 2007 }
400 name => 'VOID_PK_Formerly Named',
402 { title => 'VOID_PK_One Hit Wonder', year => 2006 },
409 { title => 'VOID_PK_Zundef test', year => 2006 },
414 ## Get the result row objects.
416 $art_rs->populate($artists);
418 my ($undef, $girl, $formerly, $damn, $crap) = $art_rs->search(
420 {name=>[ map { $_->{name} } @$artists]},
421 {order_by=>'name ASC'},
424 ## Do we have the right object?
426 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
427 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
428 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
429 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
430 isa_ok( $undef, 'DBICTest::Artist', "Got 'Artist'");
432 ## Find the expected information?
434 ok( $crap->name eq 'VOID_PK_Manufactured Crap', "Got Correct name 'VOID_PK_Manufactured Crap' for result object");
435 ok( $girl->name eq 'VOID_PK_Angsty-Whiny Girl', "Got Correct name for result object");
436 ok( $damn->name eq 'VOID_PK_Like I Give a Damn', "Got Correct name for result object");
437 ok( $formerly->name eq 'VOID_PK_Formerly Named', "Got Correct name for result object");
438 ok( !defined $undef->name, "Got Correct name 'is undef' for result object");
440 ## Create the expected children sub objects?
441 ok( $crap->can('cds'), "Has cds relationship");
442 ok( $girl->can('cds'), "Has cds relationship");
443 ok( $damn->can('cds'), "Has cds relationship");
444 ok( $formerly->can('cds'), "Has cds relationship");
445 ok( $undef->can('cds'), "Has cds relationship");
447 ok( $crap->cds->count == 0, "got Expected Number of Cds");
448 ok( $girl->cds->count == 2, "got Expected Number of Cds");
449 ok( $damn->cds->count == 3, "got Expected Number of Cds");
450 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
451 ok( $undef->cds->count == 1, "got Expected Number of Cds");
453 ## Did the cds get expected information?
455 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
457 ok( $cd1->title eq "VOID_PK_My First CD", "Got Expected CD Title");
458 ok( $cd2->title eq "VOID_PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
462 BELONGS_TO_WITH_PKs: {
464 ## Test from a belongs_to perspective, should create artist first,
465 ## then CD with artistid. This time we try setting the PK's
467 my $aid = $art_rs->get_column('artistid')->max || 0;
471 title => 'Some CD3B',
473 artist => { artistid=> ++$aid, name => 'Fred BloggsCB'},
476 title => 'Some CD4B',
478 artist => { artistid=> ++$aid, name => 'Fred BloggsDB'},
483 $cd_rs->populate($cds)
484 } qr/\QFast-path populate() of belongs_to relationship data is not possible/;
486 my ($cdA, $cdB) = $cd_rs->search(
487 {title=>[sort map {$_->{title}} @$cds]},
488 {order_by=>'title ASC'},
491 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
492 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
493 is($cdA->artist->name, 'Fred BloggsCB', 'Set Artist to FredCB');
495 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
496 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
497 is($cdB->artist->name, 'Fred BloggsDB', 'Set Artist to FredDB');
498 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
503 ## Test from a belongs_to perspective, should create artist first,
504 ## then CD with artistid.
508 title => 'Some CD3BB',
510 artist => { name => 'Fred BloggsCBB'},
513 title => 'Some CD4BB',
515 artist => { name => 'Fred BloggsDBB'},
518 title => 'Some CD5BB',
520 artist => { name => undef},
525 $cd_rs->populate($cds);
526 } qr/\QFast-path populate() of belongs_to relationship data is not possible/;
528 my ($cdA, $cdB, $cdC) = $cd_rs->search(
529 {title=>[sort map {$_->{title}} @$cds]},
530 {order_by=>'title ASC'},
533 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
534 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
535 is($cdA->title, 'Some CD3BB', 'Found Expected title');
536 is($cdA->artist->name, 'Fred BloggsCBB', 'Set Artist to FredCBB');
538 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
539 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
540 is($cdB->title, 'Some CD4BB', 'Found Expected title');
541 is($cdB->artist->name, 'Fred BloggsDBB', 'Set Artist to FredDBB');
543 isa_ok($cdC, 'DBICTest::CD', 'Created CD');
544 isa_ok($cdC->artist, 'DBICTest::Artist', 'Set Artist');
545 is($cdC->title, 'Some CD5BB', 'Found Expected title');
546 is( $cdC->artist->name, undef, 'Set Artist to something undefined');
552 ## This first group of tests checks to make sure we can call populate
553 ## with the parent having many children and let the keys be automatic
557 name => 'VOID_Angsty-Whiny Girl',
559 { title => 'VOID_My First CD', year => 2006 },
560 { title => 'VOID_Yet More Tweeny-Pop crap', year => 2007 },
564 name => 'VOID_Manufactured Crap',
567 name => 'VOID_Like I Give a Damn',
569 { title => 'VOID_My parents sold me to a record company' ,year => 2005 },
570 { title => 'VOID_Why Am I So Ugly?', year => 2006 },
571 { title => 'VOID_I Got Surgery and am now Popular', year => 2007 }
575 name => 'VOID_Formerly Named',
577 { title => 'VOID_One Hit Wonder', year => 2006 },
582 ## Get the result row objects.
584 $art_rs->populate($artists);
586 my ($girl, $formerly, $damn, $crap) = $art_rs->search(
587 {name=>[sort map {$_->{name}} @$artists]},
588 {order_by=>'name ASC'},
591 ## Do we have the right object?
593 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
594 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
595 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
596 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
598 ## Find the expected information?
600 ok( $crap->name eq 'VOID_Manufactured Crap', "Got Correct name for result object");
601 ok( $girl->name eq 'VOID_Angsty-Whiny Girl', "Got Correct name for result object");
602 ok( $damn->name eq 'VOID_Like I Give a Damn', "Got Correct name for result object");
603 ok( $formerly->name eq 'VOID_Formerly Named', "Got Correct name for result object");
605 ## Create the expected children sub objects?
606 ok( $crap->can('cds'), "Has cds relationship");
607 ok( $girl->can('cds'), "Has cds relationship");
608 ok( $damn->can('cds'), "Has cds relationship");
609 ok( $formerly->can('cds'), "Has cds relationship");
611 ok( $crap->cds->count == 0, "got Expected Number of Cds");
612 ok( $girl->cds->count == 2, "got Expected Number of Cds");
613 ok( $damn->cds->count == 3, "got Expected Number of Cds");
614 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
616 ## Did the cds get expected information?
618 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
620 ok($cd1, "Got a got CD");
621 ok($cd2, "Got a got CD");
622 ok( $cd1->title eq "VOID_My First CD", "Got Expected CD Title");
623 ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
628 $restricted_art_rs->populate([
630 name => 'VOID More Manufactured Crap',
634 my $more_crap = $art_rs->search({
635 name => 'VOID More Manufactured Crap'
638 ## Did it use the condition in the resultset?
639 $more_crap->discard_changes;
640 cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object");
641 cmp_ok( $more_crap->charfield, '==', $more_crap->id + 5, "Got Correct charfield for result object");
645 ARRAYREF_OF_ARRAYREF_STYLE: {
648 [1000, 'A Formally Unknown Singer'],
649 [1001, 'A singer that jumped the shark two albums ago'],
650 [1002, 'An actually cool singer.'],
653 ok my $unknown = $art_rs->find(1000), "got Unknown";
654 ok my $jumped = $art_rs->find(1001), "got Jumped";
655 ok my $cool = $art_rs->find(1002), "got Cool";
657 is $unknown->name, 'A Formally Unknown Singer', 'Correct Name';
658 is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name';
659 is $cool->name, 'An actually cool singer.', 'Correct Name';
661 my ($cooler, $lamer) = $restricted_art_rs->populate([
667 is $cooler->name, 'Cooler', 'Correct Name';
668 is $lamer->name, 'Lamer', 'Correct Name';
670 for ($cooler, $lamer) {
672 cmp_ok( $_->rank, '==', 42, "Got Correct rank for result object");
673 cmp_ok( $_->charfield, '==', $_->id + 5, "Got Correct charfield for result object");
676 ARRAY_CONTEXT_WITH_COND_FROM_RS: {
678 my ($mega_lamer) = $restricted_art_rs->populate([
680 name => 'Mega Lamer',
684 ## Did it use the condition in the resultset?
685 $mega_lamer->discard_changes;
686 cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object");
687 cmp_ok( $mega_lamer->charfield, '==', $mega_lamer->id + 5, "Got Correct charfield for result object");
690 VOID_CONTEXT_WITH_COND_FROM_RS: {
692 $restricted_art_rs->populate([
694 name => 'VOID Mega Lamer',
698 my $mega_lamer = $art_rs->search({
699 name => 'VOID Mega Lamer'
702 ## Did it use the condition in the resultset?
703 cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object");
704 cmp_ok( $mega_lamer->charfield, '==', $mega_lamer->id + 5, "Got Correct charfield for result object");
711 [ columns_only => [ [qw(name rank charfield)] ] ],
713 my ($desc, $arg) = @{$_};
715 $schema->is_executed_sql_bind( sub {
718 lives_ok { $rs->populate($arg); 1 } "$desc populate in void context lives";
720 my @r = $art_rs->populate($arg);
721 is_deeply( \@r, [], "$desc populate in list context returns empty list" );
723 my $r = $art_rs->populate($arg);
724 is( $r, undef, "$desc populate in scalar context returns undef" );
726 }, [], "$desc populate executed no statements" );