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