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();
26 my $art_rs = $schema->resultset('Artist');
27 my $cd_rs = $schema->resultset('CD');
29 ok( $schema, 'Got a Schema object');
30 ok( $art_rs, 'Got Good Artist Resultset');
31 ok( $cd_rs, 'Got Good CD Resultset');
34 ## ----------------------------------------------------------------------------
35 ## Array context tests
36 ## ----------------------------------------------------------------------------
40 ## These first set of tests are cake because array context just delegates
41 ## all it's processing to $resultset->create
45 ## This first group of tests checks to make sure we can call populate
46 ## with the parent having many children and let the keys be automatic
50 name => 'Angsty-Whiny Girl',
52 { title => 'My First CD', year => 2006 },
53 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
57 name => 'Manufactured Crap',
60 name => 'Like I Give a Damn',
62 { title => 'My parents sold me to a record company' ,year => 2005 },
63 { title => 'Why Am I So Ugly?', year => 2006 },
64 { title => 'I Got Surgery and am now Popular', year => 2007 }
68 name => 'Formerly Named',
70 { title => 'One Hit Wonder', year => 2006 },
75 ## Get the result row objects.
77 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
79 ## Do we have the right object?
81 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
82 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
83 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
84 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
86 ## Find the expected information?
88 ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
89 ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
90 ok( $damn->name eq 'Like I Give a Damn', "Got Correct name for result object");
91 ok( $formerly->name eq 'Formerly Named', "Got Correct name for result object");
93 ## Create the expected children sub objects?
95 ok( $crap->cds->count == 0, "got Expected Number of Cds");
96 ok( $girl->cds->count == 2, "got Expected Number of Cds");
97 ok( $damn->cds->count == 3, "got Expected Number of Cds");
98 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
100 ## Did the cds get expected information?
102 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
104 ok( $cd1->title eq "My First CD", "Got Expected CD Title");
105 ok( $cd2->title eq "Yet More Tweeny-Pop crap", "Got Expected CD Title");
110 ## This group tests the ability to specify the PK in the parent and let
111 ## DBIC transparently pass the PK down to the Child and also let's the
112 ## child create any other needed PK's for itself.
114 my $aid = $art_rs->get_column('artistid')->max || 0;
116 my $first_aid = ++$aid;
120 artistid => $first_aid,
121 name => 'PK_Angsty-Whiny Girl',
123 { artist => $first_aid, title => 'PK_My First CD', year => 2006 },
124 { artist => $first_aid, title => 'PK_Yet More Tweeny-Pop crap', year => 2007 },
129 name => 'PK_Manufactured Crap',
133 name => 'PK_Like I Give a Damn',
135 { title => 'PK_My parents sold me to a record company' ,year => 2005 },
136 { title => 'PK_Why Am I So Ugly?', year => 2006 },
137 { title => 'PK_I Got Surgery and am now Popular', year => 2007 }
142 name => 'PK_Formerly Named',
144 { title => 'PK_One Hit Wonder', year => 2006 },
149 ## Get the result row objects.
151 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
153 ## Do we have the right object?
155 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
156 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
157 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
158 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
160 ## Find the expected information?
162 ok( $crap->name eq 'PK_Manufactured Crap', "Got Correct name for result object");
163 ok( $girl->name eq 'PK_Angsty-Whiny Girl', "Got Correct name for result object");
164 ok( $girl->artistid == $first_aid, "Got Correct artist PK for result object");
165 ok( $damn->name eq 'PK_Like I Give a Damn', "Got Correct name for result object");
166 ok( $formerly->name eq 'PK_Formerly Named', "Got Correct name for result object");
168 ## Create the expected children sub objects?
170 ok( $crap->cds->count == 0, "got Expected Number of Cds");
171 ok( $girl->cds->count == 2, "got Expected Number of Cds");
172 ok( $damn->cds->count == 3, "got Expected Number of Cds");
173 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
175 ## Did the cds get expected information?
177 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
179 ok( $cd1->title eq "PK_My First CD", "Got Expected CD Title");
180 ok( $cd2->title eq "PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
185 ## Test from a belongs_to perspective, should create artist first,
186 ## then CD with artistid. This test we let the system automatically
187 ## create the PK's. Chances are good you'll use it this way mostly.
193 artist => { name => 'Fred BloggsC'},
198 artist => { name => 'Fred BloggsD'},
202 my ($cdA, $cdB) = $cd_rs->populate($cds);
205 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
206 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
207 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
210 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
211 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
212 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
215 BELONGS_TO_WITH_PKs: {
217 ## Test from a belongs_to perspective, should create artist first,
218 ## then CD with artistid. This time we try setting the PK's
220 my $aid = $art_rs->get_column('artistid')->max || 0;
226 artist => { artistid=> ++$aid, name => 'Fred BloggsC'},
231 artist => { artistid=> ++$aid, name => 'Fred BloggsD'},
235 my ($cdA, $cdB) = $cd_rs->populate($cds);
237 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
238 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
239 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
241 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
242 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
243 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
244 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
249 ## ----------------------------------------------------------------------------
250 ## Void context tests
251 ## ----------------------------------------------------------------------------
255 ## All these tests check the ability to use populate without asking for
256 ## any returned resultsets. This uses bulk_insert as much as possible
257 ## in order to increase speed.
261 ## This first group of tests checks to make sure we can call populate
262 ## with the parent having many children and the parent PK is set
264 my $aid = $art_rs->get_column('artistid')->max || 0;
266 my $first_aid = ++$aid;
270 artistid => $first_aid,
271 name => 'VOID_PK_Angsty-Whiny Girl',
273 { artist => $first_aid, title => 'VOID_PK_My First CD', year => 2006 },
274 { artist => $first_aid, title => 'VOID_PK_Yet More Tweeny-Pop crap', year => 2007 },
279 name => 'VOID_PK_Manufactured Crap',
283 name => 'VOID_PK_Like I Give a Damn',
285 { title => 'VOID_PK_My parents sold me to a record company' ,year => 2005 },
286 { title => 'VOID_PK_Why Am I So Ugly?', year => 2006 },
287 { title => 'VOID_PK_I Got Surgery and am now Popular', year => 2007 }
292 name => 'VOID_PK_Formerly Named',
294 { title => 'VOID_PK_One Hit Wonder', year => 2006 },
299 ## Get the result row objects.
301 $art_rs->populate($artists);
303 my ($girl, $formerly, $damn, $crap) = $art_rs->search(
304 {name=>[sort map {$_->{name}} @$artists]},
305 {order_by=>'name ASC'},
308 ## Do we have the right object?
310 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
311 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
312 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
313 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
315 ## Find the expected information?
317 ok( $crap->name eq 'VOID_PK_Manufactured Crap', "Got Correct name for result object");
318 ok( $girl->name eq 'VOID_PK_Angsty-Whiny Girl', "Got Correct name for result object");
319 ok( $damn->name eq 'VOID_PK_Like I Give a Damn', "Got Correct name for result object");
320 ok( $formerly->name eq 'VOID_PK_Formerly Named', "Got Correct name for result object");
322 ## Create the expected children sub objects?
323 ok( $crap->can('cds'), "Has cds relationship");
324 ok( $girl->can('cds'), "Has cds relationship");
325 ok( $damn->can('cds'), "Has cds relationship");
326 ok( $formerly->can('cds'), "Has cds relationship");
328 ok( $crap->cds->count == 0, "got Expected Number of Cds");
329 ok( $girl->cds->count == 2, "got Expected Number of Cds");
330 ok( $damn->cds->count == 3, "got Expected Number of Cds");
331 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
333 ## Did the cds get expected information?
335 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
337 ok( $cd1->title eq "VOID_PK_My First CD", "Got Expected CD Title");
338 ok( $cd2->title eq "VOID_PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
342 BELONGS_TO_WITH_PKs: {
344 ## Test from a belongs_to perspective, should create artist first,
345 ## then CD with artistid. This time we try setting the PK's
347 my $aid = $art_rs->get_column('artistid')->max || 0;
351 title => 'Some CD3B',
353 artist => { artistid=> ++$aid, name => 'Fred BloggsCB'},
356 title => 'Some CD4B',
358 artist => { artistid=> ++$aid, name => 'Fred BloggsDB'},
362 $cd_rs->populate($cds);
364 my ($cdA, $cdB) = $cd_rs->search(
365 {title=>[sort map {$_->{title}} @$cds]},
366 {order_by=>'title ASC'},
369 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
370 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
371 is($cdA->artist->name, 'Fred BloggsCB', 'Set Artist to FredCB');
373 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
374 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
375 is($cdB->artist->name, 'Fred BloggsDB', 'Set Artist to FredDB');
376 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
381 ## Test from a belongs_to perspective, should create artist first,
382 ## then CD with artistid.
386 title => 'Some CD3BB',
388 artist => { name => 'Fred BloggsCBB'},
391 title => 'Some CD4BB',
393 artist => { name => 'Fred BloggsDBB'},
397 $cd_rs->populate($cds);
399 my ($cdA, $cdB) = $cd_rs->search(
400 {title=>[sort map {$_->{title}} @$cds]},
401 {order_by=>'title ASC'},
404 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
405 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
406 is($cdA->title, 'Some CD3BB', 'Found Expected title');
407 is($cdA->artist->name, 'Fred BloggsCBB', 'Set Artist to FredCBB');
409 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
410 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
411 is($cdB->title, 'Some CD4BB', 'Found Expected title');
412 is($cdB->artist->name, 'Fred BloggsDBB', 'Set Artist to FredDBB');
418 ## This first group of tests checks to make sure we can call populate
419 ## with the parent having many children and let the keys be automatic
423 name => 'VOID_Angsty-Whiny Girl',
425 { title => 'VOID_My First CD', year => 2006 },
426 { title => 'VOID_Yet More Tweeny-Pop crap', year => 2007 },
430 name => 'VOID_Manufactured Crap',
433 name => 'VOID_Like I Give a Damn',
435 { title => 'VOID_My parents sold me to a record company' ,year => 2005 },
436 { title => 'VOID_Why Am I So Ugly?', year => 2006 },
437 { title => 'VOID_I Got Surgery and am now Popular', year => 2007 }
441 name => 'VOID_Formerly Named',
443 { title => 'VOID_One Hit Wonder', year => 2006 },
448 ## Get the result row objects.
450 $art_rs->populate($artists);
452 my ($girl, $formerly, $damn, $crap) = $art_rs->search(
453 {name=>[sort map {$_->{name}} @$artists]},
454 {order_by=>'name ASC'},
457 ## Do we have the right object?
459 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
460 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
461 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
462 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
464 ## Find the expected information?
466 ok( $crap->name eq 'VOID_Manufactured Crap', "Got Correct name for result object");
467 ok( $girl->name eq 'VOID_Angsty-Whiny Girl', "Got Correct name for result object");
468 ok( $damn->name eq 'VOID_Like I Give a Damn', "Got Correct name for result object");
469 ok( $formerly->name eq 'VOID_Formerly Named', "Got Correct name for result object");
471 ## Create the expected children sub objects?
472 ok( $crap->can('cds'), "Has cds relationship");
473 ok( $girl->can('cds'), "Has cds relationship");
474 ok( $damn->can('cds'), "Has cds relationship");
475 ok( $formerly->can('cds'), "Has cds relationship");
477 ok( $crap->cds->count == 0, "got Expected Number of Cds");
478 ok( $girl->cds->count == 2, "got Expected Number of Cds");
479 ok( $damn->cds->count == 3, "got Expected Number of Cds");
480 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
482 ## Did the cds get expected information?
484 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
486 ok($cd1, "Got a got CD");
487 ok($cd2, "Got a got CD");
488 ok( $cd1->title eq "VOID_My First CD", "Got Expected CD Title");
489 ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");