Commit | Line | Data |
33dd4e80 |
1 | use strict; |
af2d42c0 |
2 | use warnings; |
33dd4e80 |
3 | |
9c6d6d93 |
4 | use Test::More qw(no_plan); |
33dd4e80 |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | |
8 | my $schema = DBICTest->init_schema(); |
9 | |
33dd4e80 |
10 | my $cd2 = $schema->resultset('CD')->create({ artist => |
11 | { name => 'Fred Bloggs' }, |
12 | title => 'Some CD', |
13 | year => 1996 |
14 | }); |
15 | |
ac8e89d7 |
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'); |
af2d42c0 |
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 | |
3d8ee6ab |
42 | |
af2d42c0 |
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'); |
3d8ee6ab |
53 | |
c193d1d2 |
54 | my $artist2 = $schema->resultset('Artist')->create({ artistid => 1000, |
55 | name => 'Fred 3', |
56 | cds => [ |
57 | { artist => 1000, |
58 | title => 'Music to code by', |
59 | year => 2007, |
60 | }, |
61 | ], |
62 | cds_unordered => [ |
63 | { artist => 1000, |
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'); |
3d8ee6ab |
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 | |
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 | |
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'); |
2bc3c81e |
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 | } |
e02b9964 |
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 | |
e02b9964 |
193 | is($cd->artist->id, 1, 'rel okay'); |
194 | |
195 | my $new_cd = $schema->resultset("CD")->create($new_cd_hashref); |
29cfcca2 |
196 | is($new_cd->artist->id, 17, 'new id retained okay'); |
f10ac17d |
197 | |
198 | |
199 | # Make sure exceptions from errors in created rels propogate |
200 | eval { |
201 | my $t = $schema->resultset("Track")->new({}); |
202 | $t->cd($t->new_related('cd', { artist => undef } ) ); |
203 | $t->{_rel_in_storage} = 0; |
204 | $t->insert; |
205 | }; |
206 | like($@, qr/cd.artist may not be NULL/, "Exception propogated properly"); |