5 eval "use DBD::SQLite";
6 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 50);
12 Film->has_many(actors => 'Actor');
13 Actor->has_a('film' => 'Film');
14 Actor->add_constructor(double_search => 'name = ? AND salary = ?');
16 my $film = Film->create({ Title => 'MY Film' });
17 my $film2 = Film->create({ Title => 'Another Film' });
51 my @actors = $film->actors(name => 'Actor 1');
52 is @actors, 1, "Got one actor from restricted has_many";
53 is $actors[0]->name, "Actor 1", "Correct name";
55 is $@, '', "No errors";
58 my @actors = Actor->double_search("Actor 1", 10);
59 is @actors, 1, "Got one actor";
60 is $actors[0]->name, "Actor 1", "Correct name";
64 ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100";
65 is @actors, 4, "Got all";
69 my @actors = Actor->salary_between(100, 200);
70 is @actors, 0, "None in Range 100 - 200";
74 ok my @actors = Actor->salary_between(0, 10), "Range 0 - 10";
75 is @actors, 1, "Got 1";
76 is $actors[0]->name, $act[0]->name, "Actor 1";
80 ok my @actors = Actor->salary_between(20, 30), "Range 20 - 20";
81 @actors = sort { $a->salary <=> $b->salary } @actors;
82 is @actors, 2, "Got 2";
83 is $actors[0]->name, $act[1]->name, "Actor 2";
84 is $actors[1]->name, $act[2]->name, "and Actor 3";
88 ok my @actors = Actor->search(Film => $film), "Search by object";
89 is @actors, 3, "3 actors in film 1";
92 #----------------------------------------------------------------------
94 #----------------------------------------------------------------------
97 skip "Compat layer doesn't have iterator support yet", 33;
99 sub test_normal_iterator {
100 my $it = $film->actors;
101 isa_ok $it, "Class::DBI::Iterator";
102 is $it->count, 3, " - with 3 elements";
104 while (my $film = $it->next) {
105 is $film->name, $act[ $i++ ]->name, "Get $i";
107 ok !$it->next, "No more";
108 is $it->first->name, $act[0]->name, "Get first";
111 test_normal_iterator;
113 Film->has_many(actor_ids => [ Actor => 'id' ]);
114 my $it = $film->actor_ids;
115 isa_ok $it, "Class::DBI::Iterator";
116 is $it->count, 3, " - with 3 elements";
118 while (my $film_id = $it->next) {
119 is $film_id, $act[ $i++ ]->id, "Get id $i";
121 ok !$it->next, "No more";
122 is $it->first, $act[0]->id, "Get first";
125 # make sure nothing gets clobbered;
126 test_normal_iterator;
129 my @acts = $film->actors->slice(1, 2);
130 is @acts, 2, "Slice gives 2 actor";
131 is $acts[0]->name, "Actor 2", "Actor 2";
132 is $acts[1]->name, "Actor 3", "and actor 3";
136 my @acts = $film->actors->slice(1);
137 is @acts, 1, "Slice of 1 actor";
138 is $acts[0]->name, "Actor 2", "Actor 2";
142 my @acts = $film->actors->slice(2, 8);
143 is @acts, 1, "Slice off the end";
144 is $acts[0]->name, "Actor 3", "Gets last actor only";
147 package Class::DBI::My::Iterator;
149 use base 'Class::DBI::Iterator';
151 sub slice { qw/fred barney/ }
155 Actor->iterator_class('Class::DBI::My::Iterator');
158 my @acts = $film->actors->slice(1, 2);
159 is @acts, 2, "Slice gives 2 results";
160 ok eq_set(\@acts, [qw/fred barney/]), "Fred and Barney";
162 ok $film->actors->delete_all, "Can delete via iterator";
163 is $film->actors, 0, "no actors left";
165 eval { $film->actors->delete_all };
166 is $@, '', "Deleting again does no harm";