Test populate_rs with belongs_to rels
[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
79fa4982 8plan tests => 43;
81ab7888 9
10my $schema = DBICTest->init_schema();
11my $rs = $schema->resultset('Artist');
12
71d496fe 13RETURN_RESULTSETS: {
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
50RETURN_VOID: {
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' },
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
79fa4982 151## Test from a belongs_to perspective, should create artist first, then CD with artistid in:
152my $cd_rs = $schema->resultset('CD');
153$cd_rs->populate([
154 { title => 'Some CD',
155 year => 1997,
156 artist => { name => 'Fred Bloggs'},
157 }] );
158my $cd = $schema->resultset('CD')->find({title => 'Some CD'});
159
160isa_ok($cd, 'DBICTest::CD', 'Created CD');
161isa_ok($cd->artist, 'DBICTest::Artist', 'Set Artist');
162is($cd->artist->name, 'Fred Bloggs', 'Set Artist to Fred');