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