Commit | Line | Data |
b8e1e21f |
1 | use strict; |
2 | use Test::More; |
3 | |
289ba852 |
4 | |
b8e1e21f |
5 | BEGIN { |
289ba852 |
6 | eval "use DBIx::Class::CDBICompat;"; |
7 | plan skip_all => 'Class::Trigger and DBIx::ContextualFetch required' if $@; |
68de9438 |
8 | plan tests => 31; |
b8e1e21f |
9 | } |
10 | |
11 | |
50891152 |
12 | use lib 't/cdbi/testlib'; |
b8e1e21f |
13 | use Film; |
14 | use Actor; |
b8e1e21f |
15 | Actor->has_a(Film => 'Film'); |
3bec1f52 |
16 | Film->has_many(actors => 'Actor', { order_by => 'name' }); |
b8e1e21f |
17 | is(Actor->primary_column, 'id', "Actor primary OK"); |
18 | |
19 | ok(Actor->can('Salary'), "Actor table set-up OK"); |
20 | ok(Film->can('actors'), " and have a suitable method in Film"); |
21 | |
22 | Film->create_test_film; |
23 | |
24 | ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste"); |
25 | |
26 | ok( |
6a3bf251 |
27 | my $pvj = Actor->create( |
28 | { |
29 | Name => 'Peter Vere-Jones', |
30 | Film => undef, |
31 | Salary => '30_000', # For a voice! |
32 | } |
33 | ), |
34 | 'create Actor' |
b8e1e21f |
35 | ); |
36 | is $pvj->Name, "Peter Vere-Jones", "PVJ name ok"; |
37 | is $pvj->Film, undef, "No film"; |
38 | ok $pvj->set_Film($btaste), "Set film"; |
39 | $pvj->update; |
40 | is $pvj->Film->id, $btaste->id, "Now film"; |
41 | { |
6a3bf251 |
42 | my @actors = $btaste->actors; |
43 | is(@actors, 1, "Bad taste has one actor"); |
44 | is($actors[0]->Name, $pvj->Name, " - the correct one"); |
b8e1e21f |
45 | } |
46 | |
47 | my %pj_data = ( |
6a3bf251 |
48 | Name => 'Peter Jackson', |
49 | Salary => '0', # it's a labour of love |
b8e1e21f |
50 | ); |
51 | |
52 | eval { my $pj = Film->add_to_actors(\%pj_data) }; |
53 | like $@, qr/class/, "add_to_actors must be object method"; |
54 | |
55 | eval { my $pj = $btaste->add_to_actors(%pj_data) }; |
56 | like $@, qr/needs/, "add_to_actors takes hash"; |
57 | |
58 | ok( |
6a3bf251 |
59 | my $pj = $btaste->add_to_actors( |
60 | { |
61 | Name => 'Peter Jackson', |
62 | Salary => '0', # it's a labour of love |
63 | } |
64 | ), |
65 | 'add_to_actors' |
b8e1e21f |
66 | ); |
67 | is $pj->Name, "Peter Jackson", "PJ ok"; |
68 | is $pvj->Name, "Peter Vere-Jones", "PVJ still ok"; |
69 | |
70 | { |
6a3bf251 |
71 | my @actors = $btaste->actors; |
72 | is @actors, 2, " - so now we have 2"; |
73 | is $actors[0]->Name, $pj->Name, "PJ first"; |
74 | is $actors[1]->Name, $pvj->Name, "PVJ first"; |
b8e1e21f |
75 | } |
76 | |
77 | eval { |
6a3bf251 |
78 | my @actors = $btaste->actors(Name => $pj->Name); |
79 | is @actors, 1, "One actor from restricted (sorted) has_many"; |
80 | is $actors[0]->Name, $pj->Name, "It's PJ"; |
b8e1e21f |
81 | }; |
82 | is $@, '', "No errors"; |
83 | |
84 | my $as = Actor->create( |
6a3bf251 |
85 | { |
86 | Name => 'Arnold Schwarzenegger', |
87 | Film => 'Terminator 2', |
88 | Salary => '15_000_000' |
89 | } |
b8e1e21f |
90 | ); |
91 | |
92 | eval { $btaste->actors($pj, $pvj, $as) }; |
93 | ok $@, $@; |
94 | is($btaste->actors, 2, " - so we still only have 2 actors"); |
95 | |
96 | my @bta_before = Actor->search(Film => 'Bad Taste'); |
97 | is(@bta_before, 2, "We have 2 actors in bad taste"); |
98 | ok($btaste->delete, "Delete bad taste"); |
99 | my @bta_after = Actor->search(Film => 'Bad Taste'); |
100 | is(@bta_after, 0, " - after deleting there are no actors"); |
101 | |
102 | # While we're here, make sure Actors have unreadable mutators and |
103 | # unwritable accessors |
104 | |
105 | eval { $as->Name("Paul Reubens") }; |
106 | ok $@, $@; |
107 | eval { my $name = $as->set_Name }; |
108 | ok $@, $@; |
109 | |
110 | is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie"); |
111 | |
3bec1f52 |
112 | |
113 | # Test infering of the foreign key of a has_many from an existing has_a |
114 | { |
115 | use Thing; |
116 | use OtherThing; |
117 | |
118 | Thing->has_a(that_thing => "OtherThing"); |
119 | OtherThing->has_many(things => "Thing"); |
120 | |
121 | my $other_thing = OtherThing->create({ id => 1 }); |
122 | Thing->create({ id => 1, that_thing => $other_thing }); |
123 | Thing->create({ id => 2, that_thing => $other_thing }); |
124 | |
125 | is_deeply [sort map { $_->id } $other_thing->things], [1,2]; |
126 | } |