-- added support for belongs_to type relationships and better support for when the...
[dbsrgits/DBIx-Class.git] / t / 101populate_rs.t
CommitLineData
81ab7888 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
dc87edb5 8plan tests => 53;
81ab7888 9
10my $schema = DBICTest->init_schema();
11my $rs = $schema->resultset('Artist');
12
d21161f5 13RETURN_RESULTSETS_HAS_MANY: {
71d496fe 14
e287d9b0 15 my ($crap, $girl, $damn, $xxxaaa) = $rs->populate( [
71d496fe 16 { artistid => 4, name => 'Manufactured Crap', cds => [
17 { title => 'My First CD', year => 2006 },
18 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
19 ]
20 },
21 { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
22 { title => 'My parents sold me to a record company' ,year => 2005 },
23 { title => 'Why Am I So Ugly?', year => 2006 },
24 { title => 'I Got Surgery and am now Popular', year => 2007 }
25
26 ]
27 },
07bff494 28 { artistid=>6, name => 'Like I Give a Damn' },
29
30 { artistid => 7, name => 'bbbb', cds => [
31 { title => 'xxxaaa' ,year => 2005 },
32 ]
33 },
71d496fe 34
35 ] );
36
37 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
7f997467 38 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
39 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
e287d9b0 40 isa_ok( $xxxaaa, 'DBICTest::Artist', "Got 'Artist'");
71d496fe 41
42 ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
43 ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
e287d9b0 44 ok( $xxxaaa->name eq 'bbbb', "Got Correct name for result object");
6f8ab6dc 45
71d496fe 46 ok( $crap->cds->count == 2, "got Expected Number of Cds");
47 ok( $girl->cds->count == 3, "got Expected Number of Cds");
48}
49
d21161f5 50RETURN_VOID_HAS_MANY: {
71d496fe 51
52 $rs->populate( [
07bff494 53 { artistid => 8, name => 'Manufactured CrapB', cds => [
7f997467 54 { title => 'My First CDB', year => 2006 },
55 { title => 'Yet More Tweeny-Pop crapB', year => 2007 },
71d496fe 56 ]
57 },
07bff494 58 { artistid => 9, name => 'Angsty-Whiny GirlB', cds => [
7f997467 59 { title => 'My parents sold me to a record companyB' ,year => 2005 },
60 { title => 'Why Am I So Ugly?B', year => 2006 },
61 { title => 'I Got Surgery and am now PopularB', year => 2007 }
71d496fe 62
63 ]
64 },
e287d9b0 65 { artistid =>10, name => 'XXXX' },
dc87edb5 66 { artistid =>11, name => 'wart', cds =>[{ title => 'xxxaaa' ,year => 2005 }], },
71d496fe 67 ] );
7f997467 68
07bff494 69 my $artist = $rs->find(8);
71d496fe 70
71 ok($artist, 'Found artist');
e287d9b0 72 is($artist->name, 'Manufactured CrapB', "Got Correct Name");
71d496fe 73 is($artist->cds->count, 2, 'Has CDs');
74
75 my @cds = $artist->cds;
76
7f997467 77 is($cds[0]->title, 'My First CDB', 'A CD');
71d496fe 78 is($cds[0]->year, 2006, 'Published in 2006');
79
7f997467 80 is($cds[1]->title, 'Yet More Tweeny-Pop crapB', 'Another crap CD');
71d496fe 81 is($cds[1]->year, 2007, 'Published in 2007');
82
07bff494 83 $artist = $rs->find(9);
71d496fe 84 ok($artist, 'Found artist');
e287d9b0 85 is($artist->name, 'Angsty-Whiny GirlB', "Another correct name");
71d496fe 86 is($artist->cds->count, 3, 'Has CDs');
e287d9b0 87
71d496fe 88 @cds = $artist->cds;
89
90
7f997467 91 is($cds[0]->title, 'My parents sold me to a record companyB', 'A CD');
71d496fe 92 is($cds[0]->year, 2005, 'Published in 2005');
93
7f997467 94 is($cds[1]->title, 'Why Am I So Ugly?B', 'A Coaster');
71d496fe 95 is($cds[1]->year, 2006, 'Published in 2006');
96
7f997467 97 is($cds[2]->title, 'I Got Surgery and am now PopularB', 'Selling un-attainable dreams');
71d496fe 98 is($cds[2]->year, 2007, 'Published in 2007');
99
7f997467 100 $artist = $rs->search({name => 'XXXX'})->single;
101 ok($artist, "Got Expected Artist Result");
71d496fe 102
103 is($artist->cds->count, 0, 'No CDs');
e287d9b0 104
105 $artist = $rs->find(10);
106 is($artist->name, 'XXXX', "Got Correct Name");
107 is($artist->cds->count, 0, 'Has NO CDs');
108
109 $artist = $rs->find(11);
110 is($artist->name, 'wart', "Got Correct Name");
111 is($artist->cds->count, 1, 'Has One CD');
71d496fe 112}
81ab7888 113
6f8ab6dc 114RETURN_RESULTSETS_AUTOPK: {
115
116 my ($crap, $girl, $damn, $xxxaaa) = $rs->populate( [
117 { name => 'Manufactured CrapC', cds => [
118 { title => 'My First CD', year => 2006 },
119 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
120 ]
121 },
122 { name => 'Angsty-Whiny GirlC', cds => [
123 { title => 'My parents sold me to a record company' ,year => 2005 },
124 { title => 'Why Am I So Ugly?', year => 2006 },
125 { title => 'I Got Surgery and am now Popular', year => 2007 }
126
127 ]
128 },
129 { name => 'Like I Give a DamnC' },
130
131 { name => 'bbbbC', cds => [
132 { title => 'xxxaaa' ,year => 2005 },
133 ]
134 },
135
136 ] );
137
138 isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
139 isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
140 isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
141 isa_ok( $xxxaaa, 'DBICTest::Artist', "Got 'Artist'");
142
143 ok( $crap->name eq 'Manufactured CrapC', "Got Correct name for result object");
144 ok( $girl->name eq 'Angsty-Whiny GirlC', "Got Correct name for result object");
145 ok( $xxxaaa->name eq 'bbbbC', "Got Correct name for result object");
146
147 ok( $crap->cds->count == 2, "got Expected Number of Cds");
148 ok( $girl->cds->count == 3, "got Expected Number of Cds");
149}
150
d21161f5 151RETURN_RESULTSETS_BELONGS_TO: {
152
153 ## Test from a belongs_to perspective, should create artist first, then CD with artistid in:
154
155 my $cds = [
156 {
157 title => 'Some CD1',
158 year => '1997',
159 artist => { name => 'Fred BloggsA'},
160 },
161 {
162 title => 'Some CD2',
163 year => '1997',
164 artist => { name => 'Fred BloggsB'},
165 },
166 ];
167
168 my $cd_rs = $schema->resultset('CD');
169
170 my @ret = $cd_rs->populate($cds);
171
172 my $cdA = $schema->resultset('CD')->find({title => 'Some CD1'});
173
174 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
175 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
176 is($cdA->artist->name, 'Fred BloggsA', 'Set Artist to FredA');
177
178 my $cdB = $schema->resultset('CD')->find({title => 'Some CD2'});
179
180 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
181 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
182 is($cdB->artist->name, 'Fred BloggsB', 'Set Artist to FredB');
183}
184
dc87edb5 185RETURN_VOID_BELONGS_TO: {
d21161f5 186
dc87edb5 187 ## Test from a belongs_to perspective, should create artist first, then CD with artistid in:
188
189 my $cds = [
190 {
191 title => 'Some CD3',
192 year => '1997',
193 artist => { name => 'Fred BloggsC'},
194 },
195 {
196 title => 'Some CD4',
197 year => '1997',
198 artist => { name => 'Fred BloggsD'},
199 },
200 ];
201
202 my $cd_rs = $schema->resultset('CD');
203
204 ok( $cd_rs, 'Got Good CD Resultset');
205
206 $cd_rs->populate($cds);
207
208 my $cdA = $schema->resultset('CD')->find({title => 'Some CD3'});
209
210 isa_ok($cdA, 'DBICTest::CD', 'Created CD');
211 isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
212 is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
213
214 my $cdB = $schema->resultset('CD')->find({title => 'Some CD4'});
215
216 isa_ok($cdB, 'DBICTest::CD', 'Created CD');
217 isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
218 is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
219}
d21161f5 220
221
222
223