4 use lib 't/cdbi/testlib';
7 Film->has_many(actors => 'Actor');
8 Actor->has_a('film' => 'Film');
9 Actor->add_constructor(double_search => 'name = ? AND salary = ?');
11 my $film = Film->create({ Title => 'MY Film' });
12 my $film2 = Film->create({ Title => 'Another Film' });
46 my @actors = $film->actors(name => 'Actor 1');
47 is @actors, 1, "Got one actor from restricted has_many";
48 is $actors[0]->name, "Actor 1", "Correct name";
50 is $@, '', "No errors";
53 my @actors = Actor->double_search("Actor 1", 10);
54 is @actors, 1, "Got one actor";
55 is $actors[0]->name, "Actor 1", "Correct name";
59 ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100";
60 is @actors, 4, "Got all";
64 my @actors = Actor->salary_between(100, 200);
65 is @actors, 0, "None in Range 100 - 200";
69 ok my @actors = Actor->salary_between(0, 10), "Range 0 - 10";
70 is @actors, 1, "Got 1";
71 is $actors[0]->name, $act[0]->name, "Actor 1";
75 ok my @actors = Actor->salary_between(20, 30), "Range 20 - 20";
76 @actors = sort { $a->salary <=> $b->salary } @actors;
77 is @actors, 2, "Got 2";
78 is $actors[0]->name, $act[1]->name, "Actor 2";
79 is $actors[1]->name, $act[2]->name, "and Actor 3";
83 ok my @actors = Actor->search(Film => $film), "Search by object";
84 is @actors, 3, "3 actors in film 1";
87 #----------------------------------------------------------------------
89 #----------------------------------------------------------------------
91 my $it_class = 'DBIx::Class::ResultSet';
93 sub test_normal_iterator {
94 my $it = $film->actors;
95 isa_ok $it, $it_class;
96 is $it->count, 3, " - with 3 elements";
98 while (my $film = $it->next) {
99 is $film->name, $act[ $i++ ]->name, "Get $i";
101 ok !$it->next, "No more";
102 is $it->first->name, $act[0]->name, "Get first";
105 test_normal_iterator;
107 Film->has_many(actor_ids => [ Actor => 'id' ]);
108 my $it = $film->actor_ids;
109 isa_ok $it, $it_class;
110 is $it->count, 3, " - with 3 elements";
112 while (my $film_id = $it->next) {
113 is $film_id, $act[ $i++ ]->id, "Get id $i";
115 ok !$it->next, "No more";
116 is $it->first, $act[0]->id, "Get first";
119 # make sure nothing gets clobbered;
120 test_normal_iterator;
123 my @acts = $film->actors->slice(1, 2);
124 is @acts, 2, "Slice gives 2 actor";
125 is $acts[0]->name, "Actor 2", "Actor 2";
126 is $acts[1]->name, "Actor 3", "and actor 3";
130 my @acts = $film->actors->slice(1);
131 is @acts, 1, "Slice of 1 actor";
132 is $acts[0]->name, "Actor 2", "Actor 2";
136 my @acts = $film->actors->slice(2, 8);
137 is @acts, 1, "Slice off the end";
138 is $acts[0]->name, "Actor 3", "Gets last actor only";
141 package Class::DBI::My::Iterator;
147 sub slice { qw/fred barney/ }
151 Actor->iterator_class('Class::DBI::My::Iterator');
153 delete $film->{related_resultsets};
156 my @acts = $film->actors->slice(1, 2);
157 is @acts, 2, "Slice gives 2 results";
158 ok eq_set(\@acts, [qw/fred barney/]), "Fred and Barney";
160 ok $film->actors->delete_all, "Can delete via iterator";
161 is $film->actors, 0, "no actors left";
163 eval { $film->actors->delete_all };
164 is $@, '', "Deleting again does no harm";