refactor of t/96multi_create.t:
[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 => 51;
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
283 my $counts;
284 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
285
286 # do the crazy create
287 eval {
288   $schema->resultset('CD')->create ({
289     artist => {
290       name => 'larry',
291     },
292     title => 'Greatest hits 1',
293     year => '2012',
294     genre => {
295       name => '"Greatest" collections',
296     },
297     tags => [
298       { tag => 'A' },
299       { tag => 'B' },
300     ],
301     cd_to_producer => [
302       {
303         producer => {
304           name => 'Dirty Harry',
305           producer_to_cd => [
306             {
307               cd => { 
308                 artist => {
309                   name => 'Dirty Harry himself',
310                   cds => [
311                     {
312                       title => 'Greatest hits 3',
313                       year => 2012,
314                       genre => {
315                         name => '"Greatest" collections',
316                       },
317                       tags => [
318                         { tag => 'A' },
319                         { tag => 'B' },
320                       ],
321                     },
322                   ],
323                 },
324                 title => 'Greatest hits 2',
325                 year => 2012,
326                 genre => {
327                   name => '"Greatest" collections',
328                 },
329                 tags => [
330                   { tag => 'A' },
331                   { tag => 'B' },
332                 ],
333               },
334             },
335             {
336               cd => { 
337                 artist => {
338                   name => 'larry',    # should already exist
339                 },
340                 title => 'Greatest hits 4',
341                 year => 2012,
342               },
343             },
344           ],
345         },
346       },
347     ],
348   });
349
350   is ($schema->resultset ('Artist')->count, $counts->{Artist} + 1, 'One new artists created');  # even though the 'name' is not uniquely constrained find_or_create will arguably DWIM
351   is ($schema->resultset ('Genre')->count, $counts->{Genre} + 1, 'One additional genre created');
352   is ($schema->resultset ('Producer')->count, $counts->{Producer} + 1, 'One new producer');
353   is ($schema->resultset ('CD')->count, $counts->{CD} + 4, '4 new CDs');
354   is ($schema->resultset ('Tag')->count, $counts->{Tag} + 6, '6 new Tags');
355
356   my $harry_cds = $schema->resultset ('Artist')->single ({name => 'Dirty Harry himself'})->cds;
357   is ($harry_cds->count, 2, 'Two CDs created by Harry');
358   ok ($harry_cds->single ({title => 'Greatest hits 2'}), 'First CD name correct');
359   ok ($harry_cds->single ({title => 'Greatest hits 3'}), 'Second CD name correct');
360
361   my $harry_productions = $schema->resultset ('Producer')->single ({name => 'Dirty Harry'})
362     ->search_related ('producer_to_cd', {})->search_related ('cd', {});
363   is ($harry_productions->count, 4, 'All 4 CDs are produced by Harry');
364   is ($harry_productions->search ({ year => 2012 })->count, 4, 'All 4 CDs have the correct year');
365
366   my $hits_genre = $schema->resultset ('Genre')->single ({name => '"Greatest" collections'});
367   ok ($hits_genre, 'New genre row found');
368   is ($hits_genre->cds->count, 3, 'Three of the new CDs fall into the new genre');
369
370   my $a_tags = $schema->resultset('Tag')->search({ tag => 'A'});
371   my $b_tags = $schema->resultset('Tag')->search({ tag => 'A'});
372   is ($a_tags->count, 3, '3 A tags');
373   is ($a_tags->count, 3, '3 B tags');
374
375   my $cds_with_ab = $schema->resultset('CD')
376     ->search({ 'tags.tag' => { -in => [qw/A B/] } }, { join => 'tags', group_by => 'me.cdid' } );
377   is ($cds_with_ab->count, 3, '6 tags were pairwise distributed between 3 CDs');
378 };
379 diag $@ if $@;