4 use Test::More qw(no_plan);
8 my $schema = DBICTest->init_schema();
10 my $cd2 = $schema->resultset('CD')->create({ artist =>
11 { name => 'Fred Bloggs' },
16 is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
17 is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
19 my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
21 { title => 'Music to code by',
30 is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
31 is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
32 is($artist->cds->first->tags->count, 2, 'Correct tags count');
35 $artist->update({cds => [ $artist->cds,
36 { title => 'Yet another CD',
41 is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
43 my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
45 is($newartist->name, 'Fred 2', 'Retrieved the artist');
48 my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
50 { title => 'Noah Act',
57 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
59 my $artist2 = $schema->resultset('Artist')->create({
63 title => 'Music to code by',
69 title => 'Music to code by',
75 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
79 my $artist = $schema->resultset('Artist')->first;
81 my $cd_result = $artist->create_related('cds', {
83 title => 'TestOneCD1',
97 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
98 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
100 my $tracks = $cd_result->tracks;
102 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
104 foreach my $track ($tracks->all)
106 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
112 my $artist = $schema->resultset('Artist')->first;
114 my $cd_result = $artist->create_related('cds', {
116 title => 'TestOneCD2',
128 liner_notes => { notes => 'I can haz liner notes?' },
132 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
133 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
134 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
136 my $tracks = $cd_result->tracks;
138 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
140 foreach my $track ($tracks->all)
142 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
146 my $cdp = $schema->resultset('CD_to_Producer')->create({
147 cd => { artist => 1, title => 'foo', year => 2000 },
148 producer => { name => 'jorge' }
151 ok($cdp, 'join table record created ok');
154 my $kurt_cobain = { name => 'Kurt Cobain' };
156 my $in_utero = $schema->resultset('CD')->new({
161 $kurt_cobain->{cds} = [ $in_utero ];
164 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
165 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
167 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
168 is($a->cds && $a->cds->first && $a->cds->first->title,
169 'In Utero', 'CD insertion ok');
173 my $pink_floyd = { name => 'Pink Floyd' };
175 my $the_wall = { title => 'The Wall', year => 1979 };
177 $pink_floyd->{cds} = [ $the_wall ];
180 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
181 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
183 is($a->name, 'Pink Floyd', 'Artist insertion ok');
184 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
187 ## Create foreign key col obj including PK
188 ## See test 20 in 66relationships.t
189 my $new_cd_hashref = {
191 title => 'Boogie Woogie',
193 artist => { artistid => 17, name => 'king luke' }
196 my $cd = $schema->resultset("CD")->find(1);
198 is($cd->artist->id, 1, 'rel okay');
200 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
201 is($new_cd->artist->id, 17, 'new id retained okay');
204 # Test find or create related functionality
205 my $new_artist = $schema->resultset("Artist")->create({ artistid => 18, name => 'larry' });
208 $schema->resultset("CD")->create({
210 title => 'Boogie Wiggle',
212 artist => { artistid => 18, name => 'larry' }
215 is($@, '', 'new cd created without clash on related artist');
217 # Make sure exceptions from errors in created rels propogate
219 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
220 #$t->cd($t->new_related('cd', { artist => undef } ) );
221 #$t->{_rel_in_storage} = 0;
224 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
226 # Test multi create over many_to_many
227 $schema->resultset('CD')->create ({
228 artist => $new_artist,
229 title => 'Warble Marble',
232 { producer => { name => 'Cowboy Neal' } },
236 my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
237 is ($m2m_cd->count, 1, 'One CD row created via M2M create');
238 is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
239 is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
241 # and some insane multicreate
242 # (should work, despite the fact that no one will probably use it this way)
244 # first count how many rows do we have
247 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer/;
249 # do the crazy create
250 $schema->resultset('CD')->create ({
251 artist => $new_artist,
252 title => 'Greatest hits 1',
255 name => '"Greatest" collections',
260 name => 'Dirty Harry',
265 name => 'Dirty Harry himself',
267 title => 'Greatest hits 2',
270 name => '"Greatest" collections',
277 name => 'Dirty Harry himself',
279 title => 'Greatest hits 3',
282 name => '"Greatest" collections',
288 artist => $new_artist,
289 title => 'Greatest hits 4',
299 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
300 is ($schema->resultset ('Genre')->count, $counts->{Genre} + 1, 'One additional genre created');
301 is ($schema->resultset ('Producer')->count, $counts->{Producer} + 1, 'One new producer');
302 is ($schema->resultset ('CD')->count, $counts->{CD} + 4, '4 new CDs');
304 my $harry_cds = $schema->resultset ('Artist')->single ({name => 'Dirty Harry himself'})->cds;
305 is ($harry_cds->count, 2, 'Two CDs created by Harry');
306 ok ($harry_cds->single ({title => 'Greatest hits 2'}), 'First CD name correct');
307 ok ($harry_cds->single ({title => 'Greatest hits 3'}), 'Second CD name correct');
309 my $harry_productions = $schema->resultset ('Producer')->single ({name => 'Dirty Harry'})
310 ->search_related ('producer_to_cd', {})->search_related ('cd', {});
311 is ($harry_productions->count, 4, 'All 4 CDs are produced by Harry');
312 is ($harry_productions->search ({ year => 2012 })->count, 4, 'All 4 CDs have the correct year');
314 my $hits_genre = $schema->resultset ('Genre')->single ({name => '"Greatest" collections'});
315 ok ($hits_genre, 'New genre row found');
316 is ($hits_genre->cds->count, 3, 'Three of the new CDs fall into the new genre');