Merge 'trunk' into 'cdbicompat_integration'
[dbsrgits/DBIx-Class.git] / t / 101populate_rs.t
CommitLineData
e8aef98d 1## ----------------------------------------------------------------------------
2## Tests for the $resultset->populate method.
3##
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
7##
8## Also need to test some stuff that should generate errors.
9## ----------------------------------------------------------------------------
10
81ab7888 11use strict;
12use warnings;
13
14use Test::More;
15use lib qw(t/lib);
16use DBICTest;
17
7c325d53 18plan tests => 134;
735ba30e 19
81ab7888 20
e8aef98d 21## ----------------------------------------------------------------------------
22## Get a Schema and some ResultSets we can play with.
23## ----------------------------------------------------------------------------
24
25my $schema = DBICTest->init_schema();
26my $art_rs = $schema->resultset('Artist');
27my $cd_rs = $schema->resultset('CD');
28
29ok( $schema, 'Got a Schema object');
30ok( $art_rs, 'Got Good Artist Resultset');
31ok( $cd_rs, 'Got Good CD Resultset');
32
33
34## ----------------------------------------------------------------------------
8b93a938 35## Schema populate Tests
36## ----------------------------------------------------------------------------
37
7c325d53 38SCHEMA_POPULATE1: {
8b93a938 39
40 ## Test to make sure that the old $schema->populate is using the new method
41 ## for $resultset->populate when in void context and with sub objects.
42
43 $schema->populate('Artist', [
44
45 [qw/name cds/],
46 ["001First Artist", [
47 {title=>"001Title1", year=>2000},
48 {title=>"001Title2", year=>2001},
49 {title=>"001Title3", year=>2002},
50 ]],
51 ["002Second Artist", []],
52 ["003Third Artist", [
53 {title=>"003Title1", year=>2005},
54 ]],
7c325d53 55 [undef, [
56 {title=>"004Title1", year=>2010}
57 ]],
8b93a938 58 ]);
59
60 isa_ok $schema, 'DBIx::Class::Schema';
61
7c325d53 62 my ($undef, $artist1, $artist2, $artist3 ) = $schema->resultset('Artist')->search({
63 name=>["001First Artist","002Second Artist","003Third Artist", undef]},
8b93a938 64 {order_by=>'name ASC'})->all;
65
66 isa_ok $artist1, 'DBICTest::Artist';
67 isa_ok $artist2, 'DBICTest::Artist';
68 isa_ok $artist3, 'DBICTest::Artist';
7c325d53 69 isa_ok $undef, 'DBICTest::Artist';
8b93a938 70
71 ok $artist1->name eq '001First Artist', "Got Expected Artist Name for Artist001";
72 ok $artist2->name eq '002Second Artist', "Got Expected Artist Name for Artist002";
73 ok $artist3->name eq '003Third Artist', "Got Expected Artist Name for Artist003";
7c325d53 74 ok !defined $undef->name, "Got Expected Artist Name for Artist004";
8b93a938 75
76 ok $artist1->cds->count eq 3, "Got Right number of CDs for Artist1";
77 ok $artist2->cds->count eq 0, "Got Right number of CDs for Artist2";
78 ok $artist3->cds->count eq 1, "Got Right number of CDs for Artist3";
7c325d53 79 ok $undef->cds->count eq 1, "Got Right number of CDs for Artist4";
8b93a938 80
81 ARTIST1CDS: {
82
83 my ($cd1, $cd2, $cd3) = $artist1->cds->search(undef, {order_by=>'year ASC'});
84
85 isa_ok $cd1, 'DBICTest::CD';
86 isa_ok $cd2, 'DBICTest::CD';
87 isa_ok $cd3, 'DBICTest::CD';
88
89 ok $cd1->year == 2000;
90 ok $cd2->year == 2001;
91 ok $cd3->year == 2002;
92
93 ok $cd1->title eq '001Title1';
94 ok $cd2->title eq '001Title2';
95 ok $cd3->title eq '001Title3';
96 }
97
98 ARTIST3CDS: {
99
100 my ($cd1) = $artist3->cds->search(undef, {order_by=>'year ASC'});
101
102 isa_ok $cd1, 'DBICTest::CD';
103
104 ok $cd1->year == 2005;
105 ok $cd1->title eq '003Title1';
7c325d53 106 }
107
108 ARTIST4CDS: {
109
110 my ($cd1) = $undef->cds->search(undef, {order_by=>'year ASC'});
111
112 isa_ok $cd1, 'DBICTest::CD';
113
114 ok $cd1->year == 2010;
115 ok $cd1->title eq '004Title1';
116 }
117
118 ## Need to do some cleanup so that later tests don't get borked
119
120 $undef->delete;
8b93a938 121}
122
123
124## ----------------------------------------------------------------------------
e8aef98d 125## Array context tests
126## ----------------------------------------------------------------------------
127
128ARRAY_CONTEXT: {
129
130 ## These first set of tests are cake because array context just delegates
131 ## all it's processing to $resultset->create
71d496fe 132
e8aef98d 133 HAS_MANY_NO_PKS: {
71d496fe 134
e8aef98d 135 ## This first group of tests checks to make sure we can call populate
136 ## with the parent having many children and let the keys be automatic
137
138 my $artists = [
139 {
140 name => 'Angsty-Whiny Girl',
141 cds => [
142 { title => 'My First CD', year => 2006 },
143 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
144 ],
145 },
146 {
147 name => 'Manufactured Crap',
148 },
149 {
150 name => 'Like I Give a Damn',
151 cds => [
152 { title => 'My parents sold me to a record company' ,year => 2005 },
153 { title => 'Why Am I So Ugly?', year => 2006 },
154 { title => 'I Got Surgery and am now Popular', year => 2007 }
155 ],
156 },
157 {
158 name => 'Formerly Named',
159 cds => [
160 { title => 'One Hit Wonder', year => 2006 },
161 ],
162 },
163 ];
6f8ab6dc 164
e8aef98d 165 ## Get the result row objects.
166
167 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
168
169 ## Do we have the right object?
170
171 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
172 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
173 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
174 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
175
176 ## Find the expected information?
177
178 ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
179 ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
180 ok( $damn->name eq 'Like I Give a Damn', "Got Correct name for result object");
181 ok( $formerly->name eq 'Formerly Named', "Got Correct name for result object");
182
183 ## Create the expected children sub objects?
184
185 ok( $crap->cds->count == 0, "got Expected Number of Cds");
186 ok( $girl->cds->count == 2, "got Expected Number of Cds");
187 ok( $damn->cds->count == 3, "got Expected Number of Cds");
188 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
71d496fe 189
e8aef98d 190 ## Did the cds get expected information?
191
192 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
193
194 ok( $cd1->title eq "My First CD", "Got Expected CD Title");
195 ok( $cd2->title eq "Yet More Tweeny-Pop crap", "Got Expected CD Title");
196 }
7f997467 197
e8aef98d 198 HAS_MANY_WITH_PKS: {
199
200 ## This group tests the ability to specify the PK in the parent and let
201 ## DBIC transparently pass the PK down to the Child and also let's the
202 ## child create any other needed PK's for itself.
203
204 my $aid = $art_rs->get_column('artistid')->max || 0;
205
206 my $first_aid = ++$aid;
207
208 my $artists = [
209 {
210 artistid => $first_aid,
211 name => 'PK_Angsty-Whiny Girl',
212 cds => [
213 { artist => $first_aid, title => 'PK_My First CD', year => 2006 },
214 { artist => $first_aid, title => 'PK_Yet More Tweeny-Pop crap', year => 2007 },
215 ],
216 },
217 {
218 artistid => ++$aid,
219 name => 'PK_Manufactured Crap',
220 },
221 {
222 artistid => ++$aid,
223 name => 'PK_Like I Give a Damn',
224 cds => [
225 { title => 'PK_My parents sold me to a record company' ,year => 2005 },
226 { title => 'PK_Why Am I So Ugly?', year => 2006 },
227 { title => 'PK_I Got Surgery and am now Popular', year => 2007 }
228 ],
229 },
230 {
231 artistid => ++$aid,
232 name => 'PK_Formerly Named',
233 cds => [
234 { title => 'PK_One Hit Wonder', year => 2006 },
235 ],
236 },
237 ];
238
239 ## Get the result row objects.
240
241 my ($girl, $crap, $damn, $formerly) = $art_rs->populate($artists);
242
243 ## Do we have the right object?
244
245 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
246 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
247 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
248 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
249
250 ## Find the expected information?
71d496fe 251
e8aef98d 252 ok( $crap->name eq 'PK_Manufactured Crap', "Got Correct name for result object");
253 ok( $girl->name eq 'PK_Angsty-Whiny Girl', "Got Correct name for result object");
254 ok( $girl->artistid == $first_aid, "Got Correct artist PK for result object");
255 ok( $damn->name eq 'PK_Like I Give a Damn', "Got Correct name for result object");
256 ok( $formerly->name eq 'PK_Formerly Named', "Got Correct name for result object");
257
258 ## Create the expected children sub objects?
259
260 ok( $crap->cds->count == 0, "got Expected Number of Cds");
261 ok( $girl->cds->count == 2, "got Expected Number of Cds");
262 ok( $damn->cds->count == 3, "got Expected Number of Cds");
263 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
71d496fe 264
e8aef98d 265 ## Did the cds get expected information?
266
267 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
268
269 ok( $cd1->title eq "PK_My First CD", "Got Expected CD Title");
270 ok( $cd2->title eq "PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
271 }
272
273 BELONGS_TO_NO_PKs: {
71d496fe 274
e8aef98d 275 ## Test from a belongs_to perspective, should create artist first,
276 ## then CD with artistid. This test we let the system automatically
277 ## create the PK's. Chances are good you'll use it this way mostly.
278
279 my $cds = [
280 {
281 title => 'Some CD3',
282 year => '1997',
283 artist => { name => 'Fred BloggsC'},
284 },
285 {
286 title => 'Some CD4',
287 year => '1997',
288 artist => { name => 'Fred BloggsD'},
289 },
290 ];
291
292 my ($cdA, $cdB) = $cd_rs->populate($cds);
293
71d496fe 294
e8aef98d 295 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
296 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
297 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
71d496fe 298
e8aef98d 299
300 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
301 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
302 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
303 }
71d496fe 304
e8aef98d 305 BELONGS_TO_WITH_PKs: {
71d496fe 306
e8aef98d 307 ## Test from a belongs_to perspective, should create artist first,
308 ## then CD with artistid. This time we try setting the PK's
309
310 my $aid = $art_rs->get_column('artistid')->max || 0;
311
312 my $cds = [
313 {
314 title => 'Some CD3',
315 year => '1997',
316 artist => { artistid=> ++$aid, name => 'Fred BloggsC'},
317 },
318 {
319 title => 'Some CD4',
320 year => '1997',
321 artist => { artistid=> ++$aid, name => 'Fred BloggsD'},
322 },
323 ];
324
325 my ($cdA, $cdB) = $cd_rs->populate($cds);
326
327 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
328 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
329 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
330
331 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
332 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
333 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
334 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
335 }
336}
71d496fe 337
71d496fe 338
e8aef98d 339## ----------------------------------------------------------------------------
340## Void context tests
341## ----------------------------------------------------------------------------
71d496fe 342
e8aef98d 343VOID_CONTEXT: {
71d496fe 344
e8aef98d 345 ## All these tests check the ability to use populate without asking for
346 ## any returned resultsets. This uses bulk_insert as much as possible
347 ## in order to increase speed.
e287d9b0 348
e8aef98d 349 HAS_MANY_WITH_PKS: {
e287d9b0 350
e8aef98d 351 ## This first group of tests checks to make sure we can call populate
352 ## with the parent having many children and the parent PK is set
81ab7888 353
e8aef98d 354 my $aid = $art_rs->get_column('artistid')->max || 0;
355
356 my $first_aid = ++$aid;
357
358 my $artists = [
359 {
360 artistid => $first_aid,
361 name => 'VOID_PK_Angsty-Whiny Girl',
362 cds => [
363 { artist => $first_aid, title => 'VOID_PK_My First CD', year => 2006 },
364 { artist => $first_aid, title => 'VOID_PK_Yet More Tweeny-Pop crap', year => 2007 },
365 ],
366 },
367 {
368 artistid => ++$aid,
369 name => 'VOID_PK_Manufactured Crap',
370 },
371 {
372 artistid => ++$aid,
373 name => 'VOID_PK_Like I Give a Damn',
374 cds => [
375 { title => 'VOID_PK_My parents sold me to a record company' ,year => 2005 },
376 { title => 'VOID_PK_Why Am I So Ugly?', year => 2006 },
377 { title => 'VOID_PK_I Got Surgery and am now Popular', year => 2007 }
378 ],
379 },
380 {
381 artistid => ++$aid,
382 name => 'VOID_PK_Formerly Named',
383 cds => [
384 { title => 'VOID_PK_One Hit Wonder', year => 2006 },
385 ],
7c325d53 386 },
387 {
388 artistid => ++$aid,
389 name => undef,
390 cds => [
391 { title => 'VOID_PK_Zundef test', year => 2006 },
392 ],
393 },
e8aef98d 394 ];
395
396 ## Get the result row objects.
397
398 $art_rs->populate($artists);
399
7c325d53 400 my ($undef, $girl, $formerly, $damn, $crap) = $art_rs->search(
401
402 {name=>[ map { $_->{name} } @$artists]},
e8aef98d 403 {order_by=>'name ASC'},
404 );
405
406 ## Do we have the right object?
407
408 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
409 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
410 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
411 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
7c325d53 412 isa_ok( $undef, 'DBICTest::Artist', "Got 'Artist'");
413
e8aef98d 414 ## Find the expected information?
415
7c325d53 416 ok( $crap->name eq 'VOID_PK_Manufactured Crap', "Got Correct name 'VOID_PK_Manufactured Crap' for result object");
e8aef98d 417 ok( $girl->name eq 'VOID_PK_Angsty-Whiny Girl', "Got Correct name for result object");
418 ok( $damn->name eq 'VOID_PK_Like I Give a Damn', "Got Correct name for result object");
419 ok( $formerly->name eq 'VOID_PK_Formerly Named', "Got Correct name for result object");
7c325d53 420 ok( !defined $undef->name, "Got Correct name 'is undef' for result object");
e8aef98d 421
422 ## Create the expected children sub objects?
735ba30e 423 ok( $crap->can('cds'), "Has cds relationship");
424 ok( $girl->can('cds'), "Has cds relationship");
425 ok( $damn->can('cds'), "Has cds relationship");
426 ok( $formerly->can('cds'), "Has cds relationship");
7c325d53 427 ok( $undef->can('cds'), "Has cds relationship");
428
e8aef98d 429 ok( $crap->cds->count == 0, "got Expected Number of Cds");
430 ok( $girl->cds->count == 2, "got Expected Number of Cds");
431 ok( $damn->cds->count == 3, "got Expected Number of Cds");
432 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
7c325d53 433 ok( $undef->cds->count == 1, "got Expected Number of Cds");
434
e8aef98d 435 ## Did the cds get expected information?
436
437 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
438
439 ok( $cd1->title eq "VOID_PK_My First CD", "Got Expected CD Title");
440 ok( $cd2->title eq "VOID_PK_Yet More Tweeny-Pop crap", "Got Expected CD Title");
441 }
6f8ab6dc 442
6f8ab6dc 443
e8aef98d 444 BELONGS_TO_WITH_PKs: {
6f8ab6dc 445
e8aef98d 446 ## Test from a belongs_to perspective, should create artist first,
447 ## then CD with artistid. This time we try setting the PK's
448
449 my $aid = $art_rs->get_column('artistid')->max || 0;
450
451 my $cds = [
452 {
453 title => 'Some CD3B',
454 year => '1997',
455 artist => { artistid=> ++$aid, name => 'Fred BloggsCB'},
456 },
457 {
458 title => 'Some CD4B',
459 year => '1997',
460 artist => { artistid=> ++$aid, name => 'Fred BloggsDB'},
461 },
462 ];
463
464 $cd_rs->populate($cds);
465
466 my ($cdA, $cdB) = $cd_rs->search(
467 {title=>[sort map {$_->{title}} @$cds]},
468 {order_by=>'title ASC'},
469 );
470
471 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
472 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
473 is($cdA->artist->name, 'Fred BloggsCB', 'Set Artist to FredCB');
474
475 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
476 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
477 is($cdB->artist->name, 'Fred BloggsDB', 'Set Artist to FredDB');
478 ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
479 }
d21161f5 480
e8aef98d 481 BELONGS_TO_NO_PKs: {
482
483 ## Test from a belongs_to perspective, should create artist first,
484 ## then CD with artistid.
9283388a 485
e8aef98d 486 my $cds = [
487 {
488 title => 'Some CD3BB',
489 year => '1997',
490 artist => { name => 'Fred BloggsCBB'},
491 },
492 {
493 title => 'Some CD4BB',
494 year => '1997',
495 artist => { name => 'Fred BloggsDBB'},
7c325d53 496 },
497 {
498 title => 'Some CD5BB',
499 year => '1997',
500 artist => { name => undef},
e8aef98d 501 },
502 ];
503
504 $cd_rs->populate($cds);
505
7c325d53 506 my ($cdA, $cdB, $cdC) = $cd_rs->search(
e8aef98d 507 {title=>[sort map {$_->{title}} @$cds]},
508 {order_by=>'title ASC'},
509 );
510
511 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
512 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
513 is($cdA->title, 'Some CD3BB', 'Found Expected title');
514 is($cdA->artist->name, 'Fred BloggsCBB', 'Set Artist to FredCBB');
515
516 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
517 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
518 is($cdB->title, 'Some CD4BB', 'Found Expected title');
519 is($cdB->artist->name, 'Fred BloggsDBB', 'Set Artist to FredDBB');
7c325d53 520
521 isa_ok($cdC, 'DBICTest::CD', 'Created CD');
522 isa_ok($cdC->artist, 'DBICTest::Artist', 'Set Artist');
523 is($cdC->title, 'Some CD5BB', 'Found Expected title');
524 is( $cdC->artist->name, undef, 'Set Artist to something undefined');
e8aef98d 525 }
d21161f5 526
d21161f5 527
e8aef98d 528 HAS_MANY_NO_PKS: {
d21161f5 529
e8aef98d 530 ## This first group of tests checks to make sure we can call populate
531 ## with the parent having many children and let the keys be automatic
e8aef98d 532
533 my $artists = [
534 {
535 name => 'VOID_Angsty-Whiny Girl',
536 cds => [
537 { title => 'VOID_My First CD', year => 2006 },
538 { title => 'VOID_Yet More Tweeny-Pop crap', year => 2007 },
539 ],
540 },
541 {
542 name => 'VOID_Manufactured Crap',
543 },
544 {
545 name => 'VOID_Like I Give a Damn',
546 cds => [
547 { title => 'VOID_My parents sold me to a record company' ,year => 2005 },
548 { title => 'VOID_Why Am I So Ugly?', year => 2006 },
549 { title => 'VOID_I Got Surgery and am now Popular', year => 2007 }
550 ],
551 },
552 {
553 name => 'VOID_Formerly Named',
554 cds => [
555 { title => 'VOID_One Hit Wonder', year => 2006 },
556 ],
557 },
558 ];
559
560 ## Get the result row objects.
561
562 $art_rs->populate($artists);
563
564 my ($girl, $formerly, $damn, $crap) = $art_rs->search(
565 {name=>[sort map {$_->{name}} @$artists]},
566 {order_by=>'name ASC'},
567 );
568
569 ## Do we have the right object?
570
571 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
572 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
573 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
574 isa_ok( $formerly, 'DBICTest::Artist', "Got 'Artist'");
575
576 ## Find the expected information?
d21161f5 577
e8aef98d 578 ok( $crap->name eq 'VOID_Manufactured Crap', "Got Correct name for result object");
579 ok( $girl->name eq 'VOID_Angsty-Whiny Girl', "Got Correct name for result object");
580 ok( $damn->name eq 'VOID_Like I Give a Damn', "Got Correct name for result object");
581 ok( $formerly->name eq 'VOID_Formerly Named', "Got Correct name for result object");
582
583 ## Create the expected children sub objects?
735ba30e 584 ok( $crap->can('cds'), "Has cds relationship");
585 ok( $girl->can('cds'), "Has cds relationship");
586 ok( $damn->can('cds'), "Has cds relationship");
587 ok( $formerly->can('cds'), "Has cds relationship");
e8aef98d 588
589 ok( $crap->cds->count == 0, "got Expected Number of Cds");
590 ok( $girl->cds->count == 2, "got Expected Number of Cds");
591 ok( $damn->cds->count == 3, "got Expected Number of Cds");
592 ok( $formerly->cds->count == 1, "got Expected Number of Cds");
593
594 ## Did the cds get expected information?
595
596 my ($cd1, $cd2) = $girl->cds->search({},{order_by=>'year ASC'});
735ba30e 597
598 ok($cd1, "Got a got CD");
599 ok($cd2, "Got a got CD");
9283388a 600 ok( $cd1->title eq "VOID_My First CD", "Got Expected CD Title");
601 ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
e8aef98d 602 }
d21161f5 603
9283388a 604}