handle the throw_exception bit. Drop DBIx::Class::Exception
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 09-has_many.t
CommitLineData
b8e1e21f 1use strict;
2use Test::More;
3
4BEGIN {
5 eval "use DBD::SQLite";
6 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 30);
7}
8
9
10use lib 't/testlib';
11use Film;
12use Actor;
13Film->has_many(actors => Actor => 'Film', { order_by => 'name' });
14Actor->has_a(Film => 'Film');
15is(Actor->primary_column, 'id', "Actor primary OK");
16
17ok(Actor->can('Salary'), "Actor table set-up OK");
18ok(Film->can('actors'), " and have a suitable method in Film");
19
20Film->create_test_film;
21
22ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
23
24ok(
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);
34is $pvj->Name, "Peter Vere-Jones", "PVJ name ok";
35is $pvj->Film, undef, "No film";
36ok $pvj->set_Film($btaste), "Set film";
37$pvj->update;
38is $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
45my %pj_data = (
46 Name => 'Peter Jackson',
47 Salary => '0', # it's a labour of love
48);
49
50eval { my $pj = Film->add_to_actors(\%pj_data) };
51like $@, qr/class/, "add_to_actors must be object method";
52
53eval { my $pj = $btaste->add_to_actors(%pj_data) };
54like $@, qr/needs/, "add_to_actors takes hash";
55
56ok(
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);
65is $pj->Name, "Peter Jackson", "PJ ok";
66is $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
75eval {
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};
80is $@, '', "No errors";
81
82my $as = Actor->create(
83 {
84 Name => 'Arnold Schwarzenegger',
85 Film => 'Terminator 2',
86 Salary => '15_000_000'
87 }
88);
89
90eval { $btaste->actors($pj, $pvj, $as) };
91ok $@, $@;
92is($btaste->actors, 2, " - so we still only have 2 actors");
93
94my @bta_before = Actor->search(Film => 'Bad Taste');
95is(@bta_before, 2, "We have 2 actors in bad taste");
96ok($btaste->delete, "Delete bad taste");
97my @bta_after = Actor->search(Film => 'Bad Taste');
98is(@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
103eval { $as->Name("Paul Reubens") };
104ok $@, $@;
105eval { my $name = $as->set_Name };
106ok $@, $@;
107
108is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
109