Switch the main dev branch back to 'master'
[dbsrgits/DBIx-Class.git] / t / cdbi / 12-filter.t
CommitLineData
83eef562 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
2
9bc6db13 3use strict;
4a233f30 4use warnings;
83eef562 5
9bc6db13 6use Test::More;
7
50891152 8use lib 't/cdbi/testlib';
9bc6db13 9use Actor;
10use Film;
11Film->has_many(actors => 'Actor');
12Actor->has_a('film' => 'Film');
13Actor->add_constructor(double_search => 'name = ? AND salary = ?');
14
15my $film = Film->create({ Title => 'MY Film' });
16my $film2 = Film->create({ Title => 'Another Film' });
17
18my @act = (
6a3bf251 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 ),
9bc6db13 47);
48
49eval {
6a3bf251 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";
9bc6db13 53};
54is $@, '', "No errors";
55
56{
6a3bf251 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";
9bc6db13 60}
61
62{
6a3bf251 63 ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100";
64 is @actors, 4, "Got all";
9bc6db13 65}
66
67{
6a3bf251 68 my @actors = Actor->salary_between(100, 200);
69 is @actors, 0, "None in Range 100 - 200";
9bc6db13 70}
71
72{
6a3bf251 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";
9bc6db13 76}
77
78{
6a3bf251 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";
9bc6db13 84}
85
86{
6a3bf251 87 ok my @actors = Actor->search(Film => $film), "Search by object";
88 is @actors, 3, "3 actors in film 1";
9bc6db13 89}
90
91#----------------------------------------------------------------------
92# Iterators
93#----------------------------------------------------------------------
94
223b8fe3 95my $it_class = 'DBIx::Class::ResultSet';
9bc6db13 96
97sub test_normal_iterator {
6a3bf251 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";
9bc6db13 107}
108
109test_normal_iterator;
110{
6a3bf251 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";
9bc6db13 121}
122
123# make sure nothing gets clobbered;
124test_normal_iterator;
125
126{
6a3bf251 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";
9bc6db13 131}
132
133{
6a3bf251 134 my @acts = $film->actors->slice(1);
135 is @acts, 1, "Slice of 1 actor";
136 is $acts[0]->name, "Actor 2", "Actor 2";
9bc6db13 137}
138
139{
6a3bf251 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";
9bc6db13 143}
144
145package Class::DBI::My::Iterator;
146
f9cc85ce 147our @ISA;
525035fb 148
149@ISA = ($it_class);
9bc6db13 150
151sub slice { qw/fred barney/ }
152
153package main;
154
155Actor->iterator_class('Class::DBI::My::Iterator');
156
fefe2816 157delete $film->{related_resultsets};
158
9bc6db13 159{
6a3bf251 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";
9bc6db13 163
6a3bf251 164 ok $film->actors->delete_all, "Can delete via iterator";
165 is $film->actors, 0, "no actors left";
9bc6db13 166
6a3bf251 167 eval { $film->actors->delete_all };
168 is $@, '', "Deleting again does no harm";
9bc6db13 169}
d9bd5195 170
171done_testing;