Final insanification of the multicreate test
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 plan tests => 55;
9
10 my $schema = DBICTest->init_schema();
11
12 # simple create + belongs_to
13 eval {
14   my $cd2 = $schema->resultset('CD')->create({
15     artist => { 
16       name => 'Fred Bloggs' 
17     },
18     title => 'Some CD',
19     year => 1996
20   });
21
22   isa_ok($cd2, 'DBICTest::CD', 'Created CD object');
23   isa_ok($cd2->artist, 'DBICTest::Artist', 'Created related Artist');
24   is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
25 };
26 diag $@ if $@;
27
28 # create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
29 eval {
30   my $artist = $schema->resultset('Artist')->create(
31     { name => 'Fred 2',
32       cds => [
33         { title => 'Music to code by',
34           year => 2007,
35           tags => [
36             { 'tag' => 'rock' },
37           ],
38         },
39     ],
40   });
41
42   isa_ok($artist, 'DBICTest::Artist', 'Created Artist');
43   is($artist->name, 'Fred 2', 'Artist created correctly');
44   is($artist->cds->count, 1, 'One CD created for artist');
45   is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
46   is($artist->cds->first->tags->count, 1, 'One tag created for CD');
47   is($artist->cds->first->tags->first->tag, 'rock', 'Tag created correctly');
48
49   # Create via update - add a new CD
50   $artist->update({
51     cds => [ $artist->cds,
52       { title => 'Yet another CD',
53         year => 2006,
54       },
55     ],
56   });
57   is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
58
59   my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
60
61   is($newartist->name, 'Fred 2', 'Retrieved the artist');
62 };
63 diag $@ if $@;
64
65 # nested find_or_create
66 eval {
67   my $newartist2 = $schema->resultset('Artist')->find_or_create({ 
68     name => 'Fred 3',
69     cds => [
70       { 
71         title => 'Noah Act',
72         year => 2007,
73       },
74     ],
75   });
76   is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
77 };
78 diag $@ if $@;
79
80 # multiple same level has_many create
81 eval {
82   my $artist2 = $schema->resultset('Artist')->create({
83     name => 'Fred 3',
84     cds => [
85       {
86         title => 'Music to code by',
87         year => 2007,
88       },
89     ],
90     cds_unordered => [
91       {
92         title => 'Music to code by',
93         year => 2007,
94       },
95     ]
96   });
97
98   is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
99 };
100 diag $@ if $@;
101
102 # first create_related pass
103 eval {
104         my $artist = $schema->resultset('Artist')->first;
105         
106         my $cd_result = $artist->create_related('cds', {
107         
108                 title => 'TestOneCD1',
109                 year => 2007,
110                 tracks => [
111                 
112                         { position=>111,
113                           title => 'TrackOne',
114                         },
115                         { position=>112,
116                           title => 'TrackTwo',
117                         }
118                 ],
119
120         });
121         
122         ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
123         ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
124         
125         my $tracks = $cd_result->tracks;
126         
127         ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
128         
129         foreach my $track ($tracks->all)
130         {
131                 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
132         }
133 };
134 diag $@ if $@;
135
136 # second create_related with same arguments
137 eval {
138         my $artist = $schema->resultset('Artist')->first;
139         
140         my $cd_result = $artist->create_related('cds', {
141         
142                 title => 'TestOneCD2',
143                 year => 2007,
144                 tracks => [
145                 
146                         { position=>111,
147                           title => 'TrackOne',
148                         },
149                         { position=>112,
150                           title => 'TrackTwo',
151                         }
152                 ],
153
154     liner_notes => { notes => 'I can haz liner notes?' },
155
156         });
157         
158         ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
159         ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
160   ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
161         
162         my $tracks = $cd_result->tracks;
163         
164         ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
165         
166         foreach my $track ($tracks->all)
167         {
168                 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
169         }
170 };
171 diag $@ if $@;
172
173 # create of parents of a record linker table
174 eval {
175   my $cdp = $schema->resultset('CD_to_Producer')->create({
176     cd => { artist => 1, title => 'foo', year => 2000 },
177     producer => { name => 'jorge' }
178   });
179   ok($cdp, 'join table record created ok');
180 };
181 diag $@ if $@;
182
183 #SPECIAL_CASE
184 eval {
185   my $kurt_cobain = { name => 'Kurt Cobain' };
186
187   my $in_utero = $schema->resultset('CD')->new({
188       title => 'In Utero',
189       year  => 1993
190     });
191
192   $kurt_cobain->{cds} = [ $in_utero ];
193
194
195   $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
196   $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
197
198   is($a->name, 'Kurt Cobain', 'Artist insertion ok');
199   is($a->cds && $a->cds->first && $a->cds->first->title, 
200                   'In Utero', 'CD insertion ok');
201 };
202 diag $@ if $@;
203
204 #SPECIAL_CASE2
205 eval {
206   my $pink_floyd = { name => 'Pink Floyd' };
207
208   my $the_wall = { title => 'The Wall', year  => 1979 };
209
210   $pink_floyd->{cds} = [ $the_wall ];
211
212
213   $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
214   $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
215
216   is($a->name, 'Pink Floyd', 'Artist insertion ok');
217   is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
218 };
219 diag $@ if $@;
220
221 ## Create foreign key col obj including PK
222 ## See test 20 in 66relationships.t
223 eval {
224   my $new_cd_hashref = { 
225     cdid => 27, 
226     title => 'Boogie Woogie', 
227     year => '2007', 
228     artist => { artistid => 17, name => 'king luke' }
229   };
230
231   my $cd = $schema->resultset("CD")->find(1);
232
233   is($cd->artist->id, 1, 'rel okay');
234
235   my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
236   is($new_cd->artist->id, 17, 'new id retained okay');
237 };
238 diag $@ if $@;
239
240 eval {
241         $schema->resultset("CD")->create({ 
242               cdid => 28, 
243               title => 'Boogie Wiggle', 
244               year => '2007', 
245               artist => { artistid => 18, name => 'larry' }
246              });
247 };
248 is($@, '', 'new cd created without clash on related artist');
249
250 # Make sure exceptions from errors in created rels propogate
251 eval {
252     my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
253     #$t->cd($t->new_related('cd', { artist => undef } ) );
254     #$t->{_rel_in_storage} = 0;
255     $t->insert;
256 };
257 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
258
259 # Test multi create over many_to_many
260 eval {
261   $schema->resultset('CD')->create ({
262     artist => {
263       name => 'larry', # should already exist
264     },
265     title => 'Warble Marble',
266     year => '2009',
267     cd_to_producer => [
268       { producer => { name => 'Cowboy Neal' } },
269     ],
270   });
271
272   my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
273   is ($m2m_cd->count, 1, 'One CD row created via M2M create');
274   is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
275   is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
276 };
277
278 # and some insane multicreate 
279 # (should work, despite the fact that no one will probably use it this way)
280
281 # first count how many rows do we initially have
282 my $counts;
283 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
284
285 # do the crazy create
286 eval {
287   $schema->resultset('CD')->create ({
288     artist => {
289       name => 'james',
290     },
291     title => 'Greatest hits 1',
292     year => '2012',
293     genre => {
294       name => '"Greatest" collections',
295     },
296     tags => [
297       { tag => 'A' },
298       { tag => 'B' },
299     ],
300     cd_to_producer => [
301       {
302         producer => {
303           name => 'bob',
304           producer_to_cd => [
305             {
306               cd => { 
307                 artist => {
308                   name => 'lars',
309                   cds => [
310                     {
311                       title => 'Greatest hits 2',
312                       year => 2012,
313                       genre => {
314                         name => '"Greatest" collections',
315                       },
316                       tags => [
317                         { tag => 'A' },
318                         { tag => 'B' },
319                       ],
320                       # This cd is created via artist so it doesn't know about producers
321                       cd_to_producer => [
322                         # if we specify 'bob' here things bomb
323                         # as the producer attached to Greatest Hits 1 is
324                         # already created, but not yet inserted.
325                         # Maybe this can be fixed, but things are hairy
326                         # enough already.
327                         #
328                         #{ producer => { name => 'bob' } },
329                         { producer => { name => 'paul' } },
330                         { producer => {
331                           name => 'flemming',
332                           producer_to_cd => [
333                             { cd => {
334                               artist => {
335                                 name => 'kirk',
336                                 cds => [
337                                   {
338                                     title => 'Greatest hits 3',
339                                     year => 2012,
340                                     genre => {
341                                       name => '"Greatest" collections',
342                                     },
343                                     tags => [
344                                       { tag => 'A' },
345                                       { tag => 'B' },
346                                     ],
347                                   },
348                                   {
349                                     title => 'Greatest hits 4',
350                                     year => 2012,
351                                     genre => {
352                                       name => '"Greatest" collections2',
353                                     },
354                                     tags => [
355                                       { tag => 'A' },
356                                       { tag => 'B' },
357                                     ],
358                                   },
359                                 ],
360                               },
361                               title => 'Greatest hits 5',
362                               year => 2013,
363                               genre => {
364                                 name => '"Greatest" collections2',
365                               },
366                             }},
367                           ],
368                         }},
369                       ],
370                     },
371                   ],
372                 },
373                 title => 'Greatest hits 6',
374                 year => 2012,
375                 genre => {
376                   name => '"Greatest" collections',
377                 },
378                 tags => [
379                   { tag => 'A' },
380                   { tag => 'B' },
381                 ],
382               },
383             },
384             {
385               cd => { 
386                 artist => {
387                   name => 'lars',    # should already exist
388                   # even though the artist 'name' is not uniquely constrained
389                   # find_or_create will arguably DWIM 
390                 },
391                 title => 'Greatest hits 7',
392                 year => 2013,
393               },
394             },
395           ],
396         },
397       },
398     ],
399   });
400
401   is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
402   is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
403   is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
404   is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
405   is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
406
407   my $cd_rs = $schema->resultset ('CD')
408     ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
409   is ($cd_rs->count, 7, '7 greatest hits created');
410
411   my $cds_2012 = $cd_rs->search ({ year => 2012});
412   is ($cds_2012->count, 5, '5 CDs created in 2012');
413
414   is (
415     $cds_2012->search(
416       { 'tags.tag' => { -in => [qw/A B/] } },
417       { join => 'tags', group_by => 'me.cdid' }
418     ),
419     5,
420     '10 tags were pairwise distributed between 5 CDs'
421   );
422
423   my $paul_prod = $cd_rs->search (
424     { 'producer.name' => 'paul'},
425     { join => { cd_to_producer => 'producer' } }
426   );
427   is ($paul_prod->count, 1, 'Paul had 1 production');
428   my $pauls_cd = $paul_prod->single;
429   is ($pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer');
430   is (
431     $pauls_cd->search_related ('cd_to_producer',
432       { 'producer.name' => 'flemming'},
433       { join => 'producer' }
434     )->count,
435     1,
436     'The second producer is flemming',
437   );
438
439   my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
440   is ($kirk_cds, 3, 'Kirk had 3 CDs');
441   is (
442     $kirk_cds->search (
443       { 'cd_to_producer.cd' => { '!=', undef } },
444       { join => 'cd_to_producer' },
445     ),
446     1,
447     'Kirk had a producer only on one cd',
448   );
449
450   my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
451   is ($lars_cds->count, 3, 'Lars had 3 CDs');
452   is (
453     $lars_cds->search (
454       { 'cd_to_producer.cd' => undef },
455       { join => 'cd_to_producer' },
456     ),
457     0,
458     'Lars always had a producer',
459   );
460   is (
461     $lars_cds->search_related ('cd_to_producer',
462       { 'producer.name' => 'flemming'},
463       { join => 'producer' }
464     )->count,
465     1,
466     'Lars produced 1 CD with flemming',
467   );
468   is (
469     $lars_cds->search_related ('cd_to_producer',
470       { 'producer.name' => 'bob'},
471       { join => 'producer' }
472     )->count,
473     2,
474     'Lars produced 2 CDs with bob',
475   );
476
477   my $bob_prod = $cd_rs->search (
478     { 'producer.name' => 'bob'},
479     { join => { cd_to_producer => 'producer' } }
480   );
481   is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
482
483   is (
484     $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
485     1,
486     "Bob prpdoced james' only CD",
487   );
488 };
489 diag $@ if $@;