Resolve $rsrc instance duality on metadata traversal
[dbsrgits/DBIx-Class.git] / t / 93single_accessor_object.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
bc1b7747 3use strict;
8273e845 4use warnings;
bc1b7747 5
6use Test::More;
370f2ba2 7use Test::Exception;
c0329273 8
bc1b7747 9use DBICTest;
10
bc1b7747 11# Test various uses of passing an object to find, create, and update on a single
12# rel accessor
13{
d9c17594 14 my $schema = DBICTest->init_schema();
bc1b7747 15 my $artist = $schema->resultset("Artist")->find(1);
16
17 my $cd = $schema->resultset("CD")->find_or_create({
18 artist => $artist,
19 title => "Object on a might_have",
20 year => 2006,
21 });
22 ok(defined $cd, 'created a CD');
23 is($cd->get_column('artist'), $artist->id, 'artist matches CD');
24
25 my $liner_notes = $schema->resultset("LinerNotes")->find_or_create({
096f4212 26 cd => $cd,
27 notes => "Creating using an object on a might_have is helpful.",
bc1b7747 28 });
29 ok(defined $liner_notes, 'created liner notes');
30 is($liner_notes->liner_id, $cd->cdid, 'liner notes matches CD');
31 is($liner_notes->notes, "Creating using an object on a might_have is helpful.", 'liner notes are correct');
32
33 my $track = $cd->tracks->find_or_create({
096f4212 34 position => 127,
bc1b7747 35 title => 'Single Accessor'
36 });
37 is($track->get_column('cd'), $cd->cdid, 'track matches CD before update');
38
39 my $another_cd = $schema->resultset("CD")->find(5);
40 $track->update({ disc => $another_cd });
096f4212 41 is($track->get_column('cd'), $another_cd->cdid, 'track matches another CD after update');
bc1b7747 42}
370f2ba2 43
370f2ba2 44
45{
d9c17594 46 my $schema = DBICTest->init_schema();
52ed6eb2 47 my $artist = $schema->resultset('Artist')->create({ artistid => 666, name => 'bad religion' });
48 my $cd = $schema->resultset('CD')->create({ cdid => 187, artist => 1, title => 'how could hell be any worse?', year => 1982, genreid => undef });
370f2ba2 49
52ed6eb2 50 ok(!defined($cd->get_column('genreid')), 'genreid is NULL'); #no accessor was defined for this column
51 ok(!defined($cd->genre), 'genre accessor returns undef');
370f2ba2 52}
53
370f2ba2 54{
d9c17594 55 my $schema = DBICTest->init_schema();
52ed6eb2 56 my $artist = $schema->resultset('Artist')->create({ artistid => 666, name => 'bad religion' });
57 my $genre = $schema->resultset('Genre')->create({ genreid => 88, name => 'disco' });
58 my $cd = $schema->resultset('CD')->create({ cdid => 187, artist => 1, title => 'how could hell be any worse?', year => 1982 });
370f2ba2 59
52ed6eb2 60 dies_ok { $cd->genre } 'genre accessor throws without column';
370f2ba2 61}
62
d9c17594 63done_testing;