5 eval "use DBIx::Class::CDBICompat;";
7 plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
13 use lib 't/cdbi/testlib';
16 Film->has_many(actors => 'Actor');
17 Actor->has_a('film' => 'Film');
18 Actor->add_constructor(double_search => 'name = ? AND salary = ?');
20 my $film = Film->create({ Title => 'MY Film' });
21 my $film2 = Film->create({ Title => 'Another Film' });
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";
59 is $@, '', "No errors";
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";
68 ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100";
69 is @actors, 4, "Got all";
73 my @actors = Actor->salary_between(100, 200);
74 is @actors, 0, "None in Range 100 - 200";
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";
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";
92 ok my @actors = Actor->search(Film => $film), "Search by object";
93 is @actors, 3, "3 actors in film 1";
96 #----------------------------------------------------------------------
98 #----------------------------------------------------------------------
100 my $it_class = 'DBIx::Class::ResultSet';
102 sub test_normal_iterator {
103 my $it = $film->actors;
104 isa_ok $it, $it_class;
105 is $it->count, 3, " - with 3 elements";
107 while (my $film = $it->next) {
108 is $film->name, $act[ $i++ ]->name, "Get $i";
110 ok !$it->next, "No more";
111 is $it->first->name, $act[0]->name, "Get first";
114 test_normal_iterator;
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";
121 while (my $film_id = $it->next) {
122 is $film_id, $act[ $i++ ]->id, "Get id $i";
124 ok !$it->next, "No more";
125 is $it->first, $act[0]->id, "Get first";
128 # make sure nothing gets clobbered;
129 test_normal_iterator;
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";
139 my @acts = $film->actors->slice(1);
140 is @acts, 1, "Slice of 1 actor";
141 is $acts[0]->name, "Actor 2", "Actor 2";
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";
150 package Class::DBI::My::Iterator;
156 sub slice { qw/fred barney/ }
160 Actor->iterator_class('Class::DBI::My::Iterator');
162 delete $film->{related_resultsets};
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";
169 ok $film->actors->delete_all, "Can delete via iterator";
170 is $film->actors, 0, "no actors left";
172 eval { $film->actors->delete_all };
173 is $@, '', "Deleting again does no harm";