1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
3 ## ----------------------------------------------------------------------------
4 ## Tests for the $resultset->populate method.
6 ## GOALS: We need to test the method for both void and array context for all
7 ## the following relationship types: belongs_to, has_many. Additionally we
8 ## need to test each of those for both specified PK's and autogenerated PK's
10 ## Also need to test some stuff that should generate errors.
11 ## ----------------------------------------------------------------------------
23 ## ----------------------------------------------------------------------------
24 ## Get a Schema and some ResultSets we can play with.
25 ## ----------------------------------------------------------------------------
27 my $schema = DBICTest->init_schema();
28 my $art_rs = $schema->resultset('Artist');
29 my $cd_rs = $schema->resultset('CD');
31 my $restricted_art_rs = $art_rs->search({ -and => [ rank => 42, charfield => { '=', \['(SELECT MAX(artistid) FROM artist) + ?', 6] } ] });
33 ok( $schema, 'Got a Schema object');
34 ok( $art_rs, 'Got Good Artist Resultset');
35 ok( $cd_rs, 'Got Good CD Resultset');
38 ## ----------------------------------------------------------------------------
39 ## Schema populate Tests
40 ## ----------------------------------------------------------------------------
44 # throw a monkey wrench
45 my $post_jnap_monkeywrench = $schema->resultset('Artist')->find(1)->update({ name => undef });
47 warnings_exist { $schema->populate('Artist', [
51 {title=>"001Title1", year=>2000},
52 {title=>"001Title2", year=>2001},
53 {title=>"001Title3", year=>2002},
55 ["002Second Artist", []],
57 {title=>"003Title1", year=>2005},
60 {title=>"004Title1", year=>2010}
62 ]) } qr/\QFast-path populate() of non-uniquely identifiable rows with related data is not possible/;
64 isa_ok $schema, 'DBIx::Class::Schema';
66 my ( $preexisting_undef, $artist1, $artist2, $artist3, $undef ) = $schema->resultset('Artist')->search({
67 name=>["001First Artist","002Second Artist","003Third Artist", undef]},
68 {order_by => { -asc => 'artistid' }})->all;
70 isa_ok $artist1, 'DBICTest::Artist';
71 isa_ok $artist2, 'DBICTest::Artist';
72 isa_ok $artist3, 'DBICTest::Artist';
73 isa_ok $undef, 'DBICTest::Artist';
75 ok $artist1->name eq '001First Artist', "Got Expected Artist Name for Artist001";
76 ok $artist2->name eq '002Second Artist', "Got Expected Artist Name for Artist002";
77 ok $artist3->name eq '003Third Artist', "Got Expected Artist Name for Artist003";
78 ok !defined $undef->name, "Got Expected Artist Name for Artist004";
80 ok $artist1->cds->count eq 3, "Got Right number of CDs for Artist1";
81 ok $artist2->cds->count eq 0, "Got Right number of CDs for Artist2";
82 ok $artist3->cds->count eq 1, "Got Right number of CDs for Artist3";
83 ok $undef->cds->count eq 1, "Got Right number of CDs for Artist4";
85 $post_jnap_monkeywrench->delete;
89 my ($cd1, $cd2, $cd3) = $artist1->cds->search(undef, {order_by=>'year ASC'});
91 isa_ok $cd1, 'DBICTest::CD';
92 isa_ok $cd2, 'DBICTest::CD';
93 isa_ok $cd3, 'DBICTest::CD';
95 ok $cd1->year == 2000;
96 ok $cd2->year == 2001;
97 ok $cd3->year == 2002;
99 ok $cd1->title eq '001Title1';
100 ok $cd2->title eq '001Title2';
101 ok $cd3->title eq '001Title3';
106 my ($cd1) = $artist3->cds->search(undef, {order_by=>'year ASC'});
108 isa_ok $cd1, 'DBICTest::CD';
110 ok $cd1->year == 2005;
111 ok $cd1->title eq '003Title1';
116 my ($cd1) = $undef->cds->search(undef, {order_by=>'year ASC'});
118 isa_ok $cd1, 'DBICTest::CD';
120 ok $cd1->year == 2010;
121 ok $cd1->title eq '004Title1';
124 ## Need to do some cleanup so that later tests don't get borked
130 ## ----------------------------------------------------------------------------
131 ## Array context tests
132 ## ----------------------------------------------------------------------------
136 ## These first set of tests are cake because array context just delegates
137 ## all its processing to $resultset->create
141 ## This first group of tests checks to make sure we can call populate
142 ## with the parent having many children and let the keys be automatic
146 name => 'Angsty-Whiny Girl',
148 { title => 'My First CD', year => 2006 },
149 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
153 name => 'Manufactured Crap',
156 name => 'Like I Give a Damn',
158 { title => 'My parents sold me to a record company' ,year => 2005 },
159 { title => 'Why Am I So Ugly?', year => 2006 },
160 { title => 'I Got Surgery and am now Popular', year => 2007 }
164 name => 'Formerly Named',
166 { title => 'One Hit Wonder', year => 2006 },
171 ## Get the result row objects.
173 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
175 ## Do we have the right object?
177 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
178 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
179 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
180 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
182 ## Find the expected information?
184 ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
185 ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
186 ok( $damn->name eq 'Like I Give a Damn', "Got Correct name for result object");
187 ok( $formerly->name eq 'Formerly Named', "Got Correct name for result object");
189 ## Create the expected children sub objects?
191 ok( $crap->cds->count == 0, "got Expected Number of Cds");
192 ok( $girl->cds->count == 2, "got Expected Number of Cds");
193 ok( $damn->cds->count == 3, "got Expected Number of Cds");
194 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
196 ## Did the cds get expected information?
198 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year'});
200 ok( $cd1->title eq "My First CD", "Got Expected CD Title");
201 ok( $cd2->title eq "Yet More Tweeny-Pop crap", "Got Expected CD Title");
206 ## This group tests the ability to specify the PK in the parent and let
207 ## DBIC transparently pass the PK down to the Child and also let's the
208 ## child create any other needed PK's for itself.
210 my $aid = $art_rs->get_column('artistid')->max || 0;
212 my $first_aid = ++$aid;
216 artistid => $first_aid,
217 name => 'PK_Angsty-Whiny Girl',
219 { artist => $first_aid, title => 'PK_My First CD', year => 2006 },
220 { artist => $first_aid, title => 'PK_Yet More Tweeny-Pop crap', year => 2007 },
225 name => 'PK_Manufactured Crap',
229 name => 'PK_Like I Give a Damn',
231 { title => 'PK_My parents sold me to a record company' ,year => 2005 },
232 { title => 'PK_Why Am I So Ugly?', year => 2006 },
233 { title => 'PK_I Got Surgery and am now Popular', year => 2007 }
238 name => 'PK_Formerly Named',
240 { title => 'PK_One Hit Wonder', year => 2006 },
245 ## Get the result row objects.
247 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
249 ## Do we have the right object?
251 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
252 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
253 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
254 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
256 ## Find the expected information?
258 ok( $crap->name eq 'PK_Manufactured Crap', "Got Correct name for result object");
259 ok( $girl->name eq 'PK_Angsty-Whiny Girl', "Got Correct name for result object");
260 ok( $girl->artistid == $first_aid, "Got Correct artist PK for result object");
261 ok( $damn->name eq 'PK_Like I Give a Damn', "Got Correct name for result object");
262 ok( $formerly->name eq 'PK_Formerly Named', "Got Correct name for result object");
264 ## Create the expected children sub objects?
266 ok( $crap->cds->count == 0, "got Expected Number of Cds");
267 ok( $girl->cds->count == 2, "got Expected Number of Cds");
268 ok( $damn->cds->count == 3, "got Expected Number of Cds");
269 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
271 ## Did the cds get expected information?
273 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
275 ok( $cd1->title eq "PK_My First CD", "Got Expected CD Title");
276 ok( $cd2->title eq "PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
281 ## Test from a belongs_to perspective, should create artist first,
282 ## then CD with artistid. This test we let the system automatically
283 ## create the PK's. Chances are good you'll use it this way mostly.
289 artist => { name => 'Fred BloggsC'},
294 artist => { name => 'Fred BloggsD'},
298 my ($cdA, $cdB) = $cd_rs->populate($cds);
301 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
302 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
303 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
306 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
307 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
308 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
311 BELONGS_TO_WITH_PKs: {
313 ## Test from a belongs_to perspective, should create artist first,
314 ## then CD with artistid. This time we try setting the PK's
316 my $aid = $art_rs->get_column('artistid')->max || 0;
322 artist => { artistid=> ++$aid, name => 'Fred BloggsE'},
327 artist => { artistid=> ++$aid, name => 'Fred BloggsF'},
331 my ($cdA, $cdB) = $cd_rs->populate($cds);
333 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
334 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
335 is($cdA->artist->name, 'Fred BloggsE', 'Set Artist to FredE');
337 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
338 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
339 is($cdB->artist->name, 'Fred BloggsF', 'Set Artist to FredF');
340 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
345 my ($more_crap) = $restricted_art_rs->populate([
347 name => 'More Manufactured Crap',
351 ## Did it use the condition in the resultset?
352 $more_crap->discard_changes;
353 cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object");
354 cmp_ok( $more_crap->charfield, '==', $more_crap->id + 5, "Got Correct charfield for result object");
359 ## ----------------------------------------------------------------------------
360 ## Void context tests
361 ## ----------------------------------------------------------------------------
365 ## All these tests check the ability to use populate without asking for
366 ## any returned resultsets. This uses bulk_insert as much as possible
367 ## in order to increase speed.
371 ## This first group of tests checks to make sure we can call populate
372 ## with the parent having many children and the parent PK is set
374 my $aid = $art_rs->get_column('artistid')->max || 0;
376 my $first_aid = ++$aid;
380 artistid => $first_aid,
381 name => 'VOID_PK_Angsty-Whiny Girl',
383 { artist => $first_aid, title => 'VOID_PK_My First CD', year => 2006 },
384 { artist => $first_aid, title => 'VOID_PK_Yet More Tweeny-Pop crap', year => 2007 },
389 name => 'VOID_PK_Manufactured Crap',
393 name => 'VOID_PK_Like I Give a Damn',
395 { title => 'VOID_PK_My parents sold me to a record company' ,year => 2005 },
396 { title => 'VOID_PK_Why Am I So Ugly?', year => 2006 },
397 { title => 'VOID_PK_I Got Surgery and am now Popular', year => 2007 }
402 name => 'VOID_PK_Formerly Named',
404 { title => 'VOID_PK_One Hit Wonder', year => 2006 },
411 { title => 'VOID_PK_Zundef test', year => 2006 },
416 ## Get the result row objects.
418 $art_rs->populate($artists);
420 my ($undef, $girl, $formerly, $damn, $crap) = $art_rs->search(
422 {name=>[ map { $_->{name} } @$artists]},
423 {order_by=>'name ASC'},
426 ## Do we have the right object?
428 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
429 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
430 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
431 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
432 isa_ok( $undef, 'DBICTest::Artist', "Got 'Artist'");
434 ## Find the expected information?
436 ok( $crap->name eq 'VOID_PK_Manufactured Crap', "Got Correct name 'VOID_PK_Manufactured Crap' for result object");
437 ok( $girl->name eq 'VOID_PK_Angsty-Whiny Girl', "Got Correct name for result object");
438 ok( $damn->name eq 'VOID_PK_Like I Give a Damn', "Got Correct name for result object");
439 ok( $formerly->name eq 'VOID_PK_Formerly Named', "Got Correct name for result object");
440 ok( !defined $undef->name, "Got Correct name 'is undef' for result object");
442 ## Create the expected children sub objects?
443 ok( $crap->can('cds'), "Has cds relationship");
444 ok( $girl->can('cds'), "Has cds relationship");
445 ok( $damn->can('cds'), "Has cds relationship");
446 ok( $formerly->can('cds'), "Has cds relationship");
447 ok( $undef->can('cds'), "Has cds relationship");
449 ok( $crap->cds->count == 0, "got Expected Number of Cds");
450 ok( $girl->cds->count == 2, "got Expected Number of Cds");
451 ok( $damn->cds->count == 3, "got Expected Number of Cds");
452 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
453 ok( $undef->cds->count == 1, "got Expected Number of Cds");
455 ## Did the cds get expected information?
457 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
459 ok( $cd1->title eq "VOID_PK_My First CD", "Got Expected CD Title");
460 ok( $cd2->title eq "VOID_PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
464 BELONGS_TO_WITH_PKs: {
466 ## Test from a belongs_to perspective, should create artist first,
467 ## then CD with artistid. This time we try setting the PK's
469 my $aid = $art_rs->get_column('artistid')->max || 0;
473 title => 'Some CD3B',
475 artist => { artistid=> ++$aid, name => 'Fred BloggsCB'},
478 title => 'Some CD4B',
480 artist => { artistid=> ++$aid, name => 'Fred BloggsDB'},
485 $cd_rs->populate($cds)
486 } qr/\QFast-path populate() of belongs_to relationship data is not possible/;
488 my ($cdA, $cdB) = $cd_rs->search(
489 {title=>[sort map {$_->{title}} @$cds]},
490 {order_by=>'title ASC'},
493 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
494 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
495 is($cdA->artist->name, 'Fred BloggsCB', 'Set Artist to FredCB');
497 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
498 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
499 is($cdB->artist->name, 'Fred BloggsDB', 'Set Artist to FredDB');
500 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
505 ## Test from a belongs_to perspective, should create artist first,
506 ## then CD with artistid.
510 title => 'Some CD3BB',
512 artist => { name => 'Fred BloggsCBB'},
515 title => 'Some CD4BB',
517 artist => { name => 'Fred BloggsDBB'},
520 title => 'Some CD5BB',
522 artist => { name => undef},
527 $cd_rs->populate($cds);
528 } qr/\QFast-path populate() of belongs_to relationship data is not possible/;
530 my ($cdA, $cdB, $cdC) = $cd_rs->search(
531 {title=>[sort map {$_->{title}} @$cds]},
532 {order_by=>'title ASC'},
535 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
536 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
537 is($cdA->title, 'Some CD3BB', 'Found Expected title');
538 is($cdA->artist->name, 'Fred BloggsCBB', 'Set Artist to FredCBB');
540 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
541 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
542 is($cdB->title, 'Some CD4BB', 'Found Expected title');
543 is($cdB->artist->name, 'Fred BloggsDBB', 'Set Artist to FredDBB');
545 isa_ok($cdC, 'DBICTest::CD', 'Created CD');
546 isa_ok($cdC->artist, 'DBICTest::Artist', 'Set Artist');
547 is($cdC->title, 'Some CD5BB', 'Found Expected title');
548 is( $cdC->artist->name, undef, 'Set Artist to something undefined');
554 ## This first group of tests checks to make sure we can call populate
555 ## with the parent having many children and let the keys be automatic
559 name => 'VOID_Angsty-Whiny Girl',
561 { title => 'VOID_My First CD', year => 2006 },
562 { title => 'VOID_Yet More Tweeny-Pop crap', year => 2007 },
566 name => 'VOID_Manufactured Crap',
569 name => 'VOID_Like I Give a Damn',
571 { title => 'VOID_My parents sold me to a record company' ,year => 2005 },
572 { title => 'VOID_Why Am I So Ugly?', year => 2006 },
573 { title => 'VOID_I Got Surgery and am now Popular', year => 2007 }
577 name => 'VOID_Formerly Named',
579 { title => 'VOID_One Hit Wonder', year => 2006 },
584 ## Get the result row objects.
586 $art_rs->populate($artists);
588 my ($girl, $formerly, $damn, $crap) = $art_rs->search(
589 {name=>[sort map {$_->{name}} @$artists]},
590 {order_by=>'name ASC'},
593 ## Do we have the right object?
595 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
596 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
597 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
598 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
600 ## Find the expected information?
602 ok( $crap->name eq 'VOID_Manufactured Crap', "Got Correct name for result object");
603 ok( $girl->name eq 'VOID_Angsty-Whiny Girl', "Got Correct name for result object");
604 ok( $damn->name eq 'VOID_Like I Give a Damn', "Got Correct name for result object");
605 ok( $formerly->name eq 'VOID_Formerly Named', "Got Correct name for result object");
607 ## Create the expected children sub objects?
608 ok( $crap->can('cds'), "Has cds relationship");
609 ok( $girl->can('cds'), "Has cds relationship");
610 ok( $damn->can('cds'), "Has cds relationship");
611 ok( $formerly->can('cds'), "Has cds relationship");
613 ok( $crap->cds->count == 0, "got Expected Number of Cds");
614 ok( $girl->cds->count == 2, "got Expected Number of Cds");
615 ok( $damn->cds->count == 3, "got Expected Number of Cds");
616 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
618 ## Did the cds get expected information?
620 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
622 ok($cd1, "Got a got CD");
623 ok($cd2, "Got a got CD");
624 ok( $cd1->title eq "VOID_My First CD", "Got Expected CD Title");
625 ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
630 $restricted_art_rs->populate([
632 name => 'VOID More Manufactured Crap',
636 my $more_crap = $art_rs->search({
637 name => 'VOID More Manufactured Crap'
640 ## Did it use the condition in the resultset?
641 $more_crap->discard_changes;
642 cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object");
643 cmp_ok( $more_crap->charfield, '==', $more_crap->id + 5, "Got Correct charfield for result object");
647 ARRAYREF_OF_ARRAYREF_STYLE: {
650 [1000, 'A Formally Unknown Singer'],
651 [1001, 'A singer that jumped the shark two albums ago'],
652 [1002, 'An actually cool singer.'],
655 ok my $unknown = $art_rs->find(1000), "got Unknown";
656 ok my $jumped = $art_rs->find(1001), "got Jumped";
657 ok my $cool = $art_rs->find(1002), "got Cool";
659 is $unknown->name, 'A Formally Unknown Singer', 'Correct Name';
660 is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name';
661 is $cool->name, 'An actually cool singer.', 'Correct Name';
663 my ($cooler, $lamer) = $restricted_art_rs->populate([
669 is $cooler->name, 'Cooler', 'Correct Name';
670 is $lamer->name, 'Lamer', 'Correct Name';
672 for ($cooler, $lamer) {
674 cmp_ok( $_->rank, '==', 42, "Got Correct rank for result object");
675 cmp_ok( $_->charfield, '==', $_->id + 5, "Got Correct charfield for result object");
678 ARRAY_CONTEXT_WITH_COND_FROM_RS: {
680 my ($mega_lamer) = $restricted_art_rs->populate([
682 name => 'Mega Lamer',
686 ## Did it use the condition in the resultset?
687 $mega_lamer->discard_changes;
688 cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object");
689 cmp_ok( $mega_lamer->charfield, '==', $mega_lamer->id + 5, "Got Correct charfield for result object");
692 VOID_CONTEXT_WITH_COND_FROM_RS: {
694 $restricted_art_rs->populate([
696 name => 'VOID Mega Lamer',
700 my $mega_lamer = $art_rs->search({
701 name => 'VOID Mega Lamer'
704 ## Did it use the condition in the resultset?
705 cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object");
706 cmp_ok( $mega_lamer->charfield, '==', $mega_lamer->id + 5, "Got Correct charfield for result object");
713 [ columns_only => [ [qw(name rank charfield)] ] ],
715 my ($desc, $arg) = @{$_};
717 $schema->is_executed_sql_bind( sub {
720 lives_ok { $rs->populate($arg); 1 } "$desc populate in void context lives";
722 my @r = $art_rs->populate($arg);
723 is_deeply( \@r, [], "$desc populate in list context returns empty list" );
725 my $r = $art_rs->populate($arg);
726 is( $r, undef, "$desc populate in scalar context returns undef" );
728 }, [], "$desc populate executed no statements" );