1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
8 use lib 't/cdbi/testlib';
11 Actor->has_a(Film => 'Film');
12 Film->has_many(actors => 'Actor', { order_by => 'name' });
13 is(Actor->primary_column, 'id', "Actor primary OK");
15 ok(Actor->can('Salary'), "Actor table set-up OK");
16 ok(Film->can('actors'), " and have a suitable method in Film");
18 Film->create_test_film;
20 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
23 my $pvj = Actor->create(
25 Name => 'Peter Vere-Jones',
27 Salary => '30_000', # For a voice!
32 is $pvj->Name, "Peter Vere-Jones", "PVJ name ok";
33 is $pvj->Film, undef, "No film";
34 ok $pvj->set_Film($btaste), "Set film";
36 is $pvj->Film->id, $btaste->id, "Now film";
38 my @actors = $btaste->actors;
39 is(@actors, 1, "Bad taste has one actor");
40 is($actors[0]->Name, $pvj->Name, " - the correct one");
44 Name => 'Peter Jackson',
45 Salary => '0', # it's a labour of love
48 eval { my $pj = Film->add_to_actors(\%pj_data) };
49 like $@, qr/class/, "add_to_actors must be object method";
51 eval { my $pj = $btaste->add_to_actors(%pj_data) };
52 like $@, qr/Result object instantiation requires a hashref as argument/, "add_to_actors takes hash";
55 my $pj = $btaste->add_to_actors(
57 Name => 'Peter Jackson',
58 Salary => '0', # it's a labour of love
63 is $pj->Name, "Peter Jackson", "PJ ok";
64 is $pvj->Name, "Peter Vere-Jones", "PVJ still ok";
67 my @actors = $btaste->actors;
68 is @actors, 2, " - so now we have 2";
69 is $actors[0]->Name, $pj->Name, "PJ first";
70 is $actors[1]->Name, $pvj->Name, "PVJ first";
74 my @actors = $btaste->actors(Name => $pj->Name);
75 is @actors, 1, "One actor from restricted (sorted) has_many";
76 is $actors[0]->Name, $pj->Name, "It's PJ";
78 is $@, '', "No errors";
80 my $as = Actor->create(
82 Name => 'Arnold Schwarzenegger',
83 Film => 'Terminator 2',
84 Salary => '15_000_000'
88 eval { $btaste->actors($pj, $pvj, $as) };
90 is($btaste->actors, 2, " - so we still only have 2 actors");
92 my @bta_before = Actor->search(Film => 'Bad Taste');
93 is(@bta_before, 2, "We have 2 actors in bad taste");
94 ok($btaste->delete, "Delete bad taste");
95 my @bta_after = Actor->search(Film => 'Bad Taste');
96 is(@bta_after, 0, " - after deleting there are no actors");
98 # While we're here, make sure Actors have unreadable mutators and
99 # unwritable accessors
101 eval { $as->Name("Paul Reubens") };
103 eval { my $name = $as->set_Name };
106 is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
109 # Test infering of the foreign key of a has_many from an existing has_a
114 Thing->has_a(that_thing => "OtherThing");
115 OtherThing->has_many(things => "Thing");
117 my $other_thing = OtherThing->create({ id => 1 });
118 Thing->create({ id => 1, that_thing => $other_thing });
119 Thing->create({ id => 2, that_thing => $other_thing });
121 is_deeply [sort map { $_->id } $other_thing->things], [1,2];