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