A couple fixes to Ordered
[dbsrgits/DBIx-Class-Historic.git] / t / 96multi_create.t
CommitLineData
33dd4e80 1use strict;
af2d42c0 2use warnings;
33dd4e80 3
04ec3909 4use Test::More;
fe8cee09 5use Test::Exception;
33dd4e80 6use lib qw(t/lib);
7use DBICTest;
8
6e7025fb 9sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} };
10
9ffcb5b8 11plan tests => 77;
04ec3909 12
33dd4e80 13my $schema = DBICTest->init_schema();
14
6e7025fb 15mc_diag '* simple create + parent (the stuff $rs belongs_to)';
04ec3909 16eval {
c45f4e10 17 my $cd = $schema->resultset('CD')->create({
04ec3909 18 artist => {
19 name => 'Fred Bloggs'
20 },
21 title => 'Some CD',
22 year => 1996
23 });
24
c45f4e10 25 isa_ok($cd, 'DBICTest::CD', 'Created CD object');
26 isa_ok($cd->artist, 'DBICTest::Artist', 'Created related Artist');
27 is($cd->artist->name, 'Fred Bloggs', 'Artist created correctly');
28};
6e7025fb 29mc_diag $@ if $@;
c45f4e10 30
6e7025fb 31mc_diag '* same as above but the child and parent have no values, except for an explicit parent pk';
c45f4e10 32eval {
33 my $bm_rs = $schema->resultset('Bookmark');
34 my $bookmark = $bm_rs->create({
35 link => {
36 id => 66,
37 },
38 });
39
40 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
41 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
42 is (
43 $bm_rs->search (
44 { 'link.title' => $bookmark->link->title },
45 { join => 'link' },
46 )->count,
47 1,
48 'Bookmark and link made it to the DB',
49 );
04ec3909 50};
6e7025fb 51mc_diag $@ if $@;
04ec3909 52
6e7025fb 53mc_diag '* create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )';
04ec3909 54eval {
ab4b097b 55 my $artist = $schema->resultset('Artist')->first;
56 my $cd = $artist->create_related (cds => {
57 title => 'Music to code by',
58 year => 2007,
59 tags => [
60 { 'tag' => 'rock' },
61 ],
62 });
04ec3909 63
ab4b097b 64 isa_ok($cd, 'DBICTest::CD', 'Created CD');
65 is($cd->title, 'Music to code by', 'CD created correctly');
66 is($cd->tags->count, 1, 'One tag created for CD');
67 is($cd->tags->first->tag, 'rock', 'Tag created correctly');
04ec3909 68
04ec3909 69};
6e7025fb 70mc_diag $@ if $@;
04ec3909 71
fe8cee09 72throws_ok (
73 sub {
74 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
75 $schema->resultset('Artist')->first->update({
76 cds => [
77 { title => 'Yet another CD',
78 year => 2006,
79 },
80 ],
81 });
82 },
83 qr/Recursive update is not supported over relationships of type multi/,
84 'create via update of multi relationships throws an exception'
85);
86
6e7025fb 87mc_diag '* Create m2m while originating in the linker table';
ab4b097b 88eval {
89 my $artist = $schema->resultset('Artist')->first;
90 my $c2p = $schema->resultset('CD_to_Producer')->create ({
91 cd => {
92 artist => $artist,
93 title => 'Bad investment',
94 year => 2008,
95 tracks => [
1ceafb0c 96 { title => 'Just buy' },
97 { title => 'Why did we do it' },
98 { title => 'Burn baby burn' },
ab4b097b 99 ],
100 },
101 producer => {
102 name => 'Lehman Bros.',
103 },
104 });
105
106 isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created');
107 my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' });
108 isa_ok ($prod, 'DBICTest::Producer', 'Producer row found');
109 is ($prod->cds->count, 1, 'Producer has one production');
110 my $cd = $prod->cds->first;
111 is ($cd->title, 'Bad investment', 'CD created correctly');
112 is ($cd->tracks->count, 3, 'CD has 3 tracks');
113
114};
6e7025fb 115mc_diag $@ if $@;
ab4b097b 116
6e7025fb 117mc_diag (<<'DG');
9c1d9e6e 118* Create over > 1 levels of might_have with multiple has_many and multiple m2m
119but starting at a has_many level
120
121CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
122 \
123 \-> has_many \
124 --> CD2Producer
125 /-> has_many /
126 /
127 Producer
128DG
129
51f30f72 130eval {
131 my $artist = $schema->resultset('Artist')->first;
132 my $cd = $schema->resultset('CD')->create ({
133 artist => $artist,
134 title => 'Music to code by at night',
135 year => 2008,
136 tracks => [
137 {
51f30f72 138 title => 'Off by one again',
139 },
140 {
51f30f72 141 title => 'The dereferencer',
142 cd_single => {
143 artist => $artist,
144 year => 2008,
145 title => 'Was that a null (Single)',
146 tracks => [
1ceafb0c 147 { title => 'The dereferencer' },
148 { title => 'The dereferencer II' },
51f30f72 149 ],
150 cd_to_producer => [
151 {
152 producer => {
153 name => 'K&R',
154 }
43483f30 155 },
156 {
157 producer => {
158 name => 'Don Knuth',
159 }
160 },
51f30f72 161 ]
162 },
163 },
164 ],
165 });
166
167 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
168 is ($cd->title, 'Music to code by at night', 'Correct CD title');
169 is ($cd->tracks->count, 2, 'Two tracks on main CD');
170
171 my ($t1, $t2) = $cd->tracks->all;
172 is ($t1->title, 'Off by one again', 'Correct 1st track name');
173 is ($t1->cd_single, undef, 'No single for 1st track');
174 is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
175 isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
176
177 my $single = $t2->cd_single;
178 is ($single->tracks->count, 2, 'Two tracks on single CD');
179 is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
180 is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
181
43483f30 182 is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
183 is_deeply (
184 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
185 ['Don Knuth', 'K&R'],
186 'Producers named correctly',
187 );
51f30f72 188};
6e7025fb 189mc_diag $@ if $@;
51f30f72 190
6e7025fb 191mc_diag (<<'DG');
9c1d9e6e 192* Same as above but starting at the might_have directly
193
194Track -> might have -> Single -> has_many -> Tracks
195 \
196 \-> has_many \
197 --> CD2Producer
198 /-> has_many /
199 /
200 Producer
201DG
202
43483f30 203eval {
204 my $cd = $schema->resultset('CD')->first;
205 my $track = $schema->resultset('Track')->create ({
206 cd => $cd,
43483f30 207 title => 'Multicreate rocks',
208 cd_single => {
209 artist => $cd->artist,
210 year => 2008,
211 title => 'Disemboweling MultiCreate',
212 tracks => [
1ceafb0c 213 { title => 'Why does mst write this way' },
214 { title => 'Chainsaw celebration' },
215 { title => 'Purl cleans up' },
43483f30 216 ],
217 cd_to_producer => [
218 {
219 producer => {
220 name => 'mst',
221 }
222 },
223 {
224 producer => {
225 name => 'castaway',
226 }
227 },
228 {
229 producer => {
230 name => 'theorbtwo',
231 }
232 },
233 ]
234 },
235 });
236
237 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
238 is ($track->title, 'Multicreate rocks', 'Correct Track title');
239
240 my $single = $track->cd_single;
241 isa_ok ($single, 'DBICTest::CD', 'Created a single with the track');
242 is ($single->tracks->count, 3, '3 tracks on single CD');
243 is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title');
244 is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title');
245 is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title');
246
247 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
248 is_deeply (
249 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
250 ['castaway', 'mst', 'theorbtwo'],
251 'Producers named correctly',
252 );
51f30f72 253};
6e7025fb 254mc_diag $@ if $@;
51f30f72 255
6e7025fb 256mc_diag '* Test might_have again but with a PK == FK in the middle (obviously not specified)';
0c9b6ece 257eval {
258 my $artist = $schema->resultset('Artist')->first;
259 my $cd = $schema->resultset('CD')->create ({
260 artist => $artist,
261 title => 'Music to code by at twilight',
262 year => 2008,
263 artwork => {
264 images => [
265 { name => 'recursive descent' },
266 { name => 'tail packing' },
267 ],
268 },
269 });
270
271 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
272 is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
273 isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
274
275 # this test might look weird, but it failed at one point, keep it there
e5b05576 276 my $art_obj = $cd->artwork;
277 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
278 is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
0c9b6ece 279 is_deeply (
e5b05576 280 [ sort $art_obj->images->get_column ('name')->all ],
0c9b6ece 281 [ 'recursive descent', 'tail packing' ],
282 'Images named correctly in objects',
283 );
284
0c9b6ece 285 my $artwork = $schema->resultset('Artwork')->search (
286 { 'cd.title' => 'Music to code by at twilight' },
287 { join => 'cd' },
288 )->single;
289
290 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
291
292 is_deeply (
293 [ sort $artwork->images->get_column ('name')->all ],
294 [ 'recursive descent', 'tail packing' ],
295 'Images named correctly after search',
296 );
297};
6e7025fb 298mc_diag $@ if $@;
0c9b6ece 299
6e7025fb 300mc_diag '* Test might_have again but with just a PK and FK (neither specified) in the mid-table';
0c9b6ece 301eval {
302 my $cd = $schema->resultset('CD')->first;
303 my $track = $schema->resultset ('Track')->create ({
304 cd => $cd,
0c9b6ece 305 title => 'Black',
306 lyrics => {
307 lyric_versions => [
308 { text => 'The color black' },
309 { text => 'The colour black' },
310 ],
311 },
312 });
313
314 isa_ok ($track, 'DBICTest::Track', 'Main track object created');
315 is ($track->title, 'Black', 'Correct track title');
316 isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
317
318 # this test might look weird, but it was failing at one point, keep it there
e5b05576 319 my $lyric_obj = $track->lyrics;
320 ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
321 ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
322 is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
0c9b6ece 323 is_deeply (
e5b05576 324 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
0c9b6ece 325 [ 'The color black', 'The colour black' ],
326 'Lyrics text in objects matches',
327 );
328
329
330 my $lyric = $schema->resultset('Lyrics')->search (
331 { 'track.title' => 'Black' },
332 { join => 'track' },
333 )->single;
334
335 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
336
337 is_deeply (
338 [ sort $lyric->lyric_versions->get_column ('text')->all ],
339 [ 'The color black', 'The colour black' ],
340 'Lyrics text via search matches',
341 );
342};
6e7025fb 343mc_diag $@ if $@;
0c9b6ece 344
6e7025fb 345mc_diag (<<'DG');
9c1d9e6e 346* Test a multilevel might-have with a PK == FK in the might_have/has_many table
347
348CD -> might have -> Artwork
349 \
350 \-> has_many \
351 --> Artwork_to_Artist
352 /-> has_many /
353 /
354 Artist
355DG
356
3424e5e8 357eval {
358 my $someartist = $schema->resultset('Artist')->first;
359 my $cd = $schema->resultset('CD')->create ({
360 artist => $someartist,
361 title => 'Music to code by until the cows come home',
362 year => 2008,
363 artwork => {
364 artwork_to_artist => [
365 { artist => { name => 'cowboy joe' } },
366 { artist => { name => 'billy the kid' } },
367 ],
368 },
369 });
370
371 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
372 is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title');
373
374 my $art_obj = $cd->artwork;
375 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
376 is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object');
377 is_deeply (
378 [ sort $art_obj->artists->get_column ('name')->all ],
379 [ 'billy the kid', 'cowboy joe' ],
380 'Artists named correctly when queried via object',
381 );
382
383 my $artwork = $schema->resultset('Artwork')->search (
384 { 'cd.title' => 'Music to code by until the cows come home' },
385 { join => 'cd' },
386 )->single;
387 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
388 is_deeply (
389 [ sort $artwork->artists->get_column ('name')->all ],
390 [ 'billy the kid', 'cowboy joe' ],
391 'Artists named correctly queried via a new search',
392 );
393};
6e7025fb 394mc_diag $@ if $@;
3424e5e8 395
6e7025fb 396mc_diag '* Nested find_or_create';
04ec3909 397eval {
398 my $newartist2 = $schema->resultset('Artist')->find_or_create({
399 name => 'Fred 3',
400 cds => [
401 {
402 title => 'Noah Act',
403 year => 2007,
404 },
405 ],
406 });
407 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
408};
6e7025fb 409mc_diag $@ if $@;
04ec3909 410
6e7025fb 411mc_diag '* Multiple same level has_many create';
04ec3909 412eval {
413 my $artist2 = $schema->resultset('Artist')->create({
51f30f72 414 name => 'Fred 4',
04ec3909 415 cds => [
416 {
417 title => 'Music to code by',
418 year => 2007,
419 },
420 ],
421 cds_unordered => [
422 {
423 title => 'Music to code by',
424 year => 2007,
425 },
426 ]
427 });
428
429 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
430};
6e7025fb 431mc_diag $@ if $@;
3d8ee6ab 432
6e7025fb 433mc_diag '* First create_related pass';
04ec3909 434eval {
3d8ee6ab 435 my $artist = $schema->resultset('Artist')->first;
436
437 my $cd_result = $artist->create_related('cds', {
438
439 title => 'TestOneCD1',
440 year => 2007,
441 tracks => [
1ceafb0c 442 { title => 'TrackOne' },
443 { title => 'TrackTwo' },
3d8ee6ab 444 ],
445
446 });
447
448 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
449 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
450
451 my $tracks = $cd_result->tracks;
452
453 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
454
455 foreach my $track ($tracks->all)
456 {
457 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
458 }
04ec3909 459};
6e7025fb 460mc_diag $@ if $@;
3d8ee6ab 461
6e7025fb 462mc_diag '* second create_related with same arguments';
04ec3909 463eval {
2ec8e594 464 my $artist = $schema->resultset('Artist')->first;
3d8ee6ab 465
2ec8e594 466 my $cd_result = $artist->create_related('cds', {
3d8ee6ab 467
2ec8e594 468 title => 'TestOneCD2',
3d8ee6ab 469 year => 2007,
470 tracks => [
1ceafb0c 471 { title => 'TrackOne' },
472 { title => 'TrackTwo' },
3d8ee6ab 473 ],
474
9c6d6d93 475 liner_notes => { notes => 'I can haz liner notes?' },
476
3d8ee6ab 477 });
478
479 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
2ec8e594 480 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
9c6d6d93 481 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
3d8ee6ab 482
483 my $tracks = $cd_result->tracks;
484
485 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
486
487 foreach my $track ($tracks->all)
488 {
489 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
490 }
04ec3909 491};
6e7025fb 492mc_diag $@ if $@;
e5dddc05 493
6e7025fb 494mc_diag '* create of parents of a record linker table';
04ec3909 495eval {
496 my $cdp = $schema->resultset('CD_to_Producer')->create({
497 cd => { artist => 1, title => 'foo', year => 2000 },
498 producer => { name => 'jorge' }
499 });
500 ok($cdp, 'join table record created ok');
501};
6e7025fb 502mc_diag $@ if $@;
2bc3c81e 503
04ec3909 504eval {
2bc3c81e 505 my $kurt_cobain = { name => 'Kurt Cobain' };
506
507 my $in_utero = $schema->resultset('CD')->new({
508 title => 'In Utero',
509 year => 1993
510 });
511
512 $kurt_cobain->{cds} = [ $in_utero ];
513
514
515 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
516 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
517
518 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
519 is($a->cds && $a->cds->first && $a->cds->first->title,
520 'In Utero', 'CD insertion ok');
04ec3909 521};
6e7025fb 522mc_diag $@ if $@;
2bc3c81e 523
9ffcb5b8 524=pod
525# This test case has been moved to t/96multi_create/cd_single.t
04ec3909 526eval {
2bc3c81e 527 my $pink_floyd = { name => 'Pink Floyd' };
528
529 my $the_wall = { title => 'The Wall', year => 1979 };
530
531 $pink_floyd->{cds} = [ $the_wall ];
532
533
534 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
535 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
536
537 is($a->name, 'Pink Floyd', 'Artist insertion ok');
538 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
04ec3909 539};
6e7025fb 540mc_diag $@ if $@;
9ffcb5b8 541=cut
e02b9964 542
6e7025fb 543mc_diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
f847f84d 544## Create foreign key col obj including PK
545## See test 20 in 66relationships.t
04ec3909 546eval {
547 my $new_cd_hashref = {
548 cdid => 27,
549 title => 'Boogie Woogie',
550 year => '2007',
551 artist => { artistid => 17, name => 'king luke' }
552 };
e02b9964 553
04ec3909 554 my $cd = $schema->resultset("CD")->find(1);
f10ac17d 555
04ec3909 556 is($cd->artist->id, 1, 'rel okay');
f10ac17d 557
04ec3909 558 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
559 is($new_cd->artist->id, 17, 'new id retained okay');
560};
6e7025fb 561mc_diag $@ if $@;
6ede9177 562
563eval {
564 $schema->resultset("CD")->create({
38c03c20 565 cdid => 28,
370f2ba2 566 title => 'Boogie Wiggle',
38c03c20 567 year => '2007',
568 artist => { artistid => 18, name => 'larry' }
6ede9177 569 });
38c03c20 570};
6ede9177 571is($@, '', 'new cd created without clash on related artist');
38c03c20 572
6e7025fb 573mc_diag '* Make sure exceptions from errors in created rels propogate';
f10ac17d 574eval {
370f2ba2 575 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
576 #$t->cd($t->new_related('cd', { artist => undef } ) );
577 #$t->{_rel_in_storage} = 0;
f10ac17d 578 $t->insert;
579};
580like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
bbbf67eb 581
6e7025fb 582mc_diag '* Test multi create over many_to_many';
04ec3909 583eval {
584 $schema->resultset('CD')->create ({
585 artist => {
586 name => 'larry', # should already exist
587 },
76b8cf98 588 title => 'Warble Marble',
589 year => '2009',
590 cd_to_producer => [
04ec3909 591 { producer => { name => 'Cowboy Neal' } },
76b8cf98 592 ],
04ec3909 593 });
76b8cf98 594
04ec3909 595 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
596 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
597 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
598 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
599};
76b8cf98 600
ab4b097b 6011;