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 | |
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 | |
2ec8e594 |
90 | my $artist = $schema->resultset('Artist')->first; |
3d8ee6ab |
91 | |
2ec8e594 |
92 | my $cd_result = $artist->create_related('cds', { |
3d8ee6ab |
93 | |
2ec8e594 |
94 | title => 'TestOneCD2', |
3d8ee6ab |
95 | year => 2007, |
96 | tracks => [ |
97 | |
98 | { position=>111, |
99 | title => 'TrackOne', |
100 | }, |
101 | { position=>112, |
102 | title => 'TrackTwo', |
103 | } |
104 | ], |
105 | |
9c6d6d93 |
106 | liner_notes => { notes => 'I can haz liner notes?' }, |
107 | |
3d8ee6ab |
108 | }); |
109 | |
110 | ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class"); |
2ec8e594 |
111 | ok( $cd_result->title eq "TestOneCD2", "Got Expected Title"); |
9c6d6d93 |
112 | ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes'); |
3d8ee6ab |
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 | } |
e5dddc05 |
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'); |
2bc3c81e |
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 | } |
e02b9964 |
164 | |
165 | ## Create foreign key col obj including PK |
166 | ## See test 20 in 66relationships.t |
167 | my $new_cd_hashref = { |
168 | cdid => 27, |
169 | title => 'Boogie Woogie', |
170 | year => '2007', |
171 | artist => { artistid => 17, name => 'king luke' } |
172 | }; |
173 | |
174 | my $cd = $schema->resultset("CD")->find(1); |
175 | |
176 | print $cd->artist->id; |
177 | is($cd->artist->id, 1, 'rel okay'); |
178 | |
179 | my $new_cd = $schema->resultset("CD")->create($new_cd_hashref); |
180 | is($new_cd->artist->id, 17, 'new id retained okay'); |