5 eval "use DBIx::Class::CDBICompat;";
7 plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
10 eval "use DBD::SQLite";
11 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 50);
14 use lib 't/cdbi/testlib';
17 Film->has_many(actors => 'Actor');
18 Actor->has_a('film' => 'Film');
19 Actor->add_constructor(double_search => 'name = ? AND salary = ?');
21 my $film = Film->create({ Title => 'MY Film' });
22 my $film2 = Film->create({ Title => 'Another Film' });
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";
60 is $@, '', "No errors";
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";
69 ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100";
70 is @actors, 4, "Got all";
74 my @actors = Actor->salary_between(100, 200);
75 is @actors, 0, "None in Range 100 - 200";
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";
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";
93 ok my @actors = Actor->search(Film => $film), "Search by object";
94 is @actors, 3, "3 actors in film 1";
97 #----------------------------------------------------------------------
99 #----------------------------------------------------------------------
101 my $it_class = 'DBIx::Class::ResultSet';
103 sub test_normal_iterator {
104 my $it = $film->actors;
105 isa_ok $it, $it_class;
106 is $it->count, 3, " - with 3 elements";
108 while (my $film = $it->next) {
109 is $film->name, $act[ $i++ ]->name, "Get $i";
111 ok !$it->next, "No more";
112 is $it->first->name, $act[0]->name, "Get first";
115 test_normal_iterator;
117 Film->has_many(actor_ids => [ Actor => 'id' ]);
118 my $it = $film->actor_ids;
119 isa_ok $it, $it_class;
120 is $it->count, 3, " - with 3 elements";
122 while (my $film_id = $it->next) {
123 is $film_id, $act[ $i++ ]->id, "Get id $i";
125 ok !$it->next, "No more";
126 is $it->first, $act[0]->id, "Get first";
129 # make sure nothing gets clobbered;
130 test_normal_iterator;
133 #skip "dbic iterators don't support slice yet", 12;
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";
144 my @acts = $film->actors->slice(1);
145 is @acts, 1, "Slice of 1 actor";
146 is $acts[0]->name, "Actor 2", "Actor 2";
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";
155 package Class::DBI::My::Iterator;
161 sub slice { qw/fred barney/ }
165 Actor->iterator_class('Class::DBI::My::Iterator');
167 delete $film->{related_resultsets};
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";
174 ok $film->actors->delete_all, "Can delete via iterator";
175 is $film->actors, 0, "no actors left";
177 eval { $film->actors->delete_all };
178 is $@, '', "Deleting again does no harm";