11 my $schema = DBICTest->init_schema();
13 # simple create + parent (the stuff $rs belongs_to)
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');
29 # same as above but the child and parent have no values,
30 # except for an explicit parent pk
32 my $bm_rs = $schema->resultset('Bookmark');
33 my $bookmark = $bm_rs->create({
39 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
40 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
43 { 'link.title' => $bookmark->link->title },
47 'Bookmark and link made it to the DB',
52 # create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
54 my $artist = $schema->resultset('Artist')->first;
55 my $cd = $artist->create_related (cds => {
56 title => 'Music to code by',
63 isa_ok($cd, 'DBICTest::CD', 'Created CD');
64 is($cd->title, 'Music to code by', 'CD created correctly');
65 is($cd->tags->count, 1, 'One tag created for CD');
66 is($cd->tags->first->tag, 'rock', 'Tag created correctly');
73 # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
74 $schema->resultset('Artist')->first->update({
76 { title => 'Yet another CD',
82 qr/Recursive update is not supported over relationships of type multi/,
83 'create via update of multi relationships throws an exception'
86 # Create m2m while originating in the linker table
88 my $artist = $schema->resultset('Artist')->first;
89 my $c2p = $schema->resultset('CD_to_Producer')->create ({
92 title => 'Bad investment',
95 { position => 1, title => 'Just buy' },
96 { position => 2, title => 'Why did we do it' },
97 { position => 3, title => 'Burn baby burn' },
101 name => 'Lehman Bros.',
105 isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created');
106 my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' });
107 isa_ok ($prod, 'DBICTest::Producer', 'Producer row found');
108 is ($prod->cds->count, 1, 'Producer has one production');
109 my $cd = $prod->cds->first;
110 is ($cd->title, 'Bad investment', 'CD created correctly');
111 is ($cd->tracks->count, 3, 'CD has 3 tracks');
116 # create over > 1 levels of might_have (A => { might_have => { B => has_many => C } } )
118 my $artist = $schema->resultset('Artist')->first;
119 my $cd = $schema->resultset('CD')->create ({
121 title => 'Music to code by at night',
126 title => 'Off by one again',
130 title => 'The dereferencer',
134 title => 'Was that a null (Single)',
136 { title => 'The dereferencer', position => 1 },
137 { title => 'The dereferencer II', position => 2 },
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, 1, 'One producer created with the single cd');
167 is ($single->cd_to_producer->first->producer->name, 'K&R', 'Producer name correct');
172 local $TODO = "Todoify for multicreate branch";
173 # test might_have again but with a PK == FK in the middle (obviously not specified)
175 my $artist = $schema->resultset('Artist')->first;
176 my $cd = $schema->resultset('CD')->create ({
178 title => 'Music to code by at twilight',
182 { name => 'recursive descent' },
183 { name => 'tail packing' },
188 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
189 is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
190 isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
192 # this test might look weird, but it failed at one point, keep it there
193 my $art_obj = $cd->artwork;
194 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
195 is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
197 [ sort $art_obj->images->get_column ('name')->all ],
198 [ 'recursive descent', 'tail packing' ],
199 'Images named correctly in objects',
203 my $artwork = $schema->resultset('Artwork')->search (
204 { 'cd.title' => 'Music to code by at twilight' },
208 is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
211 [ sort $artwork->images->get_column ('name')->all ],
212 [ 'recursive descent', 'tail packing' ],
213 'Images named correctly after search',
218 # test might_have again but with just a PK and FK (neither specified) in the mid-table
220 my $cd = $schema->resultset('CD')->first;
221 my $track = $schema->resultset ('Track')->create ({
227 { text => 'The color black' },
228 { text => 'The colour black' },
233 isa_ok ($track, 'DBICTest::Track', 'Main track object created');
234 is ($track->title, 'Black', 'Correct track title');
235 isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
237 # this test might look weird, but it was failing at one point, keep it there
238 my $lyric_obj = $track->lyrics;
239 ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
240 ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
241 is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
243 [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
244 [ 'The color black', 'The colour black' ],
245 'Lyrics text in objects matches',
249 my $lyric = $schema->resultset('Lyrics')->search (
250 { 'track.title' => 'Black' },
254 is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
257 [ sort $lyric->lyric_versions->get_column ('text')->all ],
258 [ 'The color black', 'The colour black' ],
259 'Lyrics text via search matches',
265 # nested find_or_create
267 my $newartist2 = $schema->resultset('Artist')->find_or_create({
276 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
280 # multiple same level has_many create
282 my $artist2 = $schema->resultset('Artist')->create({
286 title => 'Music to code by',
292 title => 'Music to code by',
298 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
302 # first create_related pass
304 my $artist = $schema->resultset('Artist')->first;
306 my $cd_result = $artist->create_related('cds', {
308 title => 'TestOneCD1',
322 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
323 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
325 my $tracks = $cd_result->tracks;
327 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
329 foreach my $track ($tracks->all)
331 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
336 # second create_related with same arguments
338 my $artist = $schema->resultset('Artist')->first;
340 my $cd_result = $artist->create_related('cds', {
342 title => 'TestOneCD2',
354 liner_notes => { notes => 'I can haz liner notes?' },
358 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
359 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
360 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
362 my $tracks = $cd_result->tracks;
364 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
366 foreach my $track ($tracks->all)
368 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
373 # create of parents of a record linker table
375 my $cdp = $schema->resultset('CD_to_Producer')->create({
376 cd => { artist => 1, title => 'foo', year => 2000 },
377 producer => { name => 'jorge' }
379 ok($cdp, 'join table record created ok');
385 my $kurt_cobain = { name => 'Kurt Cobain' };
387 my $in_utero = $schema->resultset('CD')->new({
392 $kurt_cobain->{cds} = [ $in_utero ];
395 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
396 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
398 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
399 is($a->cds && $a->cds->first && $a->cds->first->title,
400 'In Utero', 'CD insertion ok');
406 my $pink_floyd = { name => 'Pink Floyd' };
408 my $the_wall = { title => 'The Wall', year => 1979 };
410 $pink_floyd->{cds} = [ $the_wall ];
413 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
414 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
416 is($a->name, 'Pink Floyd', 'Artist insertion ok');
417 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
421 ## Create foreign key col obj including PK
422 ## See test 20 in 66relationships.t
424 my $new_cd_hashref = {
426 title => 'Boogie Woogie',
428 artist => { artistid => 17, name => 'king luke' }
431 my $cd = $schema->resultset("CD")->find(1);
433 is($cd->artist->id, 1, 'rel okay');
435 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
436 is($new_cd->artist->id, 17, 'new id retained okay');
441 $schema->resultset("CD")->create({
443 title => 'Boogie Wiggle',
445 artist => { artistid => 18, name => 'larry' }
448 is($@, '', 'new cd created without clash on related artist');
450 # Make sure exceptions from errors in created rels propogate
452 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
453 #$t->cd($t->new_related('cd', { artist => undef } ) );
454 #$t->{_rel_in_storage} = 0;
457 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
459 # Test multi create over many_to_many
461 $schema->resultset('CD')->create ({
463 name => 'larry', # should already exist
465 title => 'Warble Marble',
468 { producer => { name => 'Cowboy Neal' } },
472 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
473 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
474 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
475 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');