10 my $schema = DBICTest->init_schema();
12 # simple create + belongs_to
14 my $cd2 = $schema->resultset('CD')->create({
22 isa_ok($cd2, 'DBICTest::CD', 'Created CD object');
23 isa_ok($cd2->artist, 'DBICTest::Artist', 'Created related Artist');
24 is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
28 # create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
30 my $artist = $schema->resultset('Artist')->create(
33 { title => 'Music to code by',
42 isa_ok($artist, 'DBICTest::Artist', 'Created Artist');
43 is($artist->name, 'Fred 2', 'Artist created correctly');
44 is($artist->cds->count, 1, 'One CD created for artist');
45 is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
46 is($artist->cds->first->tags->count, 1, 'One tag created for CD');
47 is($artist->cds->first->tags->first->tag, 'rock', 'Tag created correctly');
49 # Create via update - add a new CD
51 cds => [ $artist->cds,
52 { title => 'Yet another CD',
57 is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
59 my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
61 is($newartist->name, 'Fred 2', 'Retrieved the artist');
65 # nested find_or_create
67 my $newartist2 = $schema->resultset('Artist')->find_or_create({
76 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
80 # multiple same level has_many create
82 my $artist2 = $schema->resultset('Artist')->create({
86 title => 'Music to code by',
92 title => 'Music to code by',
98 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
102 # first create_related pass
104 my $artist = $schema->resultset('Artist')->first;
106 my $cd_result = $artist->create_related('cds', {
108 title => 'TestOneCD1',
122 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
123 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
125 my $tracks = $cd_result->tracks;
127 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
129 foreach my $track ($tracks->all)
131 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
136 # second create_related with same arguments
138 my $artist = $schema->resultset('Artist')->first;
140 my $cd_result = $artist->create_related('cds', {
142 title => 'TestOneCD2',
154 liner_notes => { notes => 'I can haz liner notes?' },
158 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
159 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
160 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
162 my $tracks = $cd_result->tracks;
164 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
166 foreach my $track ($tracks->all)
168 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
173 # create of parents of a record linker table
175 my $cdp = $schema->resultset('CD_to_Producer')->create({
176 cd => { artist => 1, title => 'foo', year => 2000 },
177 producer => { name => 'jorge' }
179 ok($cdp, 'join table record created ok');
185 my $kurt_cobain = { name => 'Kurt Cobain' };
187 my $in_utero = $schema->resultset('CD')->new({
192 $kurt_cobain->{cds} = [ $in_utero ];
195 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
196 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
198 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
199 is($a->cds && $a->cds->first && $a->cds->first->title,
200 'In Utero', 'CD insertion ok');
206 my $pink_floyd = { name => 'Pink Floyd' };
208 my $the_wall = { title => 'The Wall', year => 1979 };
210 $pink_floyd->{cds} = [ $the_wall ];
213 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
214 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
216 is($a->name, 'Pink Floyd', 'Artist insertion ok');
217 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
221 ## Create foreign key col obj including PK
222 ## See test 20 in 66relationships.t
224 my $new_cd_hashref = {
226 title => 'Boogie Woogie',
228 artist => { artistid => 17, name => 'king luke' }
231 my $cd = $schema->resultset("CD")->find(1);
233 is($cd->artist->id, 1, 'rel okay');
235 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
236 is($new_cd->artist->id, 17, 'new id retained okay');
241 $schema->resultset("CD")->create({
243 title => 'Boogie Wiggle',
245 artist => { artistid => 18, name => 'larry' }
248 is($@, '', 'new cd created without clash on related artist');
250 # Make sure exceptions from errors in created rels propogate
252 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
253 #$t->cd($t->new_related('cd', { artist => undef } ) );
254 #$t->{_rel_in_storage} = 0;
257 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
259 # Test multi create over many_to_many
261 $schema->resultset('CD')->create ({
263 name => 'larry', # should already exist
265 title => 'Warble Marble',
268 { producer => { name => 'Cowboy Neal' } },
272 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
273 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
274 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
275 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
278 # and some insane multicreate
279 # (should work, despite the fact that no one will probably use it this way)
281 # first count how many rows do we initially have
283 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
285 # do the crazy create
287 $schema->resultset('CD')->create ({
291 title => 'Greatest hits 1',
294 name => '"Greatest" collections',
311 title => 'Greatest hits 2',
314 name => '"Greatest" collections',
320 # This cd is created via artist so it doesn't know about producers
322 # if we specify 'bob' here things bomb
323 # as the producer attached to Greatest Hits 1 is
324 # already created, but not yet inserted.
325 # Maybe this can be fixed, but things are hairy
328 #{ producer => { name => 'bob' } },
329 { producer => { name => 'paul' } },
338 title => 'Greatest hits 3',
341 name => '"Greatest" collections',
349 title => 'Greatest hits 4',
352 name => '"Greatest" collections2',
361 title => 'Greatest hits 5',
364 name => '"Greatest" collections2',
373 title => 'Greatest hits 6',
376 name => '"Greatest" collections',
387 name => 'lars', # should already exist
388 # even though the artist 'name' is not uniquely constrained
389 # find_or_create will arguably DWIM
391 title => 'Greatest hits 7',
401 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
402 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
403 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
404 is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
405 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
407 my $cd_rs = $schema->resultset ('CD')
408 ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
409 is ($cd_rs->count, 7, '7 greatest hits created');
411 my $cds_2012 = $cd_rs->search ({ year => 2012});
412 is ($cds_2012->count, 5, '5 CDs created in 2012');
416 { 'tags.tag' => { -in => [qw/A B/] } },
417 { join => 'tags', group_by => 'me.cdid' }
420 '10 tags were pairwise distributed between 5 CDs'
423 my $paul_prod = $cd_rs->search (
424 { 'producer.name' => 'paul'},
425 { join => { cd_to_producer => 'producer' } }
427 is ($paul_prod->count, 1, 'Paul had 1 production');
428 my $pauls_cd = $paul_prod->single;
429 is ($pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer');
431 $pauls_cd->search_related ('cd_to_producer',
432 { 'producer.name' => 'flemming'},
433 { join => 'producer' }
436 'The second producer is flemming',
439 my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
440 is ($kirk_cds, 3, 'Kirk had 3 CDs');
443 { 'cd_to_producer.cd' => { '!=', undef } },
444 { join => 'cd_to_producer' },
447 'Kirk had a producer only on one cd',
450 my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
451 is ($lars_cds->count, 3, 'Lars had 3 CDs');
454 { 'cd_to_producer.cd' => undef },
455 { join => 'cd_to_producer' },
458 'Lars always had a producer',
461 $lars_cds->search_related ('cd_to_producer',
462 { 'producer.name' => 'flemming'},
463 { join => 'producer' }
466 'Lars produced 1 CD with flemming',
469 $lars_cds->search_related ('cd_to_producer',
470 { 'producer.name' => 'bob'},
471 { join => 'producer' }
474 'Lars produced 2 CDs with bob',
477 my $bob_prod = $cd_rs->search (
478 { 'producer.name' => 'bob'},
479 { join => { cd_to_producer => 'producer' } }
481 is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
484 $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
486 "Bob prpdoced james' only CD",