3a2bc4f4640eec132263eafa5e8d74a565d653a3
[dbsrgits/DBIx-Class.git] / t / 101populate_rs.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 plan tests => 43;
9
10 my $schema = DBICTest->init_schema();
11 my $rs = $schema->resultset('Artist');
12
13 RETURN_RESULTSETS: {
14
15         my ($crap, $girl, $damn, $xxxaaa) = $rs->populate( [
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           },
28           { artistid=>6, name => 'Like I Give a Damn' },
29           
30           { artistid => 7, name => 'bbbb', cds => [
31                   { title => 'xxxaaa' ,year => 2005 },
32                 ]
33           },
34
35         ] );
36         
37         isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
38         isa_ok( $damn, 'DBICTest::Artist', "Got 'Artist'");
39         isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");     
40         isa_ok( $xxxaaa, 'DBICTest::Artist', "Got 'Artist'");   
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");
44         ok( $xxxaaa->name eq 'bbbb', "Got Correct name for result object");
45                 
46         ok( $crap->cds->count == 2, "got Expected Number of Cds");
47         ok( $girl->cds->count == 3, "got Expected Number of Cds");
48 }
49
50 RETURN_VOID: {
51
52         $rs->populate( [
53           { artistid => 8, name => 'Manufactured CrapB', cds => [ 
54                   { title => 'My First CDB', year => 2006 },
55                   { title => 'Yet More Tweeny-Pop crapB', year => 2007 },
56                 ] 
57           },
58           { artistid => 9, name => 'Angsty-Whiny GirlB', cds => [
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 }
62
63                 ]
64           },
65           { artistid =>10,  name => 'XXXX' },
66           { artistid =>11, name => 'wart', cds =>{ title => 'xxxaaa' ,year => 2005 }, },
67         ] );
68         
69         my $artist = $rs->find(8);
70
71         ok($artist, 'Found artist');
72         is($artist->name, 'Manufactured CrapB', "Got Correct Name");
73         is($artist->cds->count, 2, 'Has CDs');
74
75         my @cds = $artist->cds;
76
77         is($cds[0]->title, 'My First CDB', 'A CD');
78         is($cds[0]->year,  2006, 'Published in 2006');
79
80         is($cds[1]->title, 'Yet More Tweeny-Pop crapB', 'Another crap CD');
81         is($cds[1]->year,  2007, 'Published in 2007');
82
83         $artist = $rs->find(9);
84         ok($artist, 'Found artist');
85         is($artist->name, 'Angsty-Whiny GirlB', "Another correct name");
86         is($artist->cds->count, 3, 'Has CDs');
87         
88         @cds = $artist->cds;
89
90
91         is($cds[0]->title, 'My parents sold me to a record companyB', 'A CD');
92         is($cds[0]->year,  2005, 'Published in 2005');
93
94         is($cds[1]->title, 'Why Am I So Ugly?B', 'A Coaster');
95         is($cds[1]->year,  2006, 'Published in 2006');
96
97         is($cds[2]->title, 'I Got Surgery and am now PopularB', 'Selling un-attainable dreams');
98         is($cds[2]->year,  2007, 'Published in 2007');
99
100         $artist = $rs->search({name => 'XXXX'})->single;
101         ok($artist, "Got Expected Artist Result");
102
103         is($artist->cds->count, 0, 'No CDs');
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');
112 }
113
114 RETURN_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
151 ## Test from a belongs_to perspective, should create artist first, then CD with artistid in:
152 my $cd_rs = $schema->resultset('CD');
153 $cd_rs->populate([ 
154                    { title => 'Some CD',
155                      year => 1997,
156                      artist => { name => 'Fred Bloggs'},
157                  }] );
158 my $cd = $schema->resultset('CD')->find({title => 'Some CD'});
159
160 isa_ok($cd, 'DBICTest::CD', 'Created CD');
161 isa_ok($cd->artist, 'DBICTest::Artist', 'Set Artist');
162 is($cd->artist->name, 'Fred Bloggs', 'Set Artist to Fred');