Commit | Line | Data |
9bc6db13 |
1 | use strict; |
2 | use Test::More; |
3 | |
50891152 |
4 | use lib 't/cdbi/testlib'; |
9bc6db13 |
5 | use Actor; |
6 | use Film; |
7 | Film->has_many(actors => 'Actor'); |
8 | Actor->has_a('film' => 'Film'); |
9 | Actor->add_constructor(double_search => 'name = ? AND salary = ?'); |
10 | |
11 | my $film = Film->create({ Title => 'MY Film' }); |
12 | my $film2 = Film->create({ Title => 'Another Film' }); |
13 | |
14 | my @act = ( |
6a3bf251 |
15 | Actor->create( |
16 | { |
17 | name => 'Actor 1', |
18 | film => $film, |
19 | salary => 10, |
20 | } |
21 | ), |
22 | Actor->create( |
23 | { |
24 | name => 'Actor 2', |
25 | film => $film, |
26 | salary => 20, |
27 | } |
28 | ), |
29 | Actor->create( |
30 | { |
31 | name => 'Actor 3', |
32 | film => $film, |
33 | salary => 30, |
34 | } |
35 | ), |
36 | Actor->create( |
37 | { |
38 | name => 'Actor 4', |
39 | film => $film2, |
40 | salary => 50, |
41 | } |
42 | ), |
9bc6db13 |
43 | ); |
44 | |
45 | eval { |
6a3bf251 |
46 | my @actors = $film->actors(name => 'Actor 1'); |
47 | is @actors, 1, "Got one actor from restricted has_many"; |
48 | is $actors[0]->name, "Actor 1", "Correct name"; |
9bc6db13 |
49 | }; |
50 | is $@, '', "No errors"; |
51 | |
52 | { |
6a3bf251 |
53 | my @actors = Actor->double_search("Actor 1", 10); |
54 | is @actors, 1, "Got one actor"; |
55 | is $actors[0]->name, "Actor 1", "Correct name"; |
9bc6db13 |
56 | } |
57 | |
58 | { |
6a3bf251 |
59 | ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100"; |
60 | is @actors, 4, "Got all"; |
9bc6db13 |
61 | } |
62 | |
63 | { |
6a3bf251 |
64 | my @actors = Actor->salary_between(100, 200); |
65 | is @actors, 0, "None in Range 100 - 200"; |
9bc6db13 |
66 | } |
67 | |
68 | { |
6a3bf251 |
69 | ok my @actors = Actor->salary_between(0, 10), "Range 0 - 10"; |
70 | is @actors, 1, "Got 1"; |
71 | is $actors[0]->name, $act[0]->name, "Actor 1"; |
9bc6db13 |
72 | } |
73 | |
74 | { |
6a3bf251 |
75 | ok my @actors = Actor->salary_between(20, 30), "Range 20 - 20"; |
76 | @actors = sort { $a->salary <=> $b->salary } @actors; |
77 | is @actors, 2, "Got 2"; |
78 | is $actors[0]->name, $act[1]->name, "Actor 2"; |
79 | is $actors[1]->name, $act[2]->name, "and Actor 3"; |
9bc6db13 |
80 | } |
81 | |
82 | { |
6a3bf251 |
83 | ok my @actors = Actor->search(Film => $film), "Search by object"; |
84 | is @actors, 3, "3 actors in film 1"; |
9bc6db13 |
85 | } |
86 | |
87 | #---------------------------------------------------------------------- |
88 | # Iterators |
89 | #---------------------------------------------------------------------- |
90 | |
223b8fe3 |
91 | my $it_class = 'DBIx::Class::ResultSet'; |
9bc6db13 |
92 | |
93 | sub test_normal_iterator { |
6a3bf251 |
94 | my $it = $film->actors; |
95 | isa_ok $it, $it_class; |
96 | is $it->count, 3, " - with 3 elements"; |
97 | my $i = 0; |
98 | while (my $film = $it->next) { |
99 | is $film->name, $act[ $i++ ]->name, "Get $i"; |
100 | } |
101 | ok !$it->next, "No more"; |
102 | is $it->first->name, $act[0]->name, "Get first"; |
9bc6db13 |
103 | } |
104 | |
105 | test_normal_iterator; |
106 | { |
6a3bf251 |
107 | Film->has_many(actor_ids => [ Actor => 'id' ]); |
108 | my $it = $film->actor_ids; |
109 | isa_ok $it, $it_class; |
110 | is $it->count, 3, " - with 3 elements"; |
111 | my $i = 0; |
112 | while (my $film_id = $it->next) { |
113 | is $film_id, $act[ $i++ ]->id, "Get id $i"; |
114 | } |
115 | ok !$it->next, "No more"; |
116 | is $it->first, $act[0]->id, "Get first"; |
9bc6db13 |
117 | } |
118 | |
119 | # make sure nothing gets clobbered; |
120 | test_normal_iterator; |
121 | |
122 | { |
6a3bf251 |
123 | my @acts = $film->actors->slice(1, 2); |
124 | is @acts, 2, "Slice gives 2 actor"; |
125 | is $acts[0]->name, "Actor 2", "Actor 2"; |
126 | is $acts[1]->name, "Actor 3", "and actor 3"; |
9bc6db13 |
127 | } |
128 | |
129 | { |
6a3bf251 |
130 | my @acts = $film->actors->slice(1); |
131 | is @acts, 1, "Slice of 1 actor"; |
132 | is $acts[0]->name, "Actor 2", "Actor 2"; |
9bc6db13 |
133 | } |
134 | |
135 | { |
6a3bf251 |
136 | my @acts = $film->actors->slice(2, 8); |
137 | is @acts, 1, "Slice off the end"; |
138 | is $acts[0]->name, "Actor 3", "Gets last actor only"; |
9bc6db13 |
139 | } |
140 | |
141 | package Class::DBI::My::Iterator; |
142 | |
f9cc85ce |
143 | our @ISA; |
525035fb |
144 | |
145 | @ISA = ($it_class); |
9bc6db13 |
146 | |
147 | sub slice { qw/fred barney/ } |
148 | |
149 | package main; |
150 | |
151 | Actor->iterator_class('Class::DBI::My::Iterator'); |
152 | |
fefe2816 |
153 | delete $film->{related_resultsets}; |
154 | |
9bc6db13 |
155 | { |
6a3bf251 |
156 | my @acts = $film->actors->slice(1, 2); |
157 | is @acts, 2, "Slice gives 2 results"; |
158 | ok eq_set(\@acts, [qw/fred barney/]), "Fred and Barney"; |
9bc6db13 |
159 | |
6a3bf251 |
160 | ok $film->actors->delete_all, "Can delete via iterator"; |
161 | is $film->actors, 0, "no actors left"; |
9bc6db13 |
162 | |
6a3bf251 |
163 | eval { $film->actors->delete_all }; |
164 | is $@, '', "Deleting again does no harm"; |
9bc6db13 |
165 | } |
d9bd5195 |
166 | |
167 | done_testing; |