4 use Test::More qw(no_plan);
8 my $schema = DBICTest->init_schema();
10 my $cd2 = $schema->resultset('CD')->create({ artist =>
11 { name => 'Fred Bloggs' },
16 is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
17 is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
19 my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
21 { title => 'Music to code by',
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');
30 $artist->update({cds => [ $artist->cds,
31 { title => 'Yet another CD',
36 is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
38 my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
40 is($newartist->name, 'Fred 2', 'Retrieved the artist');
43 my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
45 { title => 'Noah Act',
52 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
54 my $artist2 = $schema->resultset('Artist')->create({ artistid => 1000,
58 title => 'Music to code by',
64 title => 'Music to code by',
70 is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
74 my $artist = $schema->resultset('Artist')->first;
76 my $cd_result = $artist->create_related('cds', {
78 title => 'TestOneCD1',
92 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
93 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
95 my $tracks = $cd_result->tracks;
97 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
99 foreach my $track ($tracks->all)
101 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
107 my $artist = $schema->resultset('Artist')->first;
109 my $cd_result = $artist->create_related('cds', {
111 title => 'TestOneCD2',
123 liner_notes => { notes => 'I can haz liner notes?' },
127 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
128 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
129 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
131 my $tracks = $cd_result->tracks;
133 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
135 foreach my $track ($tracks->all)
137 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
141 my $cdp = $schema->resultset('CD_to_Producer')->create({
142 cd => { artist => 1, title => 'foo', year => 2000 },
143 producer => { name => 'jorge' }
146 ok($cdp, 'join table record created ok');
149 my $kurt_cobain = { name => 'Kurt Cobain' };
151 my $in_utero = $schema->resultset('CD')->new({
156 $kurt_cobain->{cds} = [ $in_utero ];
159 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
160 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
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');
168 my $pink_floyd = { name => 'Pink Floyd' };
170 my $the_wall = { title => 'The Wall', year => 1979 };
172 $pink_floyd->{cds} = [ $the_wall ];
175 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
176 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
178 is($a->name, 'Pink Floyd', 'Artist insertion ok');
179 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
182 ## Create foreign key col obj including PK
183 ## See test 20 in 66relationships.t
184 my $new_cd_hashref = {
186 title => 'Boogie Woogie',
188 artist => { artistid => 17, name => 'king luke' }
191 my $cd = $schema->resultset("CD")->find(1);
193 is($cd->artist->id, 1, 'rel okay');
195 my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
196 is($new_cd->artist->id, 17, 'new id retained okay');