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