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
284 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
286 # do the crazy create
288 $schema->resultset('CD')->create ({
292 title => 'Greatest hits 1',
295 name => '"Greatest" collections',
304 name => 'Dirty Harry',
309 name => 'Dirty Harry himself',
312 title => 'Greatest hits 3',
315 name => '"Greatest" collections',
324 title => 'Greatest hits 2',
327 name => '"Greatest" collections',
338 name => 'larry', # should already exist
340 title => 'Greatest hits 4',
350 is ($schema->resultset ('Artist')->count, $counts->{Artist} + 1, 'One new artists created'); # even though the 'name' is not uniquely constrained find_or_create will arguably DWIM
351 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 1, 'One additional genre created');
352 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 1, 'One new producer');
353 is ($schema->resultset ('CD')->count, $counts->{CD} + 4, '4 new CDs');
354 is ($schema->resultset ('Tag')->count, $counts->{Tag} + 6, '6 new Tags');
356 my $harry_cds = $schema->resultset ('Artist')->single ({name => 'Dirty Harry himself'})->cds;
357 is ($harry_cds->count, 2, 'Two CDs created by Harry');
358 ok ($harry_cds->single ({title => 'Greatest hits 2'}), 'First CD name correct');
359 ok ($harry_cds->single ({title => 'Greatest hits 3'}), 'Second CD name correct');
361 my $harry_productions = $schema->resultset ('Producer')->single ({name => 'Dirty Harry'})
362 ->search_related ('producer_to_cd', {})->search_related ('cd', {});
363 is ($harry_productions->count, 4, 'All 4 CDs are produced by Harry');
364 is ($harry_productions->search ({ year => 2012 })->count, 4, 'All 4 CDs have the correct year');
366 my $hits_genre = $schema->resultset ('Genre')->single ({name => '"Greatest" collections'});
367 ok ($hits_genre, 'New genre row found');
368 is ($hits_genre->cds->count, 3, 'Three of the new CDs fall into the new genre');
370 my $a_tags = $schema->resultset('Tag')->search({ tag => 'A'});
371 my $b_tags = $schema->resultset('Tag')->search({ tag => 'A'});
372 is ($a_tags->count, 3, '3 A tags');
373 is ($a_tags->count, 3, '3 B tags');
375 my $cds_with_ab = $schema->resultset('CD')
376 ->search({ 'tags.tag' => { -in => [qw/A B/] } }, { join => 'tags', group_by => 'me.cdid' } );
377 is ($cds_with_ab->count, 3, '6 tags were pairwise distributed between 3 CDs');