fix related resultsets and multi-create
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
1 use strict;
2 use warnings;
3
4 use Test::More qw(no_plan);
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 my $cd2 = $schema->resultset('CD')->create({ artist => 
11                                    { name => 'Fred Bloggs' },
12                                    title => 'Some CD',
13                                    year => 1996
14                                  });
15
16 is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
17 is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
18
19 my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
20                                                      cds => [
21                                                              { title => 'Music to code by',
22                                                                year => 2007,
23                                                              },
24                                                              ],
25                                                      });
26 is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
27 is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
28
29 # Add a new CD
30 $artist->update({cds => [ $artist->cds, 
31                           { title => 'Yet another CD',
32                             year => 2006,
33                           },
34                         ],
35                 });
36 is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
37
38 my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
39
40 is($newartist->name, 'Fred 2', 'Retrieved the artist');
41
42
43 my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
44                                                                 cds => [
45                                                                         { title => 'Noah Act',
46                                                                           year => 2007,
47                                                                         },
48                                                                        ],
49
50                                                               });
51
52 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
53
54 my $artist2 = $schema->resultset('Artist')->create({
55                                                     name => 'Fred 3',
56                                                      cds => [
57                                                              {
58                                                                title => 'Music to code by',
59                                                                year => 2007,
60                                                              },
61                                                              ],
62                                                     cds_unordered => [
63                                                              {
64                                                                title => 'Music to code by',
65                                                                year => 2007,
66                                                              },
67                                                              ]
68                                                      });
69
70 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
71
72 CREATE_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
105 CREATE_RELATED2 :{
106
107         my $artist = $schema->resultset('Artist')->first;
108         
109         my $cd_result = $artist->create_related('cds', {
110         
111                 title => 'TestOneCD2',
112                 year => 2007,
113                 tracks => [
114                 
115                         { position=>111,
116                           title => 'TrackOne',
117                         },
118                         { position=>112,
119                           title => 'TrackTwo',
120                         }
121                 ],
122
123     liner_notes => { notes => 'I can haz liner notes?' },
124
125         });
126         
127         ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
128         ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
129   ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
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 }
140
141 my $cdp = $schema->resultset('CD_to_Producer')->create({
142             cd => { artist => 1, title => 'foo', year => 2000 },
143             producer => { name => 'jorge' }
144           });
145
146 ok($cdp, 'join table record created ok');
147
148 SPECIAL_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
167 SPECIAL_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 }
181
182 ## Create foreign key col obj including PK
183 ## See test 20 in 66relationships.t
184 my $new_cd_hashref = { 
185               cdid => 27, 
186                title => 'Boogie Woogie', 
187               year => '2007', 
188               artist => { artistid => 17, name => 'king luke' }
189              };
190
191 my $cd = $schema->resultset("CD")->find(1);
192
193 is($cd->artist->id, 1, 'rel okay');
194
195 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
196 is($new_cd->artist->id, 17, 'new id retained okay');
197
198
199 # Test find or create related functionality
200 my $new_artist = $schema->resultset("Artist")->create({ artistid => 18, name => 'larry' });
201
202 eval {
203         $schema->resultset("CD")->create({ 
204               cdid => 28, 
205               title => 'Boogie Wiggle', 
206               year => '2007', 
207               artist => { artistid => 18, name => 'larry' }
208              });
209 };
210 is($@, '', 'new cd created without clash on related artist');
211
212 # Make sure exceptions from errors in created rels propogate
213 eval {
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;
217     $t->insert;
218 };
219 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");