1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
8 use lib 't/cdbi/testlib';
11 Film->has_many(actors => 'Actor');
12 Actor->has_a('film' => 'Film');
13 Actor->add_constructor(double_search => 'name = ? AND salary = ?');
15 my $film = Film->create({ Title => 'MY Film' });
16 my $film2 = Film->create({ Title => 'Another Film' });
50 my @actors = $film->actors(name => 'Actor 1');
51 is @actors, 1, "Got one actor from restricted has_many";
52 is $actors[0]->name, "Actor 1", "Correct name";
54 is $@, '', "No errors";
57 my @actors = Actor->double_search("Actor 1", 10);
58 is @actors, 1, "Got one actor";
59 is $actors[0]->name, "Actor 1", "Correct name";
63 ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100";
64 is @actors, 4, "Got all";
68 my @actors = Actor->salary_between(100, 200);
69 is @actors, 0, "None in Range 100 - 200";
73 ok my @actors = Actor->salary_between(0, 10), "Range 0 - 10";
74 is @actors, 1, "Got 1";
75 is $actors[0]->name, $act[0]->name, "Actor 1";
79 ok my @actors = Actor->salary_between(20, 30), "Range 20 - 20";
80 @actors = sort { $a->salary <=> $b->salary } @actors;
81 is @actors, 2, "Got 2";
82 is $actors[0]->name, $act[1]->name, "Actor 2";
83 is $actors[1]->name, $act[2]->name, "and Actor 3";
87 ok my @actors = Actor->search(Film => $film), "Search by object";
88 is @actors, 3, "3 actors in film 1";
91 #----------------------------------------------------------------------
93 #----------------------------------------------------------------------
95 my $it_class = 'DBIx::Class::ResultSet';
97 sub test_normal_iterator {
98 my $it = $film->actors;
99 isa_ok $it, $it_class;
100 is $it->count, 3, " - with 3 elements";
102 while (my $film = $it->next) {
103 is $film->name, $act[ $i++ ]->name, "Get $i";
105 ok !$it->next, "No more";
106 is $it->first->name, $act[0]->name, "Get first";
109 test_normal_iterator;
111 Film->has_many(actor_ids => [ Actor => 'id' ]);
112 my $it = $film->actor_ids;
113 isa_ok $it, $it_class;
114 is $it->count, 3, " - with 3 elements";
116 while (my $film_id = $it->next) {
117 is $film_id, $act[ $i++ ]->id, "Get id $i";
119 ok !$it->next, "No more";
120 is $it->first, $act[0]->id, "Get first";
123 # make sure nothing gets clobbered;
124 test_normal_iterator;
127 my @acts = $film->actors->slice(1, 2);
128 is @acts, 2, "Slice gives 2 actor";
129 is $acts[0]->name, "Actor 2", "Actor 2";
130 is $acts[1]->name, "Actor 3", "and actor 3";
134 my @acts = $film->actors->slice(1);
135 is @acts, 1, "Slice of 1 actor";
136 is $acts[0]->name, "Actor 2", "Actor 2";
140 my @acts = $film->actors->slice(2, 8);
141 is @acts, 1, "Slice off the end";
142 is $acts[0]->name, "Actor 3", "Gets last actor only";
145 package Class::DBI::My::Iterator;
151 sub slice { qw/fred barney/ }
155 Actor->iterator_class('Class::DBI::My::Iterator');
157 delete $film->{related_resultsets};
160 my @acts = $film->actors->slice(1, 2);
161 is @acts, 2, "Slice gives 2 results";
162 ok eq_set(\@acts, [qw/fred barney/]), "Fred and Barney";
164 ok $film->actors->delete_all, "Can delete via iterator";
165 is $film->actors, 0, "no actors left";
167 eval { $film->actors->delete_all };
168 is $@, '', "Deleting again does no harm";