1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
9 use lib 't/cdbi/testlib';
12 Actor->has_a(Film => 'Film');
13 Film->has_many(actors => 'Actor', { order_by => 'name' });
14 is(Actor->primary_column, 'id', "Actor primary OK");
16 ok(Actor->can('Salary'), "Actor table set-up OK");
17 ok(Film->can('actors'), " and have a suitable method in Film");
19 Film->create_test_film;
21 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
24 my $pvj = Actor->create(
26 Name => 'Peter Vere-Jones',
28 Salary => '30_000', # For a voice!
33 is $pvj->Name, "Peter Vere-Jones", "PVJ name ok";
34 is $pvj->Film, undef, "No film";
35 ok $pvj->set_Film($btaste), "Set film";
37 is $pvj->Film->id, $btaste->id, "Now film";
39 my @actors = $btaste->actors;
40 is(@actors, 1, "Bad taste has one actor");
41 is($actors[0]->Name, $pvj->Name, " - the correct one");
45 Name => 'Peter Jackson',
46 Salary => '0', # it's a labour of love
49 eval { my $pj = Film->add_to_actors(\%pj_data) };
50 like $@, qr/class/, "add_to_actors must be object method";
52 eval { my $pj = $btaste->add_to_actors(%pj_data) };
53 like $@, qr/Result object instantiation requires a single hashref argument/, "add_to_actors takes hash";
56 my $pj = $btaste->add_to_actors(
58 Name => 'Peter Jackson',
59 Salary => '0', # it's a labour of love
64 is $pj->Name, "Peter Jackson", "PJ ok";
65 is $pvj->Name, "Peter Vere-Jones", "PVJ still ok";
68 my @actors = $btaste->actors;
69 is @actors, 2, " - so now we have 2";
70 is $actors[0]->Name, $pj->Name, "PJ first";
71 is $actors[1]->Name, $pvj->Name, "PVJ first";
75 my @actors = $btaste->actors(Name => $pj->Name);
76 is @actors, 1, "One actor from restricted (sorted) has_many";
77 is $actors[0]->Name, $pj->Name, "It's PJ";
79 is $@, '', "No errors";
81 my $as = Actor->create(
83 Name => 'Arnold Schwarzenegger',
84 Film => 'Terminator 2',
85 Salary => '15_000_000'
89 eval { $btaste->actors($pj, $pvj, $as) };
91 is($btaste->actors, 2, " - so we still only have 2 actors");
93 my @bta_before = Actor->search(Film => 'Bad Taste');
94 is(@bta_before, 2, "We have 2 actors in bad taste");
95 ok($btaste->delete, "Delete bad taste");
96 my @bta_after = Actor->search(Film => 'Bad Taste');
97 is(@bta_after, 0, " - after deleting there are no actors");
99 # While we're here, make sure Actors have unreadable mutators and
100 # unwritable accessors
102 eval { $as->Name("Paul Reubens") };
104 eval { my $name = $as->set_Name };
107 is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
110 # Test infering of the foreign key of a has_many from an existing has_a
115 Thing->has_a(that_thing => "OtherThing");
116 OtherThing->has_many(things => "Thing");
118 my $other_thing = OtherThing->create({ id => 1 });
119 Thing->create({ id => 1, that_thing => $other_thing });
120 Thing->create({ id => 2, that_thing => $other_thing });
122 is_deeply [sort map { $_->id } $other_thing->things], [1,2];