11 my $schema = DBICTest->init_schema();
14 my $cd = $schema->resultset('CD')->create({
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');
28 my $bm_rs = $schema->resultset('Bookmark');
29 my $bookmark = $bm_rs->create({
35 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
36 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
39 { 'link.title' => $bookmark->link->title },
43 'Bookmark and link made it to the DB',
45 }, 'simple create where the child and parent have no values, except for an explicit parent pk ok');
48 my $artist = $schema->resultset('Artist')->first;
49 my $cd = $artist->create_related (cds => {
50 title => 'Music to code by',
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');
62 }, 'create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )');
66 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
67 $schema->resultset('Artist')->first->update({
69 { title => 'Yet another CD',
75 qr/Recursive update is not supported over relationships of type multi/,
76 'create via update of multi relationships throws an exception'
80 my $artist = $schema->resultset('Artist')->first;
81 my $c2p = $schema->resultset('CD_to_Producer')->create ({
84 title => 'Bad investment',
87 { title => 'Just buy' },
88 { title => 'Why did we do it' },
89 { title => 'Burn baby burn' },
93 name => 'Lehman Bros.',
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');
107 #CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
115 my $artist = $schema->resultset('Artist')->first;
116 my $cd = $schema->resultset('CD')->create ({
118 title => 'Music to code by at night',
122 title => 'Off by one again',
125 title => 'The dereferencer',
129 title => 'Was that a null (Single)',
131 { title => 'The dereferencer' },
132 { title => 'The dereferencer II' },
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');
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');
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');
166 is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
168 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
169 ['Don Knuth', 'K&R'],
170 'Producers named correctly',
172 }, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at a has_many level');
174 #Track -> might have -> Single -> has_many -> Tracks
182 my $cd = $schema->resultset('CD')->first;
183 my $track = $schema->resultset('Track')->create ({
185 title => 'Multicreate rocks',
187 artist => $cd->artist,
189 title => 'Disemboweling MultiCreate',
191 { title => 'Why does mst write this way' },
192 { title => 'Chainsaw celebration' },
193 { title => 'Purl cleans up' },
215 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
216 is ($track->title, 'Multicreate rocks', 'Correct Track title');
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');
225 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
227 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
228 ['castaway', 'mst', 'theorbtwo'],
229 'Producers named correctly',
231 }, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at the might_have directly');
234 my $artist = $schema->resultset('Artist')->first;
235 my $cd = $schema->resultset('CD')->create ({
237 title => 'Music to code by at twilight',
241 { name => 'recursive descent' },
242 { name => 'tail packing' },
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');
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');
256 [ sort $art_obj->images->get_column ('name')->all ],
257 [ 'recursive descent', 'tail packing' ],
258 'Images named correctly in objects',
261 my $artwork = $schema->resultset('Artwork')->search (
262 { 'cd.title' => 'Music to code by at twilight' },
266 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
269 [ sort $artwork->images->get_column ('name')->all ],
270 [ 'recursive descent', 'tail packing' ],
271 'Images named correctly after search',
273 }, 'Test might_have again but with a PK == FK in the middle (obviously not specified)');
276 my $cd = $schema->resultset('CD')->first;
277 my $track = $schema->resultset ('Track')->create ({
282 { text => 'The color black' },
283 { text => 'The colour black' },
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');
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');
298 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
299 [ 'The color black', 'The colour black' ],
300 'Lyrics text in objects matches',
304 my $lyric = $schema->resultset('Lyrics')->search (
305 { 'track.title' => 'Black' },
309 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
312 [ sort $lyric->lyric_versions->get_column ('text')->all ],
313 [ 'The color black', 'The colour black' ],
314 'Lyrics text via search matches',
316 }, 'Test might_have again but with just a PK and FK (neither specified) in the mid-table');
319 my $newartist2 = $schema->resultset('Artist')->find_or_create({
328 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
329 }, 'Nested find_or_create');
332 my $artist = $schema->resultset('Artist')->first;
334 my $cd_result = $artist->create_related('cds', {
336 title => 'TestOneCD1',
339 { title => 'TrackOne' },
340 { title => 'TrackTwo' },
345 isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
346 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
348 my $tracks = $cd_result->tracks;
350 isa_ok( $tracks, 'DBIx::Class::ResultSet', 'Got Expected Tracks ResultSet');
352 foreach my $track ($tracks->all)
354 isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
356 }, 'First create_related pass');
359 my $artist = $schema->resultset('Artist')->first;
361 my $cd_result = $artist->create_related('cds', {
363 title => 'TestOneCD2',
366 { title => 'TrackOne' },
367 { title => 'TrackTwo' },
370 liner_notes => { notes => 'I can haz liner notes?' },
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');
378 my $tracks = $cd_result->tracks;
380 isa_ok( $tracks, 'DBIx::Class::ResultSet', "Got Expected Tracks ResultSet");
382 foreach my $track ($tracks->all)
384 isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
386 }, 'second create_related with same arguments');
389 my $cdp = $schema->resultset('CD_to_Producer')->create({
390 cd => { artist => 1, title => 'foo', year => 2000 },
391 producer => { name => 'jorge' }
393 ok($cdp, 'join table record created ok');
394 }, 'create of parents of a record linker table');
397 my $kurt_cobain = { name => 'Kurt Cobain' };
399 my $in_utero = $schema->resultset('CD')->new({
404 $kurt_cobain->{cds} = [ $in_utero ];
407 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
408 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
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');
415 ## Create foreign key col obj including PK
416 ## See test 20 in 66relationships.t
418 my $new_cd_hashref = {
420 title => 'Boogie Woogie',
422 artist => { artistid => 17, name => 'king luke' }
425 my $cd = $schema->resultset("CD")->find(1);
427 is($cd->artist->id, 1, 'rel okay');
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');
434 $schema->resultset("CD")->create({
436 title => 'Boogie Wiggle',
438 artist => { artistid => 18, name => 'larry' }
440 }, 'new cd created without clash on related artist');
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;
447 }, qr/cd.artist may not be NULL/, "Exception propogated properly");
450 $schema->resultset('CD')->create ({
452 name => 'larry', # should already exist
454 title => 'Warble Marble',
457 { producer => { name => 'Cowboy Neal' } },
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');