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