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 each 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();
27 $schema->resultset('Genre')->create({
32 my $art_rs = $schema->resultset('Artist');
33 my $restricted_art_rs = $art_rs->search({rank => 42});
34 my $cd_rs = $schema->resultset('CD');
35 my $restricted_cd_rs = $cd_rs->search({genreid => 1});
37 ok( $schema, 'Got a Schema object');
38 ok( $art_rs, 'Got Good Artist Resultset');
39 ok( $cd_rs, 'Got Good CD Resultset');
42 ## ----------------------------------------------------------------------------
43 ## Schema populate Tests
44 ## ----------------------------------------------------------------------------
48 ## Test to make sure that the old $schema->populate is using the new method
49 ## for $resultset->populate when in void context and with sub objects.
51 $schema->populate('Artist', [
55 {title=>"001Title1", year=>2000},
56 {title=>"001Title2", year=>2001},
57 {title=>"001Title3", year=>2002},
59 ["002Second Artist", []],
61 {title=>"003Title1", year=>2005},
64 {title=>"004Title1", year=>2010}
68 isa_ok $schema, 'DBIx::Class::Schema';
70 my ($undef, $artist1, $artist2, $artist3 ) = $schema->resultset('Artist')->search({
71 name=>["001First Artist","002Second Artist","003Third Artist", undef]},
72 {order_by=>'name ASC'})->all;
74 isa_ok $artist1, 'DBICTest::Artist';
75 isa_ok $artist2, 'DBICTest::Artist';
76 isa_ok $artist3, 'DBICTest::Artist';
77 isa_ok $undef, 'DBICTest::Artist';
79 ok $artist1->name eq '001First Artist', "Got Expected Artist Name for Artist001";
80 ok $artist2->name eq '002Second Artist', "Got Expected Artist Name for Artist002";
81 ok $artist3->name eq '003Third Artist', "Got Expected Artist Name for Artist003";
82 ok !defined $undef->name, "Got Expected Artist Name for Artist004";
84 ok $artist1->cds->count eq 3, "Got Right number of CDs for Artist1";
85 ok $artist2->cds->count eq 0, "Got Right number of CDs for Artist2";
86 ok $artist3->cds->count eq 1, "Got Right number of CDs for Artist3";
87 ok $undef->cds->count eq 1, "Got Right number of CDs for Artist4";
91 my ($cd1, $cd2, $cd3) = $artist1->cds->search(undef, {order_by=>'year ASC'});
93 isa_ok $cd1, 'DBICTest::CD';
94 isa_ok $cd2, 'DBICTest::CD';
95 isa_ok $cd3, 'DBICTest::CD';
97 ok $cd1->year == 2000;
98 ok $cd2->year == 2001;
99 ok $cd3->year == 2002;
101 ok $cd1->title eq '001Title1';
102 ok $cd2->title eq '001Title2';
103 ok $cd3->title eq '001Title3';
108 my ($cd1) = $artist3->cds->search(undef, {order_by=>'year ASC'});
110 isa_ok $cd1, 'DBICTest::CD';
112 ok $cd1->year == 2005;
113 ok $cd1->title eq '003Title1';
118 my ($cd1) = $undef->cds->search(undef, {order_by=>'year ASC'});
120 isa_ok $cd1, 'DBICTest::CD';
122 ok $cd1->year == 2010;
123 ok $cd1->title eq '004Title1';
126 ## Need to do some cleanup so that later tests don't get borked
132 ## ----------------------------------------------------------------------------
133 ## Array context tests
134 ## ----------------------------------------------------------------------------
138 ## These first set of tests are cake because array context just delegates
139 ## all it's processing to $resultset->create
143 ## This first group of tests checks to make sure we can call populate
144 ## with the parent having many children and let the keys be automatic
148 name => 'Angsty-Whiny Girl',
150 { title => 'My First CD', year => 2006 },
151 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
155 name => 'Manufactured Crap',
158 name => 'Like I Give a Damn',
160 { title => 'My parents sold me to a record company' ,year => 2005 },
161 { title => 'Why Am I So Ugly?', year => 2006 },
162 { title => 'I Got Surgery and am now Popular', year => 2007 }
166 name => 'Formerly Named',
168 { title => 'One Hit Wonder', year => 2006 },
173 ## Get the result row objects.
175 my ($girl, $crap, $damn, $formerly) = $restricted_art_rs->populate($artists);
177 ## Do we have the right object?
179 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
180 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
181 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
182 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
184 ## Find the expected information?
186 ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
187 ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
188 ok( $damn->name eq 'Like I Give a Damn', "Got Correct name for result object");
189 ok( $formerly->name eq 'Formerly Named', "Got Correct name for result object");
190 cmp_ok( $crap->rank, '==', 42, "Got Correct rank for result object");
191 cmp_ok( $girl->rank, '==', 42, "Got Correct rank for result object");
192 cmp_ok( $damn->rank, '==', 42, "Got Correct rank for result object");
193 cmp_ok( $formerly->rank, '==', 42, "Got Correct rank for result object");
195 ## Create the expected children sub objects?
197 ok( $crap->cds->count == 0, "got Expected Number of Cds");
198 ok( $girl->cds->count == 2, "got Expected Number of Cds");
199 ok( $damn->cds->count == 3, "got Expected Number of Cds");
200 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
202 ## Did the cds get expected information?
204 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
206 ok( $cd1->title eq "My First CD", "Got Expected CD Title");
207 ok( $cd2->title eq "Yet More Tweeny-Pop crap", "Got Expected CD Title");
212 ## This group tests the ability to specify the PK in the parent and let
213 ## DBIC transparently pass the PK down to the Child and also let's the
214 ## child create any other needed PK's for itself.
216 my $aid = $art_rs->get_column('artistid')->max || 0;
218 my $first_aid = ++$aid;
222 artistid => $first_aid,
223 name => 'PK_Angsty-Whiny Girl',
225 { artist => $first_aid, title => 'PK_My First CD', year => 2006 },
226 { artist => $first_aid, title => 'PK_Yet More Tweeny-Pop crap', year => 2007 },
231 name => 'PK_Manufactured Crap',
235 name => 'PK_Like I Give a Damn',
237 { title => 'PK_My parents sold me to a record company' ,year => 2005 },
238 { title => 'PK_Why Am I So Ugly?', year => 2006 },
239 { title => 'PK_I Got Surgery and am now Popular', year => 2007 }
244 name => 'PK_Formerly Named',
246 { title => 'PK_One Hit Wonder', year => 2006 },
251 ## Get the result row objects.
253 my ($girl, $crap, $damn, $formerly) = $restricted_art_rs->populate($artists);
255 ## Do we have the right object?
257 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
258 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
259 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
260 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
262 ## Find the expected information?
264 ok( $crap->name eq 'PK_Manufactured Crap', "Got Correct name for result object");
265 ok( $girl->name eq 'PK_Angsty-Whiny Girl', "Got Correct name for result object");
266 ok( $girl->artistid == $first_aid, "Got Correct artist PK for result object");
267 ok( $damn->name eq 'PK_Like I Give a Damn', "Got Correct name for result object");
268 ok( $formerly->name eq 'PK_Formerly Named', "Got Correct name for result object");
269 cmp_ok( $crap->rank, '==', 42, "Got Correct rank for result object");
270 cmp_ok( $girl->rank, '==', 42, "Got Correct rank for result object");
271 cmp_ok( $damn->rank, '==', 42, "Got Correct rank for result object");
272 cmp_ok( $formerly->rank, '==', 42, "Got Correct rank for result object");
274 ## Create the expected children sub objects?
276 ok( $crap->cds->count == 0, "got Expected Number of Cds");
277 ok( $girl->cds->count == 2, "got Expected Number of Cds");
278 ok( $damn->cds->count == 3, "got Expected Number of Cds");
279 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
281 ## Did the cds get expected information?
283 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
285 ok( $cd1->title eq "PK_My First CD", "Got Expected CD Title");
286 ok( $cd2->title eq "PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
291 ## Test from a belongs_to perspective, should create artist first,
292 ## then CD with artistid. This test we let the system automatically
293 ## create the PK's. Chances are good you'll use it this way mostly.
299 artist => { name => 'Fred BloggsC'},
304 artist => { name => 'Fred BloggsD'},
308 my ($cdA, $cdB) = $restricted_cd_rs->populate($cds);
311 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
312 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
313 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
314 isa_ok($cdA->genre, 'DBICTest::Genre', 'Set Genre');
315 cmp_ok($cdA->genre->id, '==', 1, 'Set Genre ID to 1');
318 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
319 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
320 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
321 isa_ok($cdB->genre, 'DBICTest::Genre', 'Set Genre');
322 cmp_ok($cdB->genre->id, '==', 1, 'Set Genre ID to 1');
325 BELONGS_TO_WITH_PKs: {
327 ## Test from a belongs_to perspective, should create artist first,
328 ## then CD with artistid. This time we try setting the PK's
330 my $aid = $art_rs->get_column('artistid')->max || 0;
336 artist => { artistid=> ++$aid, name => 'Fred BloggsC'},
341 artist => { artistid=> ++$aid, name => 'Fred BloggsD'},
345 my ($cdA, $cdB) = $restricted_cd_rs->populate($cds);
347 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
348 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
349 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
350 isa_ok($cdA->genre, 'DBICTest::Genre', 'Set Genre');
351 cmp_ok($cdA->genre->id, '==', 1, 'Set Genre ID to 1');
353 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
354 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
355 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
356 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
357 isa_ok($cdB->genre, 'DBICTest::Genre', 'Set Genre');
358 cmp_ok($cdB->genre->id, '==', 1, 'Set Genre ID to 1');
363 ## ----------------------------------------------------------------------------
364 ## Void context tests
365 ## ----------------------------------------------------------------------------
369 ## All these tests check the ability to use populate without asking for
370 ## any returned resultsets. This uses bulk_insert as much as possible
371 ## in order to increase speed.
375 ## This first group of tests checks to make sure we can call populate
376 ## with the parent having many children and the parent PK is set
378 my $aid = $art_rs->get_column('artistid')->max || 0;
380 my $first_aid = ++$aid;
384 artistid => $first_aid,
385 name => 'VOID_PK_Angsty-Whiny Girl',
387 { artist => $first_aid, title => 'VOID_PK_My First CD', year => 2006 },
388 { artist => $first_aid, title => 'VOID_PK_Yet More Tweeny-Pop crap', year => 2007 },
393 name => 'VOID_PK_Manufactured Crap',
397 name => 'VOID_PK_Like I Give a Damn',
399 { title => 'VOID_PK_My parents sold me to a record company' ,year => 2005 },
400 { title => 'VOID_PK_Why Am I So Ugly?', year => 2006 },
401 { title => 'VOID_PK_I Got Surgery and am now Popular', year => 2007 }
406 name => 'VOID_PK_Formerly Named',
408 { title => 'VOID_PK_One Hit Wonder', year => 2006 },
415 { title => 'VOID_PK_Zundef test', year => 2006 },
420 ## Get the result row objects.
422 $restricted_art_rs->populate($artists);
424 my ($undef, $girl, $formerly, $damn, $crap) = $art_rs->search(
426 {name=>[ map { $_->{name} } @$artists]},
427 {order_by=>'name ASC'},
430 ## Do we have the right object?
432 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
433 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
434 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
435 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
436 isa_ok( $undef, 'DBICTest::Artist', "Got 'Artist'");
438 ## Find the expected information?
440 ok( $crap->name eq 'VOID_PK_Manufactured Crap', "Got Correct name 'VOID_PK_Manufactured Crap' for result object");
441 ok( $girl->name eq 'VOID_PK_Angsty-Whiny Girl', "Got Correct name for result object");
442 ok( $damn->name eq 'VOID_PK_Like I Give a Damn', "Got Correct name for result object");
443 ok( $formerly->name eq 'VOID_PK_Formerly Named', "Got Correct name for result object");
444 ok( !defined $undef->name, "Got Correct name 'is undef' for result object");
445 cmp_ok( $crap->rank, '==', 42, "Got Correct rank for result object");
446 cmp_ok( $girl->rank, '==', 42, "Got Correct rank for result object");
447 cmp_ok( $damn->rank, '==', 42, "Got Correct rank for result object");
448 cmp_ok( $formerly->rank, '==', 42, "Got Correct rank for result object");
449 cmp_ok( $undef->rank, '==', 42, "Got Correct rank for result object");
451 ## Create the expected children sub objects?
452 ok( $crap->can('cds'), "Has cds relationship");
453 ok( $girl->can('cds'), "Has cds relationship");
454 ok( $damn->can('cds'), "Has cds relationship");
455 ok( $formerly->can('cds'), "Has cds relationship");
456 ok( $undef->can('cds'), "Has cds relationship");
458 ok( $crap->cds->count == 0, "got Expected Number of Cds");
459 ok( $girl->cds->count == 2, "got Expected Number of Cds");
460 ok( $damn->cds->count == 3, "got Expected Number of Cds");
461 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
462 ok( $undef->cds->count == 1, "got Expected Number of Cds");
464 ## Did the cds get expected information?
466 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
468 ok( $cd1->title eq "VOID_PK_My First CD", "Got Expected CD Title");
469 ok( $cd2->title eq "VOID_PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
473 BELONGS_TO_WITH_PKs: {
475 ## Test from a belongs_to perspective, should create artist first,
476 ## then CD with artistid. This time we try setting the PK's
478 my $aid = $art_rs->get_column('artistid')->max || 0;
482 title => 'Some CD3B',
484 artist => { artistid=> ++$aid, name => 'Fred BloggsCB'},
487 title => 'Some CD4B',
489 artist => { artistid=> ++$aid, name => 'Fred BloggsDB'},
493 $restricted_cd_rs->populate($cds);
495 my ($cdA, $cdB) = $cd_rs->search(
496 {title=>[sort map {$_->{title}} @$cds]},
497 {order_by=>'title ASC'},
500 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
501 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
502 is($cdA->artist->name, 'Fred BloggsCB', 'Set Artist to FredCB');
503 isa_ok($cdA->genre, 'DBICTest::Genre', 'Set Genre');
504 cmp_ok($cdA->genre->id, '==', 1, 'Set Genre ID to 1');
506 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
507 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
508 is($cdB->artist->name, 'Fred BloggsDB', 'Set Artist to FredDB');
509 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
510 isa_ok($cdB->genre, 'DBICTest::Genre', 'Set Genre');
511 cmp_ok($cdB->genre->id, '==', 1, 'Set Genre ID to 1');
516 ## Test from a belongs_to perspective, should create artist first,
517 ## then CD with artistid.
521 title => 'Some CD3BB',
523 artist => { name => 'Fred BloggsCBB'},
526 title => 'Some CD4BB',
528 artist => { name => 'Fred BloggsDBB'},
531 title => 'Some CD5BB',
533 artist => { name => undef},
537 $restricted_cd_rs->populate($cds);
539 my ($cdA, $cdB, $cdC) = $cd_rs->search(
540 {title=>[sort map {$_->{title}} @$cds]},
541 {order_by=>'title ASC'},
544 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
545 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
546 is($cdA->title, 'Some CD3BB', 'Found Expected title');
547 is($cdA->artist->name, 'Fred BloggsCBB', 'Set Artist to FredCBB');
548 isa_ok($cdA->genre, 'DBICTest::Genre', 'Set Genre');
549 cmp_ok($cdA->genre->id, '==', 1, 'Set Genre ID to 1');
551 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
552 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
553 is($cdB->title, 'Some CD4BB', 'Found Expected title');
554 is($cdB->artist->name, 'Fred BloggsDBB', 'Set Artist to FredDBB');
555 isa_ok($cdB->genre, 'DBICTest::Genre', 'Set Genre');
556 cmp_ok($cdB->genre->id, '==', 1, 'Set Genre ID to 1');
558 isa_ok($cdC, 'DBICTest::CD', 'Created CD');
559 isa_ok($cdC->artist, 'DBICTest::Artist', 'Set Artist');
560 is($cdC->title, 'Some CD5BB', 'Found Expected title');
561 is( $cdC->artist->name, undef, 'Set Artist to something undefined');
562 isa_ok($cdC->genre, 'DBICTest::Genre', 'Set Genre');
563 cmp_ok($cdC->genre->id, '==', 1, 'Set Genre ID to 1');
569 ## This first group of tests checks to make sure we can call populate
570 ## with the parent having many children and let the keys be automatic
574 name => 'VOID_Angsty-Whiny Girl',
576 { title => 'VOID_My First CD', year => 2006 },
577 { title => 'VOID_Yet More Tweeny-Pop crap', year => 2007 },
581 name => 'VOID_Manufactured Crap',
584 name => 'VOID_Like I Give a Damn',
586 { title => 'VOID_My parents sold me to a record company' ,year => 2005 },
587 { title => 'VOID_Why Am I So Ugly?', year => 2006 },
588 { title => 'VOID_I Got Surgery and am now Popular', year => 2007 }
592 name => 'VOID_Formerly Named',
594 { title => 'VOID_One Hit Wonder', year => 2006 },
599 ## Get the result row objects.
601 $restricted_art_rs->populate($artists);
603 my ($girl, $formerly, $damn, $crap) = $art_rs->search(
604 {name=>[sort map {$_->{name}} @$artists]},
605 {order_by=>'name ASC'},
608 ## Do we have the right object?
610 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
611 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
612 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
613 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
615 ## Find the expected information?
617 ok( $crap->name eq 'VOID_Manufactured Crap', "Got Correct name for result object");
618 ok( $girl->name eq 'VOID_Angsty-Whiny Girl', "Got Correct name for result object");
619 ok( $damn->name eq 'VOID_Like I Give a Damn', "Got Correct name for result object");
620 ok( $formerly->name eq 'VOID_Formerly Named', "Got Correct name for result object");
621 cmp_ok( $crap->rank, '==', 42, "Got Correct rank for result object");
622 cmp_ok( $girl->rank, '==', 42, "Got Correct rank for result object");
623 cmp_ok( $damn->rank, '==', 42, "Got Correct rank for result object");
624 cmp_ok( $formerly->rank, '==', 42, "Got Correct rank for result object");
626 ## Create the expected children sub objects?
627 ok( $crap->can('cds'), "Has cds relationship");
628 ok( $girl->can('cds'), "Has cds relationship");
629 ok( $damn->can('cds'), "Has cds relationship");
630 ok( $formerly->can('cds'), "Has cds relationship");
632 ok( $crap->cds->count == 0, "got Expected Number of Cds");
633 ok( $girl->cds->count == 2, "got Expected Number of Cds");
634 ok( $damn->cds->count == 3, "got Expected Number of Cds");
635 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
637 ## Did the cds get expected information?
639 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
641 ok($cd1, "Got a got CD");
642 ok($cd2, "Got a got CD");
643 ok( $cd1->title eq "VOID_My First CD", "Got Expected CD Title");
644 ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
649 ARRAYREF_OF_ARRAYREF_STYLE: {
650 $restricted_art_rs->populate([
652 [1000, 'A Formally Unknown Singer'],
653 [1001, 'A singer that jumped the shark two albums ago'],
654 [1002, 'An actually cool singer.'],
657 ok my $unknown = $art_rs->find(1000), "got Unknown";
658 ok my $jumped = $art_rs->find(1001), "got Jumped";
659 ok my $cool = $art_rs->find(1002), "got Cool";
661 is $unknown->name, 'A Formally Unknown Singer', 'Correct Name';
662 is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name';
663 is $cool->name, 'An actually cool singer.', 'Correct Name';
665 cmp_ok $unknown->rank, '==', 42, 'Correct Rank';
666 cmp_ok $jumped->rank, '==', 42, 'Correct Rank';
667 cmp_ok $cool->rank, '==', 42, 'Correct Rank';
669 my ($cooler, $lamer) = $restricted_art_rs->populate([
675 is $cooler->name, 'Cooler', 'Correct Name';
676 is $lamer->name, 'Lamer', 'Correct Name';
678 cmp_ok $cooler->rank, '==', 42, 'Correct Rank';
679 cmp_ok $lamer->rank, '==', 42, 'Correct Rank';