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