normalize the indentation and detabify
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
CommitLineData
33dd4e80 1use strict;
af2d42c0 2use warnings;
33dd4e80 3
04ec3909 4use Test::More;
33dd4e80 5use lib qw(t/lib);
6use DBICTest;
7
04ec3909 8plan tests => 51;
9
33dd4e80 10my $schema = DBICTest->init_schema();
11
04ec3909 12# simple create + belongs_to
13eval {
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};
26diag $@ if $@;
27
28# create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
29eval {
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};
63diag $@ if $@;
64
65# nested find_or_create
66eval {
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};
78diag $@ if $@;
79
80# multiple same level has_many create
81eval {
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};
100diag $@ if $@;
3d8ee6ab 101
04ec3909 102# first create_related pass
103eval {
3d8ee6ab 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 }
04ec3909 133};
134diag $@ if $@;
3d8ee6ab 135
04ec3909 136# second create_related with same arguments
137eval {
2ec8e594 138 my $artist = $schema->resultset('Artist')->first;
3d8ee6ab 139
2ec8e594 140 my $cd_result = $artist->create_related('cds', {
3d8ee6ab 141
2ec8e594 142 title => 'TestOneCD2',
3d8ee6ab 143 year => 2007,
144 tracks => [
145
146 { position=>111,
147 title => 'TrackOne',
148 },
149 { position=>112,
150 title => 'TrackTwo',
151 }
152 ],
153
9c6d6d93 154 liner_notes => { notes => 'I can haz liner notes?' },
155
3d8ee6ab 156 });
157
158 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
2ec8e594 159 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
9c6d6d93 160 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
3d8ee6ab 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 }
04ec3909 170};
171diag $@ if $@;
e5dddc05 172
04ec3909 173# create of parents of a record linker table
174eval {
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};
181diag $@ if $@;
2bc3c81e 182
04ec3909 183#SPECIAL_CASE
184eval {
2bc3c81e 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');
04ec3909 201};
202diag $@ if $@;
2bc3c81e 203
04ec3909 204#SPECIAL_CASE2
205eval {
2bc3c81e 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');
04ec3909 218};
219diag $@ if $@;
e02b9964 220
221## Create foreign key col obj including PK
222## See test 20 in 66relationships.t
04ec3909 223eval {
224 my $new_cd_hashref = {
225 cdid => 27,
226 title => 'Boogie Woogie',
227 year => '2007',
228 artist => { artistid => 17, name => 'king luke' }
229 };
e02b9964 230
04ec3909 231 my $cd = $schema->resultset("CD")->find(1);
f10ac17d 232
04ec3909 233 is($cd->artist->id, 1, 'rel okay');
f10ac17d 234
04ec3909 235 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
236 is($new_cd->artist->id, 17, 'new id retained okay');
237};
238diag $@ if $@;
6ede9177 239
240eval {
241 $schema->resultset("CD")->create({
38c03c20 242 cdid => 28,
370f2ba2 243 title => 'Boogie Wiggle',
38c03c20 244 year => '2007',
245 artist => { artistid => 18, name => 'larry' }
6ede9177 246 });
38c03c20 247};
6ede9177 248is($@, '', 'new cd created without clash on related artist');
38c03c20 249
f10ac17d 250# Make sure exceptions from errors in created rels propogate
251eval {
370f2ba2 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;
f10ac17d 255 $t->insert;
256};
257like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
bbbf67eb 258
259# Test multi create over many_to_many
04ec3909 260eval {
261 $schema->resultset('CD')->create ({
262 artist => {
263 name => 'larry', # should already exist
264 },
76b8cf98 265 title => 'Warble Marble',
266 year => '2009',
267 cd_to_producer => [
04ec3909 268 { producer => { name => 'Cowboy Neal' } },
76b8cf98 269 ],
04ec3909 270 });
76b8cf98 271
04ec3909 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};
76b8cf98 277
278# and some insane multicreate
279# (should work, despite the fact that no one will probably use it this way)
280
04ec3909 281# first count how many rows do we initially have
76b8cf98 282
283my $counts;
04ec3909 284$counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
76b8cf98 285
286# do the crazy create
04ec3909 287eval {
288 $schema->resultset('CD')->create ({
289 artist => {
290 name => 'larry',
291 },
76b8cf98 292 title => 'Greatest hits 1',
293 year => '2012',
294 genre => {
295 name => '"Greatest" collections',
296 },
04ec3909 297 tags => [
298 { tag => 'A' },
299 { tag => 'B' },
300 ],
76b8cf98 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',
04ec3909 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 ],
76b8cf98 323 },
324 title => 'Greatest hits 2',
325 year => 2012,
326 genre => {
327 name => '"Greatest" collections',
328 },
04ec3909 329 tags => [
330 { tag => 'A' },
331 { tag => 'B' },
332 ],
76b8cf98 333 },
334 },
335 {
336 cd => {
337 artist => {
04ec3909 338 name => 'larry', # should already exist
76b8cf98 339 },
76b8cf98 340 title => 'Greatest hits 4',
341 year => 2012,
342 },
343 },
344 ],
345 },
346 },
347 ],
04ec3909 348 });
76b8cf98 349
04ec3909 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');
76b8cf98 355
04ec3909 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');
76b8cf98 360
04ec3909 361 my $harry_productions = $schema->resultset ('Producer')->single ({name => 'Dirty Harry'})
76b8cf98 362 ->search_related ('producer_to_cd', {})->search_related ('cd', {});
04ec3909 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');
76b8cf98 369
04ec3909 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};
379diag $@ if $@;