File: 12-filter.t

package info (click to toggle)
libclass-dbi-perl 3.0.17-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 548 kB
  • ctags: 251
  • sloc: perl: 2,118; makefile: 6
file content (165 lines) | stat: -rw-r--r-- 3,644 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use strict;
use Test::More;

BEGIN {
	eval "use DBD::SQLite";
	plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 50);
}

use lib 't/testlib';
use Actor;
use Film;
Film->has_many(actors                => 'Actor');
Actor->has_a('film'                  => 'Film');
Actor->add_constructor(double_search => 'name = ? AND salary = ?');

my $film  = Film->insert({ Title => 'MY Film' });
my $film2 = Film->insert({ Title => 'Another Film' });

my @act = (
	Actor->insert(
		{
			name   => 'Actor 1',
			film   => $film,
			salary => 10,
		}
	),
	Actor->insert(
		{
			name   => 'Actor 2',
			film   => $film,
			salary => 20,
		}
	),
	Actor->insert(
		{
			name   => 'Actor 3',
			film   => $film,
			salary => 30,
		}
	),
	Actor->insert(
		{
			name   => 'Actor 4',
			film   => $film2,
			salary => 50,
		}
	),
);

eval {
	my @actors = $film->actors(name => 'Actor 1');
	is @actors, 1, "Got one actor from restricted has_many";
	is $actors[0]->name, "Actor 1", "Correct name";
};
is $@, '', "No errors";

{
	my @actors = Actor->double_search("Actor 1", 10);
	is @actors, 1, "Got one actor";
	is $actors[0]->name, "Actor 1", "Correct name";
}

{
	ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100";
	is @actors, 4, "Got all";
}

{
	my @actors = Actor->salary_between(100, 200);
	is @actors, 0, "None in Range 100 - 200";
}

{
	ok my @actors = Actor->salary_between(0, 10), "Range 0 - 10";
	is @actors, 1, "Got 1";
	is $actors[0]->name, $act[0]->name, "Actor 1";
}

{
	ok my @actors = Actor->salary_between(20, 30), "Range 20 - 20";
	@actors = sort { $a->salary <=> $b->salary } @actors;
	is @actors, 2, "Got 2";
	is $actors[0]->name, $act[1]->name, "Actor 2";
	is $actors[1]->name, $act[2]->name, "and Actor 3";
}

{
	ok my @actors = Actor->search(Film => $film), "Search by object";
	is @actors, 3, "3 actors in film 1";
}

#----------------------------------------------------------------------
# Iterators
#----------------------------------------------------------------------

sub test_normal_iterator {
	my $it = $film->actors;
	isa_ok $it, "Class::DBI::Iterator";
	is $it->count, 3, " - with 3 elements";
	my $i = 0;
	while (my $film = $it->next) {
		is $film->name, $act[ $i++ ]->name, "Get $i";
	}
	ok !$it->next, "No more";
	is $it->first->name, $act[0]->name, "Get first";
}

test_normal_iterator;
{
	Film->has_many(actor_ids => [ Actor => 'id' ]);
	my $it = $film->actor_ids;
	isa_ok $it, "Class::DBI::Iterator";
	is $it->count, 3, " - with 3 elements";
	my $i = 0;
	while (my $film_id = $it->next) {
		is $film_id, $act[ $i++ ]->id, "Get id $i";
	}
	ok !$it->next, "No more";
	is $it->first, $act[0]->id, "Get first";
}

# make sure nothing gets clobbered;
test_normal_iterator;

{
	my @acts = $film->actors->slice(1, 2);
	is @acts, 2, "Slice gives 2 actor";
	is $acts[0]->name, "Actor 2", "Actor 2";
	is $acts[1]->name, "Actor 3", "and actor 3";
}

{
	my @acts = $film->actors->slice(1);
	is @acts, 1, "Slice of 1 actor";
	is $acts[0]->name, "Actor 2", "Actor 2";
}

{
	my @acts = $film->actors->slice(2, 8);
	is @acts, 1, "Slice off the end";
	is $acts[0]->name, "Actor 3", "Gets last actor only";
}

package Class::DBI::My::Iterator;

use base 'Class::DBI::Iterator';

sub slice { qw/fred barney/ }

package main;

Actor->iterator_class('Class::DBI::My::Iterator');

{
	my @acts = $film->actors->slice(1, 2);
	is @acts, 2, "Slice gives 2 results";
	is_deeply [sort map "$_", @acts], [qw/barney fred/], "Fred and Barney";

	ok $film->actors->delete_all, "Can delete via iterator";
	is $film->actors, 0, "no actors left";

	eval { $film->actors->delete_all };
	is $@, '', "Deleting again does no harm";
}