add test for IDENTITY_INSERT
[dbsrgits/DBIx-Class.git] / t / multi_create / standard.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
22b3008f 9plan tests => 91;
04ec3909 10
33dd4e80 11my $schema = DBICTest->init_schema();
12
f8a464e5 13lives_ok ( sub {
c45f4e10 14 my $cd = $schema->resultset('CD')->create({
04ec3909 15 artist => {
16 name => 'Fred Bloggs'
17 },
18 title => 'Some CD',
19 year => 1996
20 });
21
c45f4e10 22 isa_ok($cd, 'DBICTest::CD', 'Created CD object');
23 isa_ok($cd->artist, 'DBICTest::Artist', 'Created related Artist');
24 is($cd->artist->name, 'Fred Bloggs', 'Artist created correctly');
f8a464e5 25}, 'simple create + parent (the stuff $rs belongs_to) ok');
c45f4e10 26
f8a464e5 27lives_ok ( sub {
c45f4e10 28 my $bm_rs = $schema->resultset('Bookmark');
29 my $bookmark = $bm_rs->create({
30 link => {
31 id => 66,
32 },
33 });
34
35 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
36 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
37 is (
38 $bm_rs->search (
39 { 'link.title' => $bookmark->link->title },
40 { join => 'link' },
41 )->count,
42 1,
43 'Bookmark and link made it to the DB',
44 );
f8a464e5 45}, 'simple create where the child and parent have no values, except for an explicit parent pk ok');
04ec3909 46
f8a464e5 47lives_ok ( sub {
ab4b097b 48 my $artist = $schema->resultset('Artist')->first;
49 my $cd = $artist->create_related (cds => {
50 title => 'Music to code by',
51 year => 2007,
52 tags => [
53 { 'tag' => 'rock' },
54 ],
55 });
04ec3909 56
ab4b097b 57 isa_ok($cd, 'DBICTest::CD', 'Created CD');
58 is($cd->title, 'Music to code by', 'CD created correctly');
59 is($cd->tags->count, 1, 'One tag created for CD');
60 is($cd->tags->first->tag, 'rock', 'Tag created correctly');
04ec3909 61
f8a464e5 62}, 'create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )');
04ec3909 63
fe8cee09 64throws_ok (
65 sub {
66 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
67 $schema->resultset('Artist')->first->update({
68 cds => [
69 { title => 'Yet another CD',
70 year => 2006,
71 },
72 ],
73 });
74 },
75 qr/Recursive update is not supported over relationships of type multi/,
76 'create via update of multi relationships throws an exception'
77);
78
f8a464e5 79lives_ok ( sub {
ab4b097b 80 my $artist = $schema->resultset('Artist')->first;
81 my $c2p = $schema->resultset('CD_to_Producer')->create ({
82 cd => {
83 artist => $artist,
84 title => 'Bad investment',
85 year => 2008,
86 tracks => [
1ceafb0c 87 { title => 'Just buy' },
88 { title => 'Why did we do it' },
89 { title => 'Burn baby burn' },
ab4b097b 90 ],
91 },
92 producer => {
93 name => 'Lehman Bros.',
94 },
95 });
96
97 isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created');
98 my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' });
99 isa_ok ($prod, 'DBICTest::Producer', 'Producer row found');
100 is ($prod->cds->count, 1, 'Producer has one production');
101 my $cd = $prod->cds->first;
102 is ($cd->title, 'Bad investment', 'CD created correctly');
103 is ($cd->tracks->count, 3, 'CD has 3 tracks');
f8a464e5 104}, 'Create m2m while originating in the linker table');
ab4b097b 105
9c1d9e6e 106
f8a464e5 107#CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
108# \
109# \-> has_many \
110# --> CD2Producer
111# /-> has_many /
112# /
113# Producer
114lives_ok ( sub {
51f30f72 115 my $artist = $schema->resultset('Artist')->first;
116 my $cd = $schema->resultset('CD')->create ({
117 artist => $artist,
118 title => 'Music to code by at night',
119 year => 2008,
120 tracks => [
121 {
51f30f72 122 title => 'Off by one again',
123 },
124 {
51f30f72 125 title => 'The dereferencer',
126 cd_single => {
127 artist => $artist,
128 year => 2008,
129 title => 'Was that a null (Single)',
130 tracks => [
1ceafb0c 131 { title => 'The dereferencer' },
132 { title => 'The dereferencer II' },
51f30f72 133 ],
134 cd_to_producer => [
135 {
136 producer => {
137 name => 'K&R',
138 }
43483f30 139 },
140 {
141 producer => {
142 name => 'Don Knuth',
143 }
144 },
51f30f72 145 ]
146 },
147 },
148 ],
149 });
150
151 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
152 is ($cd->title, 'Music to code by at night', 'Correct CD title');
153 is ($cd->tracks->count, 2, 'Two tracks on main CD');
154
155 my ($t1, $t2) = $cd->tracks->all;
156 is ($t1->title, 'Off by one again', 'Correct 1st track name');
157 is ($t1->cd_single, undef, 'No single for 1st track');
158 is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
159 isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
160
161 my $single = $t2->cd_single;
162 is ($single->tracks->count, 2, 'Two tracks on single CD');
163 is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
164 is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
165
43483f30 166 is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
167 is_deeply (
168 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
169 ['Don Knuth', 'K&R'],
170 'Producers named correctly',
171 );
f8a464e5 172}, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at a has_many level');
173
174#Track -> might have -> Single -> has_many -> Tracks
175# \
176# \-> has_many \
177# --> CD2Producer
178# /-> has_many /
179# /
180# Producer
181lives_ok ( sub {
43483f30 182 my $cd = $schema->resultset('CD')->first;
183 my $track = $schema->resultset('Track')->create ({
184 cd => $cd,
43483f30 185 title => 'Multicreate rocks',
186 cd_single => {
187 artist => $cd->artist,
188 year => 2008,
189 title => 'Disemboweling MultiCreate',
190 tracks => [
1ceafb0c 191 { title => 'Why does mst write this way' },
192 { title => 'Chainsaw celebration' },
193 { title => 'Purl cleans up' },
43483f30 194 ],
195 cd_to_producer => [
196 {
197 producer => {
198 name => 'mst',
199 }
200 },
201 {
202 producer => {
203 name => 'castaway',
204 }
205 },
206 {
207 producer => {
208 name => 'theorbtwo',
209 }
210 },
211 ]
212 },
213 });
214
215 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
216 is ($track->title, 'Multicreate rocks', 'Correct Track title');
217
218 my $single = $track->cd_single;
219 isa_ok ($single, 'DBICTest::CD', 'Created a single with the track');
220 is ($single->tracks->count, 3, '3 tracks on single CD');
221 is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title');
222 is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title');
223 is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title');
224
225 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
226 is_deeply (
227 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
228 ['castaway', 'mst', 'theorbtwo'],
229 'Producers named correctly',
230 );
f8a464e5 231}, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at the might_have directly');
51f30f72 232
f8a464e5 233lives_ok ( sub {
0c9b6ece 234 my $artist = $schema->resultset('Artist')->first;
235 my $cd = $schema->resultset('CD')->create ({
236 artist => $artist,
237 title => 'Music to code by at twilight',
238 year => 2008,
239 artwork => {
240 images => [
241 { name => 'recursive descent' },
242 { name => 'tail packing' },
243 ],
244 },
245 });
246
247 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
248 is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
249 isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
250
251 # this test might look weird, but it failed at one point, keep it there
e5b05576 252 my $art_obj = $cd->artwork;
253 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
254 is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
0c9b6ece 255 is_deeply (
e5b05576 256 [ sort $art_obj->images->get_column ('name')->all ],
0c9b6ece 257 [ 'recursive descent', 'tail packing' ],
258 'Images named correctly in objects',
259 );
260
0c9b6ece 261 my $artwork = $schema->resultset('Artwork')->search (
262 { 'cd.title' => 'Music to code by at twilight' },
263 { join => 'cd' },
264 )->single;
265
266 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
267
268 is_deeply (
269 [ sort $artwork->images->get_column ('name')->all ],
270 [ 'recursive descent', 'tail packing' ],
271 'Images named correctly after search',
272 );
f8a464e5 273}, 'Test might_have again but with a PK == FK in the middle (obviously not specified)');
0c9b6ece 274
f8a464e5 275lives_ok ( sub {
0c9b6ece 276 my $cd = $schema->resultset('CD')->first;
277 my $track = $schema->resultset ('Track')->create ({
278 cd => $cd,
0c9b6ece 279 title => 'Black',
280 lyrics => {
281 lyric_versions => [
282 { text => 'The color black' },
283 { text => 'The colour black' },
284 ],
285 },
286 });
287
288 isa_ok ($track, 'DBICTest::Track', 'Main track object created');
289 is ($track->title, 'Black', 'Correct track title');
290 isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
291
292 # this test might look weird, but it was failing at one point, keep it there
e5b05576 293 my $lyric_obj = $track->lyrics;
294 ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
295 ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
296 is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
0c9b6ece 297 is_deeply (
e5b05576 298 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
0c9b6ece 299 [ 'The color black', 'The colour black' ],
300 'Lyrics text in objects matches',
301 );
302
303
304 my $lyric = $schema->resultset('Lyrics')->search (
305 { 'track.title' => 'Black' },
306 { join => 'track' },
307 )->single;
308
309 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
310
311 is_deeply (
312 [ sort $lyric->lyric_versions->get_column ('text')->all ],
313 [ 'The color black', 'The colour black' ],
314 'Lyrics text via search matches',
315 );
f8a464e5 316}, 'Test might_have again but with just a PK and FK (neither specified) in the mid-table');
0c9b6ece 317
f8a464e5 318lives_ok ( sub {
04ec3909 319 my $newartist2 = $schema->resultset('Artist')->find_or_create({
320 name => 'Fred 3',
321 cds => [
322 {
323 title => 'Noah Act',
324 year => 2007,
325 },
326 ],
327 });
328 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
f8a464e5 329}, 'Nested find_or_create');
04ec3909 330
f8a464e5 331lives_ok ( sub {
3d8ee6ab 332 my $artist = $schema->resultset('Artist')->first;
333
334 my $cd_result = $artist->create_related('cds', {
335
336 title => 'TestOneCD1',
337 year => 2007,
338 tracks => [
1ceafb0c 339 { title => 'TrackOne' },
340 { title => 'TrackTwo' },
3d8ee6ab 341 ],
342
343 });
344
660cf1be 345 isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
3d8ee6ab 346 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
347
348 my $tracks = $cd_result->tracks;
349
660cf1be 350 isa_ok( $tracks, 'DBIx::Class::ResultSet', 'Got Expected Tracks ResultSet');
3d8ee6ab 351
352 foreach my $track ($tracks->all)
353 {
660cf1be 354 isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
3d8ee6ab 355 }
f8a464e5 356}, 'First create_related pass');
3d8ee6ab 357
f8a464e5 358lives_ok ( sub {
2ec8e594 359 my $artist = $schema->resultset('Artist')->first;
3d8ee6ab 360
2ec8e594 361 my $cd_result = $artist->create_related('cds', {
3d8ee6ab 362
2ec8e594 363 title => 'TestOneCD2',
3d8ee6ab 364 year => 2007,
365 tracks => [
1ceafb0c 366 { title => 'TrackOne' },
367 { title => 'TrackTwo' },
3d8ee6ab 368 ],
369
9c6d6d93 370 liner_notes => { notes => 'I can haz liner notes?' },
371
3d8ee6ab 372 });
373
660cf1be 374 isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
2ec8e594 375 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
9c6d6d93 376 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
3d8ee6ab 377
378 my $tracks = $cd_result->tracks;
379
660cf1be 380 isa_ok( $tracks, 'DBIx::Class::ResultSet', "Got Expected Tracks ResultSet");
3d8ee6ab 381
382 foreach my $track ($tracks->all)
383 {
660cf1be 384 isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
3d8ee6ab 385 }
f8a464e5 386}, 'second create_related with same arguments');
e5dddc05 387
f8a464e5 388lives_ok ( sub {
04ec3909 389 my $cdp = $schema->resultset('CD_to_Producer')->create({
390 cd => { artist => 1, title => 'foo', year => 2000 },
391 producer => { name => 'jorge' }
392 });
393 ok($cdp, 'join table record created ok');
f8a464e5 394}, 'create of parents of a record linker table');
2bc3c81e 395
f8a464e5 396lives_ok ( sub {
2bc3c81e 397 my $kurt_cobain = { name => 'Kurt Cobain' };
398
399 my $in_utero = $schema->resultset('CD')->new({
400 title => 'In Utero',
401 year => 1993
402 });
403
404 $kurt_cobain->{cds} = [ $in_utero ];
405
406
407 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
408 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
409
410 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
411 is($a->cds && $a->cds->first && $a->cds->first->title,
412 'In Utero', 'CD insertion ok');
f8a464e5 413}, 'populate');
e02b9964 414
f847f84d 415## Create foreign key col obj including PK
416## See test 20 in 66relationships.t
f8a464e5 417lives_ok ( sub {
04ec3909 418 my $new_cd_hashref = {
419 cdid => 27,
420 title => 'Boogie Woogie',
421 year => '2007',
422 artist => { artistid => 17, name => 'king luke' }
423 };
e02b9964 424
04ec3909 425 my $cd = $schema->resultset("CD")->find(1);
f10ac17d 426
04ec3909 427 is($cd->artist->id, 1, 'rel okay');
f10ac17d 428
04ec3909 429 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
430 is($new_cd->artist->id, 17, 'new id retained okay');
f8a464e5 431}, 'Create foreign key col obj including PK');
6ede9177 432
f8a464e5 433lives_ok ( sub {
6ede9177 434 $schema->resultset("CD")->create({
38c03c20 435 cdid => 28,
370f2ba2 436 title => 'Boogie Wiggle',
38c03c20 437 year => '2007',
438 artist => { artistid => 18, name => 'larry' }
6ede9177 439 });
f8a464e5 440}, 'new cd created without clash on related artist');
38c03c20 441
f8a464e5 442throws_ok ( sub {
370f2ba2 443 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
444 #$t->cd($t->new_related('cd', { artist => undef } ) );
445 #$t->{_rel_in_storage} = 0;
f10ac17d 446 $t->insert;
f8a464e5 447}, qr/cd.artist may not be NULL/, "Exception propogated properly");
bbbf67eb 448
f8a464e5 449lives_ok ( sub {
04ec3909 450 $schema->resultset('CD')->create ({
451 artist => {
452 name => 'larry', # should already exist
453 },
76b8cf98 454 title => 'Warble Marble',
455 year => '2009',
456 cd_to_producer => [
04ec3909 457 { producer => { name => 'Cowboy Neal' } },
76b8cf98 458 ],
04ec3909 459 });
76b8cf98 460
04ec3909 461 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
462 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
463 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
464 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
f8a464e5 465}, 'Test multi create over many_to_many');
76b8cf98 466
ab4b097b 4671;