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