10 my $schema = DBICTest->init_schema();
12 # simple create + parent (the stuff $rs belongs_to)
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');
28 # same as above but the child and parent have no values,
29 # except for an explicit parent pk
31 my $bm_rs = $schema->resultset('Bookmark');
32 my $bookmark = $bm_rs->create({
38 isa_ok($bookmark, 'DBICTest::Bookmark', 'Created Bookrmark object');
39 isa_ok($bookmark->link, 'DBICTest::Link', 'Created related Link');
42 { 'link.title' => $bookmark->link->title },
46 'Bookmark and link made it to the DB',
51 # create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
53 my $artist = $schema->resultset('Artist')->create(
56 { title => 'Music to code by',
66 isa_ok($artist, 'DBICTest::Artist', 'Created Artist');
67 is($artist->name, 'Fred 2', 'Artist created correctly');
68 is($artist->cds->count, 1, 'One CD created for artist');
69 is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
70 is($artist->cds->first->tags->count, 1, 'One tag created for CD');
71 is($artist->cds->first->tags->first->tag, 'rock', 'Tag created correctly');
73 # Create via update - add a new CD
75 cds => [ $artist->cds,
76 { title => 'Yet another CD',
81 is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
83 my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
85 is($newartist->name, 'Fred 2', 'Retrieved the artist');
89 # create over > 1 levels of might_have (A => { might_have => { B => has_many => C } } )
91 my $artist = $schema->resultset('Artist')->first;
92 my $cd = $schema->resultset('CD')->create ({
94 title => 'Music to code by at night',
99 title => 'Off by one again',
103 title => 'The dereferencer',
107 title => 'Was that a null (Single)',
109 { title => 'The dereferencer', position => 1 },
110 { title => 'The dereferencer II', position => 2 },
124 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
125 is ($cd->title, 'Music to code by at night', 'Correct CD title');
126 is ($cd->tracks->count, 2, 'Two tracks on main CD');
128 my ($t1, $t2) = $cd->tracks->all;
129 is ($t1->title, 'Off by one again', 'Correct 1st track name');
130 is ($t1->cd_single, undef, 'No single for 1st track');
131 is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
132 isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
134 my $single = $t2->cd_single;
135 is ($single->tracks->count, 2, 'Two tracks on single CD');
136 is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
137 is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
139 is ($single->cd_to_producer->count, 1, 'One producer created with the single cd');
140 is ($single->cd_to_producer->first->producer->name, 'K&R', 'Producer name correct');
144 # nested find_or_create
146 my $newartist2 = $schema->resultset('Artist')->find_or_create({
155 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
159 # multiple same level has_many create
161 my $artist2 = $schema->resultset('Artist')->create({
165 title => 'Music to code by',
171 title => 'Music to code by',
177 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
181 # first create_related pass
183 my $artist = $schema->resultset('Artist')->first;
185 my $cd_result = $artist->create_related('cds', {
187 title => 'TestOneCD1',
201 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
202 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
204 my $tracks = $cd_result->tracks;
206 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
208 foreach my $track ($tracks->all)
210 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
215 # second create_related with same arguments
217 my $artist = $schema->resultset('Artist')->first;
219 my $cd_result = $artist->create_related('cds', {
221 title => 'TestOneCD2',
233 liner_notes => { notes => 'I can haz liner notes?' },
237 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
238 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
239 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
241 my $tracks = $cd_result->tracks;
243 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
245 foreach my $track ($tracks->all)
247 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
252 # create of parents of a record linker table
254 my $cdp = $schema->resultset('CD_to_Producer')->create({
255 cd => { artist => 1, title => 'foo', year => 2000 },
256 producer => { name => 'jorge' }
258 ok($cdp, 'join table record created ok');
264 my $kurt_cobain = { name => 'Kurt Cobain' };
266 my $in_utero = $schema->resultset('CD')->new({
271 $kurt_cobain->{cds} = [ $in_utero ];
274 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
275 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
277 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
278 is($a->cds && $a->cds->first && $a->cds->first->title,
279 'In Utero', 'CD insertion ok');
285 my $pink_floyd = { name => 'Pink Floyd' };
287 my $the_wall = { title => 'The Wall', year => 1979 };
289 $pink_floyd->{cds} = [ $the_wall ];
292 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
293 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
295 is($a->name, 'Pink Floyd', 'Artist insertion ok');
296 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
300 ## Create foreign key col obj including PK
301 ## See test 20 in 66relationships.t
303 my $new_cd_hashref = {
305 title => 'Boogie Woogie',
307 artist => { artistid => 17, name => 'king luke' }
310 my $cd = $schema->resultset("CD")->find(1);
312 is($cd->artist->id, 1, 'rel okay');
314 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
315 is($new_cd->artist->id, 17, 'new id retained okay');
320 $schema->resultset("CD")->create({
322 title => 'Boogie Wiggle',
324 artist => { artistid => 18, name => 'larry' }
327 is($@, '', 'new cd created without clash on related artist');
329 # Make sure exceptions from errors in created rels propogate
331 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
332 #$t->cd($t->new_related('cd', { artist => undef } ) );
333 #$t->{_rel_in_storage} = 0;
336 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
338 # Test multi create over many_to_many
340 $schema->resultset('CD')->create ({
342 name => 'larry', # should already exist
344 title => 'Warble Marble',
347 { producer => { name => 'Cowboy Neal' } },
351 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
352 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
353 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
354 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
357 # and some insane multicreate
358 # (should work, despite the fact that no one will probably use it this way)
360 # first count how many rows do we initially have
362 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
364 # do the crazy create
366 $schema->resultset('CD')->create ({
370 title => 'Greatest hits 1',
373 name => '"Greatest" collections',
390 title => 'Greatest hits 2',
393 name => '"Greatest" collections',
399 # This cd is created via artist so it doesn't know about producers
401 # if we specify 'bob' here things bomb
402 # as the producer attached to Greatest Hits 1 is
403 # already created, but not yet inserted.
404 # Maybe this can be fixed, but things are hairy
407 #{ producer => { name => 'bob' } },
408 { producer => { name => 'paul' } },
417 title => 'Greatest hits 3',
420 name => '"Greatest" collections',
428 title => 'Greatest hits 4',
431 name => '"Greatest" collections2',
440 title => 'Greatest hits 5',
443 name => '"Greatest" collections2',
452 title => 'Greatest hits 6',
455 name => '"Greatest" collections',
466 name => 'lars', # should already exist
467 # even though the artist 'name' is not uniquely constrained
468 # find_or_create will arguably DWIM
470 title => 'Greatest hits 7',
480 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
481 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
482 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
483 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
484 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
486 my $cd_rs = $schema->resultset ('CD')
487 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
488 is ($cd_rs->count, 7, '7 greatest hits created');
490 my $cds_2012 = $cd_rs->search ({ year => 2012});
491 is ($cds_2012->count, 5, '5 CDs created in 2012');
495 { 'tags.tag' => { -in => [qw/A B/] } },
496 { join => 'tags', group_by => 'me.cdid' }
499 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
502 my $paul_prod = $cd_rs->search (
503 { 'producer.name' => 'paul'},
504 { join => { cd_to_producer => 'producer' } }
506 is ($paul_prod->count, 1, 'Paul had 1 production');
507 my $pauls_cd = $paul_prod->single;
508 is ($pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer');
510 $pauls_cd->search_related ('cd_to_producer',
511 { 'producer.name' => 'flemming'},
512 { join => 'producer' }
515 'The second producer is flemming',
518 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
519 is ($kirk_cds, 3, 'Kirk had 3 CDs');
522 { 'cd_to_producer.cd' => { '!=', undef } },
523 { join => 'cd_to_producer' },
526 'Kirk had a producer only on one cd',
529 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
530 is ($lars_cds->count, 3, 'Lars had 3 CDs');
533 { 'cd_to_producer.cd' => undef },
534 { join => 'cd_to_producer' },
537 'Lars always had a producer',
540 $lars_cds->search_related ('cd_to_producer',
541 { 'producer.name' => 'flemming'},
542 { join => 'producer' }
545 'Lars produced 1 CD with flemming',
548 $lars_cds->search_related ('cd_to_producer',
549 { 'producer.name' => 'bob'},
550 { join => 'producer' }
553 'Lars produced 2 CDs with bob',
556 my $bob_prod = $cd_rs->search (
557 { 'producer.name' => 'bob'},
558 { join => { cd_to_producer => 'producer' } }
560 is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
563 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
565 "Bob produced james' only CD",