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