1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
12 my $schema = DBICTest->init_schema();
15 my $cd = $schema->resultset('CD')->create({
23 isa_ok($cd, 'DBICTest::CD', 'Created CD object');
24 isa_ok($cd->artist, 'DBICTest::Artist', 'Created related Artist');
25 is($cd->artist->name, 'Fred Bloggs', 'Artist created correctly');
26 }, 'simple create + parent (the stuff $rs belongs_to) ok');
29 my $bm_rs = $schema->resultset('Bookmark');
30 my $bookmark = $bm_rs->create({
36 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
37 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
40 { 'link.title' => $bookmark->link->title },
44 'Bookmark and link made it to the DB',
46 }, 'simple create where the child and parent have no values, except for an explicit parent pk ok');
49 my $artist = $schema->resultset('Artist')->first;
50 my $cd = $artist->create_related (cds => {
51 title => 'Music to code by',
58 isa_ok($cd, 'DBICTest::CD', 'Created CD');
59 is($cd->title, 'Music to code by', 'CD created correctly');
60 is($cd->tags->count, 1, 'One tag created for CD');
61 is($cd->tags->first->tag, 'rock', 'Tag created correctly');
63 }, 'create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )');
67 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
68 $schema->resultset('Artist')->first->update({
70 { title => 'Yet another CD',
76 qr/Recursive update is not supported over relationships of type 'multi'/,
77 'create via update of multi relationships throws an exception'
81 my $artist = $schema->resultset('Artist')->first;
82 my $c2p = $schema->resultset('CD_to_Producer')->create ({
85 title => 'Bad investment',
88 { title => 'Just buy' },
89 { title => 'Why did we do it' },
90 { title => 'Burn baby burn' },
94 name => 'Lehman Bros.',
98 isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created');
99 my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' });
100 isa_ok ($prod, 'DBICTest::Producer', 'Producer row found');
101 is ($prod->cds->count, 1, 'Producer has one production');
102 my $cd = $prod->cds->first;
103 is ($cd->title, 'Bad investment', 'CD created correctly');
104 is ($cd->tracks->count, 3, 'CD has 3 tracks');
105 }, 'Create m2m while originating in the linker table');
108 #CD -> has_many -> Tracks -> might have -> Single -> has_many -> Tracks
116 my $artist = $schema->resultset('Artist')->find(1);
117 my $cd = $schema->resultset('CD')->create ({
119 title => 'Music to code by at night',
123 title => 'Off by one again',
126 title => 'The dereferencer',
130 title => 'Was that a null (Single)',
132 { title => 'The dereferencer' },
133 { title => 'The dereferencer II' },
152 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
153 is ($cd->title, 'Music to code by at night', 'Correct CD title');
154 is ($cd->tracks->count, 2, 'Two tracks on main CD');
156 my ($t1, $t2) = sort { $a->id <=> $b->id } $cd->tracks->all;
157 is ($t1->title, 'Off by one again', 'Correct 1st track name');
158 is ($t1->cd_single, undef, 'No single for 1st track');
159 is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
160 isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
162 my $single = $t2->cd_single;
163 is ($single->tracks->count, 2, 'Two tracks on single CD');
164 is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
165 is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
167 is ($single->cd_to_producer->count, 2, 'Two producers created for the single cd');
169 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
170 ['Don Knuth', 'K&R'],
171 'Producers named correctly',
173 }, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at a has_many level');
175 #Track -> might have -> Single -> has_many -> Tracks
183 my $cd = $schema->resultset('CD')->first;
184 my $track = $schema->resultset('Track')->create ({
186 title => 'Multicreate rocks',
188 artist => $cd->artist,
190 title => 'Disemboweling MultiCreate',
192 { title => 'Why does mst write this way' },
193 { title => 'Chainsaw celebration' },
194 { title => 'Purl cleans up' },
216 isa_ok ($track, 'DBICTest::Track', 'Main Track object created');
217 is ($track->title, 'Multicreate rocks', 'Correct Track title');
219 my $single = $track->cd_single;
220 isa_ok ($single, 'DBICTest::CD', 'Created a single with the track');
221 is ($single->tracks->count, 3, '3 tracks on single CD');
222 is ($single->tracks->find ({ position => 1})->title, 'Why does mst write this way', 'Correct 1st track title');
223 is ($single->tracks->find ({ position => 2})->title, 'Chainsaw celebration', 'Correct 2nd track title');
224 is ($single->tracks->find ({ position => 3})->title, 'Purl cleans up', 'Correct 3rd track title');
226 is ($single->cd_to_producer->count, 3, '3 producers created for the single cd');
228 [ sort map { $_->producer->name } ($single->cd_to_producer->all) ],
229 ['castaway', 'mst', 'theorbtwo'],
230 'Producers named correctly',
232 }, 'Create over > 1 levels of might_have with multiple has_many and multiple m2m but starting at the might_have directly');
235 my $artist = $schema->resultset('Artist')->first;
236 my $cd = $schema->resultset('CD')->create ({
238 title => 'Music to code by at twilight',
242 { name => 'recursive descent' },
243 { name => 'tail packing' },
248 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
249 is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
250 isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
252 # this test might look weird, but it failed at one point, keep it there
253 my $art_obj = $cd->artwork;
254 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
255 is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
257 [ sort $art_obj->images->get_column ('name')->all ],
258 [ 'recursive descent', 'tail packing' ],
259 'Images named correctly in objects',
262 my $artwork = $schema->resultset('Artwork')->search (
263 { 'cd.title' => 'Music to code by at twilight' },
267 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
270 [ sort $artwork->images->get_column ('name')->all ],
271 [ 'recursive descent', 'tail packing' ],
272 'Images named correctly after search',
274 }, 'Test might_have again but with a PK == FK in the middle (obviously not specified)');
277 my $cd = $schema->resultset('CD')->first;
278 my $track = $schema->resultset ('Track')->create ({
283 { text => 'The color black' },
284 { text => 'The colour black' },
289 isa_ok ($track, 'DBICTest::Track', 'Main track object created');
290 is ($track->title, 'Black', 'Correct track title');
291 isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
293 # this test might look weird, but it was failing at one point, keep it there
294 my $lyric_obj = $track->lyrics;
295 ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
296 ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
297 is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
299 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
300 [ 'The color black', 'The colour black' ],
301 'Lyrics text in objects matches',
305 my $lyric = $schema->resultset('Lyrics')->search (
306 { 'track.title' => 'Black' },
310 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
313 [ sort $lyric->lyric_versions->get_column ('text')->all ],
314 [ 'The color black', 'The colour black' ],
315 'Lyrics text via search matches',
317 }, 'Test might_have again but with just a PK and FK (neither specified) in the mid-table');
320 my $newartist2 = $schema->resultset('Artist')->find_or_create({
329 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
330 }, 'Nested find_or_create');
333 my $artist = $schema->resultset('Artist')->first;
335 my $cd_result = $artist->create_related('cds', {
337 title => 'TestOneCD1',
340 { title => 'TrackOne' },
341 { title => 'TrackTwo' },
346 isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
347 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
349 my $tracks = $cd_result->tracks;
351 isa_ok( $tracks, 'DBIx::Class::ResultSet', 'Got Expected Tracks ResultSet');
353 foreach my $track ($tracks->all)
355 isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
357 }, 'First create_related pass');
360 my $artist = $schema->resultset('Artist')->first;
362 my $cd_result = $artist->create_related('cds', {
364 title => 'TestOneCD2',
367 { title => 'TrackOne' },
368 { title => 'TrackTwo' },
371 liner_notes => { notes => 'I can haz liner notes?' },
375 isa_ok( $cd_result, 'DBICTest::CD', "Got Good CD Class");
376 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
377 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
379 my $tracks = $cd_result->tracks;
381 isa_ok( $tracks, 'DBIx::Class::ResultSet', "Got Expected Tracks ResultSet");
383 foreach my $track ($tracks->all)
385 isa_ok( $track, 'DBICTest::Track', 'Got Expected Track Class');
387 }, 'second create_related with same arguments');
390 my $cdp = $schema->resultset('CD_to_Producer')->create({
391 cd => { artist => 1, title => 'foo', year => 2000 },
392 producer => { name => 'jorge' }
394 ok($cdp, 'join table record created ok');
395 }, 'create of parents of a record linker table');
398 my $kurt_cobain = { name => 'Kurt Cobain' };
400 my $in_utero = $schema->resultset('CD')->new({
405 $kurt_cobain->{cds} = [ $in_utero ];
408 $schema->resultset('Artist')->populate([ $kurt_cobain ]);
409 } qr/\QFast-path populate() with supplied related objects is not possible/;
412 my $artist = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
414 is($artist->name, 'Kurt Cobain', 'Artist insertion ok');
415 is($artist->cds && $artist->cds->first && $artist->cds->first->title,
416 'In Utero', 'CD insertion ok');
419 ## Create foreign key col obj including PK
420 ## See test 20 in 66relationships.t
422 my $new_cd_hashref = {
424 title => 'Boogie Woogie',
426 artist => { artistid => 17, name => 'king luke' }
429 my $cd = $schema->resultset("CD")->find(1);
431 is($cd->artist->id, 1, 'rel okay');
433 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
434 is($new_cd->artist->id, 17, 'new id retained okay');
435 }, 'Create foreign key col obj including PK');
438 $schema->resultset("CD")->create({
440 title => 'Boogie Wiggle',
442 artist => { artistid => 18, name => 'larry' }
444 }, 'new cd created without clash on related artist');
447 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
448 #$t->cd($t->new_related('cd', { artist => undef } ) );
449 #$t->{_rel_in_storage} = 0;
451 }, qr/DBI Exception.+(?x:
452 \QNOT NULL constraint failed: cd.artist\E
454 \Qcd.artist may not be NULL\E
455 )/s, "Exception propogated properly");
458 $schema->resultset('CD')->create ({
460 name => 'larry', # should already exist
462 title => 'Warble Marble',
465 { producer => { name => 'Cowboy Neal' } },
469 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
470 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
471 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
472 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
473 }, 'Test multi create over many_to_many');