928bc70298a5fc3bde32508825f65e63c273ab2e
[dbsrgits/DBIx-Class.git] / t / cdbi / 12-filter.t
1 use strict;
2 use Test::More;
3
4 use lib 't/cdbi/testlib';
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 = (
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   ),
43 );
44
45 eval {
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";
49 };
50 is $@, '', "No errors";
51
52 {
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";
56 }
57
58 {
59   ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100";
60   is @actors, 4, "Got all";
61 }
62
63 {
64   my @actors = Actor->salary_between(100, 200);
65   is @actors, 0, "None in Range 100 - 200";
66 }
67
68 {
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";
72 }
73
74 {
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";
80 }
81
82 {
83   ok my @actors = Actor->search(Film => $film), "Search by object";
84   is @actors, 3, "3 actors in film 1";
85 }
86
87 #----------------------------------------------------------------------
88 # Iterators
89 #----------------------------------------------------------------------
90
91 my $it_class = 'DBIx::Class::ResultSet';
92
93 sub test_normal_iterator {
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";
103 }
104
105 test_normal_iterator;
106 {
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";
117 }
118
119 # make sure nothing gets clobbered;
120 test_normal_iterator;
121
122 {
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";
127 }
128
129 {
130   my @acts = $film->actors->slice(1);
131   is @acts, 1, "Slice of 1 actor";
132   is $acts[0]->name, "Actor 2", "Actor 2";
133 }
134
135 {
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";
139 }
140
141 package Class::DBI::My::Iterator;
142
143 our @ISA;
144
145 @ISA = ($it_class);
146
147 sub slice { qw/fred barney/ }
148
149 package main;
150
151 Actor->iterator_class('Class::DBI::My::Iterator');
152
153 delete $film->{related_resultsets};
154
155 {
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";
159
160   ok $film->actors->delete_all, "Can delete via iterator";
161   is $film->actors, 0, "no actors left";
162
163   eval { $film->actors->delete_all };
164   is $@, '', "Deleting again does no harm";
165 }
166
167 done_testing;