Final set of DBI docfixes
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
CommitLineData
33dd4e80 1use strict;
af2d42c0 2use warnings;
33dd4e80 3
9c6d6d93 4use Test::More qw(no_plan);
33dd4e80 5use lib qw(t/lib);
6use DBICTest;
7
8my $schema = DBICTest->init_schema();
9
33dd4e80 10my $cd2 = $schema->resultset('CD')->create({ artist =>
11 { name => 'Fred Bloggs' },
12 title => 'Some CD',
13 year => 1996
14 });
15
ac8e89d7 16is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
17is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
18
19my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
20 cds => [
21 { title => 'Music to code by',
22 year => 2007,
23 },
24 ],
25 });
26is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
27is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
af2d42c0 28
29# Add a new CD
30$artist->update({cds => [ $artist->cds,
31 { title => 'Yet another CD',
32 year => 2006,
33 },
34 ],
35 });
36is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
37
38my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
39
40is($newartist->name, 'Fred 2', 'Retrieved the artist');
41
3d8ee6ab 42
af2d42c0 43my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
44 cds => [
45 { title => 'Noah Act',
46 year => 2007,
47 },
48 ],
49
50 });
51
52is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
3d8ee6ab 53
370f2ba2 54my $artist2 = $schema->resultset('Artist')->create({
c193d1d2 55 name => 'Fred 3',
56 cds => [
370f2ba2 57 {
c193d1d2 58 title => 'Music to code by',
59 year => 2007,
60 },
61 ],
62 cds_unordered => [
370f2ba2 63 {
c193d1d2 64 title => 'Music to code by',
65 year => 2007,
66 },
67 ]
68 });
69
70is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
3d8ee6ab 71
72CREATE_RELATED1 :{
73
74 my $artist = $schema->resultset('Artist')->first;
75
76 my $cd_result = $artist->create_related('cds', {
77
78 title => 'TestOneCD1',
79 year => 2007,
80 tracks => [
81
82 { position=>111,
83 title => 'TrackOne',
84 },
85 { position=>112,
86 title => 'TrackTwo',
87 }
88 ],
89
90 });
91
92 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
93 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
94
95 my $tracks = $cd_result->tracks;
96
97 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
98
99 foreach my $track ($tracks->all)
100 {
101 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
102 }
103}
104
105CREATE_RELATED2 :{
106
2ec8e594 107 my $artist = $schema->resultset('Artist')->first;
3d8ee6ab 108
2ec8e594 109 my $cd_result = $artist->create_related('cds', {
3d8ee6ab 110
2ec8e594 111 title => 'TestOneCD2',
3d8ee6ab 112 year => 2007,
113 tracks => [
114
115 { position=>111,
116 title => 'TrackOne',
117 },
118 { position=>112,
119 title => 'TrackTwo',
120 }
121 ],
122
9c6d6d93 123 liner_notes => { notes => 'I can haz liner notes?' },
124
3d8ee6ab 125 });
126
127 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
2ec8e594 128 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
9c6d6d93 129 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
3d8ee6ab 130
131 my $tracks = $cd_result->tracks;
132
133 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
134
135 foreach my $track ($tracks->all)
136 {
137 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
138 }
139}
e5dddc05 140
141my $cdp = $schema->resultset('CD_to_Producer')->create({
142 cd => { artist => 1, title => 'foo', year => 2000 },
143 producer => { name => 'jorge' }
144 });
145
146ok($cdp, 'join table record created ok');
2bc3c81e 147
148SPECIAL_CASE: {
149 my $kurt_cobain = { name => 'Kurt Cobain' };
150
151 my $in_utero = $schema->resultset('CD')->new({
152 title => 'In Utero',
153 year => 1993
154 });
155
156 $kurt_cobain->{cds} = [ $in_utero ];
157
158
159 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
160 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
161
162 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
163 is($a->cds && $a->cds->first && $a->cds->first->title,
164 'In Utero', 'CD insertion ok');
165}
166
167SPECIAL_CASE2: {
168 my $pink_floyd = { name => 'Pink Floyd' };
169
170 my $the_wall = { title => 'The Wall', year => 1979 };
171
172 $pink_floyd->{cds} = [ $the_wall ];
173
174
175 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
176 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
177
178 is($a->name, 'Pink Floyd', 'Artist insertion ok');
179 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
180}
e02b9964 181
182## Create foreign key col obj including PK
183## See test 20 in 66relationships.t
184my $new_cd_hashref = {
185 cdid => 27,
186 title => 'Boogie Woogie',
187 year => '2007',
188 artist => { artistid => 17, name => 'king luke' }
189 };
190
191my $cd = $schema->resultset("CD")->find(1);
192
e02b9964 193is($cd->artist->id, 1, 'rel okay');
194
195my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
29cfcca2 196is($new_cd->artist->id, 17, 'new id retained okay');
f10ac17d 197
198
38c03c20 199# Test find or create related functionality
200my $new_artist = $schema->resultset("Artist")->create({ artistid => 18, name => 'larry' });
6ede9177 201
202eval {
203 $schema->resultset("CD")->create({
38c03c20 204 cdid => 28,
370f2ba2 205 title => 'Boogie Wiggle',
38c03c20 206 year => '2007',
207 artist => { artistid => 18, name => 'larry' }
6ede9177 208 });
38c03c20 209};
6ede9177 210is($@, '', 'new cd created without clash on related artist');
38c03c20 211
f10ac17d 212# Make sure exceptions from errors in created rels propogate
213eval {
370f2ba2 214 my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
215 #$t->cd($t->new_related('cd', { artist => undef } ) );
216 #$t->{_rel_in_storage} = 0;
f10ac17d 217 $t->insert;
218};
219like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
bbbf67eb 220
221# Test multi create over many_to_many
76b8cf98 222$schema->resultset('CD')->create ({
223 artist => $new_artist,
224 title => 'Warble Marble',
225 year => '2009',
226 cd_to_producer => [
227 { producer => { name => 'Cowboy Neal' } },
228 ],
229});
230
231my $m2m_cd = $schema->resultset('CD')->search ({ title => 'Warble Marble'});
232is ($m2m_cd->count, 1, 'One CD row created via M2M create');
233is ($m2m_cd->first->producers->count, 1, 'CD row created with one producer');
234is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
235
236# and some insane multicreate
237# (should work, despite the fact that no one will probably use it this way)
238
239# first count how many rows do we have
240
241my $counts;
242$counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer/;
243
244# do the crazy create
245$schema->resultset('CD')->create ({
246 artist => $new_artist,
247 title => 'Greatest hits 1',
248 year => '2012',
249 genre => {
250 name => '"Greatest" collections',
251 },
252 cd_to_producer => [
253 {
254 producer => {
255 name => 'Dirty Harry',
256 producer_to_cd => [
257 {
258 cd => {
259 artist => {
260 name => 'Dirty Harry himself',
261 },
262 title => 'Greatest hits 2',
263 year => 2012,
264 genre => {
265 name => '"Greatest" collections',
266 },
267 },
268 },
269 {
270 cd => {
271 artist => {
272 name => 'Dirty Harry himself',
273 },
274 title => 'Greatest hits 3',
275 year => 2012,
276 genre => {
277 name => '"Greatest" collections',
278 },
279 },
280 },
281 {
282 cd => {
283 artist => $new_artist,
284 title => 'Greatest hits 4',
285 year => 2012,
286 },
287 },
288 ],
289 },
290 },
291 ],
292});
293
294is ($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
295is ($schema->resultset ('Genre')->count, $counts->{Genre} + 1, 'One additional genre created');
296is ($schema->resultset ('Producer')->count, $counts->{Producer} + 1, 'One new producer');
297is ($schema->resultset ('CD')->count, $counts->{CD} + 4, '4 new CDs');
298
299my $harry_cds = $schema->resultset ('Artist')->single ({name => 'Dirty Harry himself'})->cds;
300is ($harry_cds->count, 2, 'Two CDs created by Harry');
301ok ($harry_cds->single ({title => 'Greatest hits 2'}), 'First CD name correct');
302ok ($harry_cds->single ({title => 'Greatest hits 3'}), 'Second CD name correct');
303
304my $harry_productions = $schema->resultset ('Producer')->single ({name => 'Dirty Harry'})
305 ->search_related ('producer_to_cd', {})->search_related ('cd', {});
306is ($harry_productions->count, 4, 'All 4 CDs are produced by Harry');
307is ($harry_productions->search ({ year => 2012 })->count, 4, 'All 4 CDs have the correct year');
308
309my $hits_genre = $schema->resultset ('Genre')->single ({name => '"Greatest" collections'});
310ok ($hits_genre, 'New genre row found');
311is ($hits_genre->cds->count, 3, 'Three of the new CDs fall into the new genre');