applied patch from davinch: fix bug with create_multi not inserting non-storage objects
[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
55 CREATE_RELATED1 :{
56
57         my $artist = $schema->resultset('Artist')->first;
58         
59         my $cd_result = $artist->create_related('cds', {
60         
61                 title => 'TestOneCD1',
62                 year => 2007,
63                 tracks => [
64                 
65                         { position=>111,
66                           title => 'TrackOne',
67                         },
68                         { position=>112,
69                           title => 'TrackTwo',
70                         }
71                 ],
72
73         });
74         
75         ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
76         ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
77         
78         my $tracks = $cd_result->tracks;
79         
80         ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
81         
82         foreach my $track ($tracks->all)
83         {
84                 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
85         }
86 }
87
88 CREATE_RELATED2 :{
89
90         my $artist = $schema->resultset('Artist')->first;
91         
92         my $cd_result = $artist->create_related('cds', {
93         
94                 title => 'TestOneCD2',
95                 year => 2007,
96                 tracks => [
97                 
98                         { position=>111,
99                           title => 'TrackOne',
100                         },
101                         { position=>112,
102                           title => 'TrackTwo',
103                         }
104                 ],
105
106     liner_notes => { notes => 'I can haz liner notes?' },
107
108         });
109         
110         ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
111         ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
112   ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
113         
114         my $tracks = $cd_result->tracks;
115         
116         ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
117         
118         foreach my $track ($tracks->all)
119         {
120                 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
121         }
122 }
123
124 my $cdp = $schema->resultset('CD_to_Producer')->create({
125             cd => { artist => 1, title => 'foo', year => 2000 },
126             producer => { name => 'jorge' }
127           });
128
129 ok($cdp, 'join table record created ok');
130
131 SPECIAL_CASE: {
132   my $kurt_cobain = { name => 'Kurt Cobain' };
133
134   my $in_utero = $schema->resultset('CD')->new({
135       title => 'In Utero',
136       year  => 1993
137     });
138
139   $kurt_cobain->{cds} = [ $in_utero ];
140
141
142   $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
143   $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
144
145   is($a->name, 'Kurt Cobain', 'Artist insertion ok');
146   is($a->cds && $a->cds->first && $a->cds->first->title, 
147                   'In Utero', 'CD insertion ok');
148 }
149
150 SPECIAL_CASE2: {
151   my $pink_floyd = { name => 'Pink Floyd' };
152
153   my $the_wall = { title => 'The Wall', year  => 1979 };
154
155   $pink_floyd->{cds} = [ $the_wall ];
156
157
158   $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
159   $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
160
161   is($a->name, 'Pink Floyd', 'Artist insertion ok');
162   is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
163 }