Fix last pieces of retardation and UNtodo the quick cycle
[dbsrgits/DBIx-Class.git] / t / multi_create / standard.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 plan tests => 91;
10
11 my $schema = DBICTest->init_schema();
12
13 lives_ok ( sub {
14   my $cd = $schema->resultset('CD')->create({
15     artist => { 
16       name => 'Fred Bloggs' 
17     },
18     title => 'Some CD',
19     year => 1996
20   });
21
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');
25 }, 'simple create + parent (the stuff $rs belongs_to) ok');
26
27 lives_ok ( sub {
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   );
45 }, 'simple create where the child and parent have no values, except for an explicit parent pk ok');
46
47 lives_ok ( sub {
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   });
56
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');
61
62 }, 'create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )');
63
64 throws_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
79 lives_ok ( sub {
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 => [
87         { title => 'Just buy' },
88         { title => 'Why did we do it' },
89         { title => 'Burn baby burn' },
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');
104 }, 'Create m2m while originating in the linker table');
105
106
107 #CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
108 #                                               \
109 #                                                \-> has_many \
110 #                                                              --> CD2Producer
111 #                                                /-> has_many /
112 #                                               /
113 #                                          Producer
114 lives_ok ( sub {
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       {
122         title => 'Off by one again',
123       },
124       {
125         title => 'The dereferencer',
126         cd_single => {
127           artist => $artist,
128           year => 2008,
129           title => 'Was that a null (Single)',
130           tracks => [
131             { title => 'The dereferencer' },
132             { title => 'The dereferencer II' },
133           ],
134           cd_to_producer => [
135             {
136               producer => {
137                 name => 'K&R',
138               }
139             },
140             {
141               producer => {
142                 name => 'Don Knuth',
143               }
144             },
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
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   );
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
181 lives_ok ( sub {
182   my $cd = $schema->resultset('CD')->first;
183   my $track = $schema->resultset('Track')->create ({
184     cd => $cd,
185     title => 'Multicreate rocks',
186     cd_single => {
187       artist => $cd->artist,
188       year => 2008,
189       title => 'Disemboweling MultiCreate',
190       tracks => [
191         { title => 'Why does mst write this way' },
192         { title => 'Chainsaw celebration' },
193         { title => 'Purl cleans up' },
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   );
231 }, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at the might_have directly');
232
233 lives_ok ( sub {
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
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');
255   is_deeply (
256     [ sort $art_obj->images->get_column ('name')->all ],
257     [ 'recursive descent', 'tail packing' ],
258     'Images named correctly in objects',
259   );
260
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   );
273 }, 'Test might_have again but with a PK == FK in the middle (obviously not specified)');
274
275 lives_ok ( sub {
276   my $cd = $schema->resultset('CD')->first;
277   my $track = $schema->resultset ('Track')->create ({
278     cd => $cd,
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
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');
297   is_deeply (
298     [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
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   );
316 }, 'Test might_have again but with just a PK and FK (neither specified) in the mid-table');
317
318 lives_ok ( sub {
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');
329 }, 'Nested find_or_create');
330
331 lives_ok ( sub {
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 => [
339                         { title => 'TrackOne' },
340                         { title => 'TrackTwo' },
341                 ],
342
343         });
344         
345         isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
346         ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
347         
348         my $tracks = $cd_result->tracks;
349         
350         isa_ok( $tracks, 'DBIx::Class::ResultSet', 'Got Expected Tracks ResultSet');
351         
352         foreach my $track ($tracks->all)
353         {
354                 isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
355         }
356 }, 'First create_related pass');
357
358 lives_ok ( sub {
359         my $artist = $schema->resultset('Artist')->first;
360         
361         my $cd_result = $artist->create_related('cds', {
362         
363                 title => 'TestOneCD2',
364                 year => 2007,
365                 tracks => [
366                         { title => 'TrackOne' },
367                         { title => 'TrackTwo' },
368                 ],
369
370     liner_notes => { notes => 'I can haz liner notes?' },
371
372         });
373         
374         isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
375         ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
376   ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
377         
378         my $tracks = $cd_result->tracks;
379         
380         isa_ok( $tracks, 'DBIx::Class::ResultSet', "Got Expected Tracks ResultSet");
381         
382         foreach my $track ($tracks->all)
383         {
384                 isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
385         }
386 }, 'second create_related with same arguments');
387
388 lives_ok ( sub {
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');
394 }, 'create of parents of a record linker table');
395
396 lives_ok ( sub {
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');
413 }, 'populate');
414
415 ## Create foreign key col obj including PK
416 ## See test 20 in 66relationships.t
417 lives_ok ( sub {
418   my $new_cd_hashref = { 
419     cdid => 27, 
420     title => 'Boogie Woogie', 
421     year => '2007', 
422     artist => { artistid => 17, name => 'king luke' }
423   };
424
425   my $cd = $schema->resultset("CD")->find(1);
426
427   is($cd->artist->id, 1, 'rel okay');
428
429   my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
430   is($new_cd->artist->id, 17, 'new id retained okay');
431 }, 'Create foreign key col obj including PK');
432
433 lives_ok ( sub {
434         $schema->resultset("CD")->create({ 
435               cdid => 28, 
436               title => 'Boogie Wiggle', 
437               year => '2007', 
438               artist => { artistid => 18, name => 'larry' }
439              });
440 }, 'new cd created without clash on related artist');
441
442 throws_ok ( sub {
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;
446     $t->insert;
447 }, qr/cd.artist may not be NULL/, "Exception propogated properly");
448
449 lives_ok ( sub {
450   $schema->resultset('CD')->create ({
451     artist => {
452       name => 'larry', # should already exist
453     },
454     title => 'Warble Marble',
455     year => '2009',
456     cd_to_producer => [
457       { producer => { name => 'Cowboy Neal' } },
458     ],
459   });
460
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');
465 }, 'Test multi create over many_to_many');
466
467 1;