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