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',
65 isa_ok($artist, 'DBICTest::Artist', 'Created Artist');
66 is($artist->name, 'Fred 2', 'Artist created correctly');
67 is($artist->cds->count, 1, 'One CD created for artist');
68 is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
69 is($artist->cds->first->tags->count, 1, 'One tag created for CD');
70 is($artist->cds->first->tags->first->tag, 'rock', 'Tag created correctly');
72 # Create via update - add a new CD
74 cds => [ $artist->cds,
75 { title => 'Yet another CD',
80 is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
82 my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
84 is($newartist->name, 'Fred 2', 'Retrieved the artist');
88 # nested find_or_create
90 my $newartist2 = $schema->resultset('Artist')->find_or_create({
99 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
103 # multiple same level has_many create
105 my $artist2 = $schema->resultset('Artist')->create({
109 title => 'Music to code by',
115 title => 'Music to code by',
121 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
125 # first create_related pass
127 my $artist = $schema->resultset('Artist')->first;
129 my $cd_result = $artist->create_related('cds', {
131 title => 'TestOneCD1',
145 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
146 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
148 my $tracks = $cd_result->tracks;
150 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
152 foreach my $track ($tracks->all)
154 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
159 # second create_related with same arguments
161 my $artist = $schema->resultset('Artist')->first;
163 my $cd_result = $artist->create_related('cds', {
165 title => 'TestOneCD2',
177 liner_notes => { notes => 'I can haz liner notes?' },
181 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
182 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
183 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
185 my $tracks = $cd_result->tracks;
187 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
189 foreach my $track ($tracks->all)
191 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
196 # create of parents of a record linker table
198 my $cdp = $schema->resultset('CD_to_Producer')->create({
199 cd => { artist => 1, title => 'foo', year => 2000 },
200 producer => { name => 'jorge' }
202 ok($cdp, 'join table record created ok');
208 my $kurt_cobain = { name => 'Kurt Cobain' };
210 my $in_utero = $schema->resultset('CD')->new({
215 $kurt_cobain->{cds} = [ $in_utero ];
218 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
219 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
221 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
222 is($a->cds && $a->cds->first && $a->cds->first->title,
223 'In Utero', 'CD insertion ok');
229 my $pink_floyd = { name => 'Pink Floyd' };
231 my $the_wall = { title => 'The Wall', year => 1979 };
233 $pink_floyd->{cds} = [ $the_wall ];
236 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
237 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
239 is($a->name, 'Pink Floyd', 'Artist insertion ok');
240 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
244 ## Create foreign key col obj including PK
245 ## See test 20 in 66relationships.t
247 my $new_cd_hashref = {
249 title => 'Boogie Woogie',
251 artist => { artistid => 17, name => 'king luke' }
254 my $cd = $schema->resultset("CD")->find(1);
256 is($cd->artist->id, 1, 'rel okay');
258 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
259 is($new_cd->artist->id, 17, 'new id retained okay');
264 $schema->resultset("CD")->create({
266 title => 'Boogie Wiggle',
268 artist => { artistid => 18, name => 'larry' }
271 is($@, '', 'new cd created without clash on related artist');
273 # Make sure exceptions from errors in created rels propogate
275 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
276 #$t->cd($t->new_related('cd', { artist => undef } ) );
277 #$t->{_rel_in_storage} = 0;
280 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
282 # Test multi create over many_to_many
284 $schema->resultset('CD')->create ({
286 name => 'larry', # should already exist
288 title => 'Warble Marble',
291 { producer => { name => 'Cowboy Neal' } },
295 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
296 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
297 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
298 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
301 # and some insane multicreate
302 # (should work, despite the fact that no one will probably use it this way)
304 # first count how many rows do we initially have
306 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
308 # do the crazy create
310 $schema->resultset('CD')->create ({
314 title => 'Greatest hits 1',
317 name => '"Greatest" collections',
334 title => 'Greatest hits 2',
337 name => '"Greatest" collections',
343 # This cd is created via artist so it doesn't know about producers
345 # if we specify 'bob' here things bomb
346 # as the producer attached to Greatest Hits 1 is
347 # already created, but not yet inserted.
348 # Maybe this can be fixed, but things are hairy
351 #{ producer => { name => 'bob' } },
352 { producer => { name => 'paul' } },
361 title => 'Greatest hits 3',
364 name => '"Greatest" collections',
372 title => 'Greatest hits 4',
375 name => '"Greatest" collections2',
384 title => 'Greatest hits 5',
387 name => '"Greatest" collections2',
396 title => 'Greatest hits 6',
399 name => '"Greatest" collections',
410 name => 'lars', # should already exist
411 # even though the artist 'name' is not uniquely constrained
412 # find_or_create will arguably DWIM
414 title => 'Greatest hits 7',
424 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
425 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
426 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
427 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
428 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
430 my $cd_rs = $schema->resultset ('CD')
431 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
432 is ($cd_rs->count, 7, '7 greatest hits created');
434 my $cds_2012 = $cd_rs->search ({ year => 2012});
435 is ($cds_2012->count, 5, '5 CDs created in 2012');
439 { 'tags.tag' => { -in => [qw/A B/] } },
440 { join => 'tags', group_by => 'me.cdid' }
443 'All 10 tags were pairwise distributed between 5 year-2012 CDs'
446 my $paul_prod = $cd_rs->search (
447 { 'producer.name' => 'paul'},
448 { join => { cd_to_producer => 'producer' } }
450 is ($paul_prod->count, 1, 'Paul had 1 production');
451 my $pauls_cd = $paul_prod->single;
452 is ($pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer');
454 $pauls_cd->search_related ('cd_to_producer',
455 { 'producer.name' => 'flemming'},
456 { join => 'producer' }
459 'The second producer is flemming',
462 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
463 is ($kirk_cds, 3, 'Kirk had 3 CDs');
466 { 'cd_to_producer.cd' => { '!=', undef } },
467 { join => 'cd_to_producer' },
470 'Kirk had a producer only on one cd',
473 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
474 is ($lars_cds->count, 3, 'Lars had 3 CDs');
477 { 'cd_to_producer.cd' => undef },
478 { join => 'cd_to_producer' },
481 'Lars always had a producer',
484 $lars_cds->search_related ('cd_to_producer',
485 { 'producer.name' => 'flemming'},
486 { join => 'producer' }
489 'Lars produced 1 CD with flemming',
492 $lars_cds->search_related ('cd_to_producer',
493 { 'producer.name' => 'bob'},
494 { join => 'producer' }
497 'Lars produced 2 CDs with bob',
500 my $bob_prod = $cd_rs->search (
501 { 'producer.name' => 'bob'},
502 { join => { cd_to_producer => 'producer' } }
504 is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
507 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
509 "Bob produced james' only CD",
514 ## test might_have/has_many interactions
515 my $ff = $schema->resultset('ForceForeign');
517 my $thing = $ff->create(
521 name => 'Crazy Frog',
550 isa_ok($thing->artist_1, 'DBICTest::Schema::Artist', 'created might_have artist');
551 is($thing->artist_1->name, 'Crazy Frog');
552 isa_ok($thing->artist_1->cds, 'DBIx::Class::ResultSet', 'created artists cds');
553 is($thing->artist_1->cds->count, 2, 'created two cds for artist');