Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
83eef562 |
2 | use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat'; |
3 | |
b8e1e21f |
4 | use strict; |
4a233f30 |
5 | use warnings; |
83eef562 |
6 | |
b8e1e21f |
7 | use Test::More; |
8 | |
50891152 |
9 | use lib 't/cdbi/testlib'; |
b8e1e21f |
10 | use Film; |
11 | use Actor; |
b8e1e21f |
12 | Actor->has_a(Film => 'Film'); |
3bec1f52 |
13 | Film->has_many(actors => 'Actor', { order_by => 'name' }); |
b8e1e21f |
14 | is(Actor->primary_column, 'id', "Actor primary OK"); |
15 | |
16 | ok(Actor->can('Salary'), "Actor table set-up OK"); |
17 | ok(Film->can('actors'), " and have a suitable method in Film"); |
18 | |
19 | Film->create_test_film; |
20 | |
21 | ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste"); |
22 | |
23 | ok( |
6a3bf251 |
24 | my $pvj = Actor->create( |
25 | { |
26 | Name => 'Peter Vere-Jones', |
27 | Film => undef, |
28 | Salary => '30_000', # For a voice! |
29 | } |
30 | ), |
31 | 'create Actor' |
b8e1e21f |
32 | ); |
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"; |
36 | $pvj->update; |
37 | is $pvj->Film->id, $btaste->id, "Now film"; |
38 | { |
6a3bf251 |
39 | my @actors = $btaste->actors; |
40 | is(@actors, 1, "Bad taste has one actor"); |
41 | is($actors[0]->Name, $pvj->Name, " - the correct one"); |
b8e1e21f |
42 | } |
43 | |
44 | my %pj_data = ( |
6a3bf251 |
45 | Name => 'Peter Jackson', |
46 | Salary => '0', # it's a labour of love |
b8e1e21f |
47 | ); |
48 | |
49 | eval { my $pj = Film->add_to_actors(\%pj_data) }; |
50 | like $@, qr/class/, "add_to_actors must be object method"; |
51 | |
52 | eval { my $pj = $btaste->add_to_actors(%pj_data) }; |
e84ae5d1 |
53 | like $@, qr/Result object instantiation requires a hashref as argument/, "add_to_actors takes hash"; |
b8e1e21f |
54 | |
55 | ok( |
6a3bf251 |
56 | my $pj = $btaste->add_to_actors( |
57 | { |
58 | Name => 'Peter Jackson', |
59 | Salary => '0', # it's a labour of love |
60 | } |
61 | ), |
62 | 'add_to_actors' |
b8e1e21f |
63 | ); |
64 | is $pj->Name, "Peter Jackson", "PJ ok"; |
65 | is $pvj->Name, "Peter Vere-Jones", "PVJ still ok"; |
66 | |
67 | { |
6a3bf251 |
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"; |
b8e1e21f |
72 | } |
73 | |
74 | eval { |
6a3bf251 |
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"; |
b8e1e21f |
78 | }; |
79 | is $@, '', "No errors"; |
80 | |
81 | my $as = Actor->create( |
6a3bf251 |
82 | { |
83 | Name => 'Arnold Schwarzenegger', |
84 | Film => 'Terminator 2', |
85 | Salary => '15_000_000' |
86 | } |
b8e1e21f |
87 | ); |
88 | |
89 | eval { $btaste->actors($pj, $pvj, $as) }; |
90 | ok $@, $@; |
91 | is($btaste->actors, 2, " - so we still only have 2 actors"); |
92 | |
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"); |
98 | |
99 | # While we're here, make sure Actors have unreadable mutators and |
100 | # unwritable accessors |
101 | |
102 | eval { $as->Name("Paul Reubens") }; |
103 | ok $@, $@; |
104 | eval { my $name = $as->set_Name }; |
105 | ok $@, $@; |
106 | |
107 | is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie"); |
108 | |
3bec1f52 |
109 | |
110 | # Test infering of the foreign key of a has_many from an existing has_a |
111 | { |
112 | use Thing; |
113 | use OtherThing; |
114 | |
115 | Thing->has_a(that_thing => "OtherThing"); |
116 | OtherThing->has_many(things => "Thing"); |
117 | |
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 }); |
121 | |
122 | is_deeply [sort map { $_->id } $other_thing->things], [1,2]; |
123 | } |
d9bd5195 |
124 | |
125 | done_testing; |