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