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