1use strict; 2use Test::More; 3 4BEGIN { 5 eval "use DBD::SQLite"; 6 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 50); 7} 8 9use lib 't/testlib'; 10use Actor; 11use Film; 12Film->has_many(actors => 'Actor'); 13Actor->has_a('film' => 'Film'); 14Actor->add_constructor(double_search => 'name = ? AND salary = ?'); 15 16my $film = Film->insert({ Title => 'MY Film' }); 17my $film2 = Film->insert({ Title => 'Another Film' }); 18 19my @act = ( 20 Actor->insert( 21 { 22 name => 'Actor 1', 23 film => $film, 24 salary => 10, 25 } 26 ), 27 Actor->insert( 28 { 29 name => 'Actor 2', 30 film => $film, 31 salary => 20, 32 } 33 ), 34 Actor->insert( 35 { 36 name => 'Actor 3', 37 film => $film, 38 salary => 30, 39 } 40 ), 41 Actor->insert( 42 { 43 name => 'Actor 4', 44 film => $film2, 45 salary => 50, 46 } 47 ), 48); 49 50eval { 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"; 54}; 55is $@, '', "No errors"; 56 57{ 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"; 61} 62 63{ 64 ok my @actors = Actor->salary_between(0, 100), "Range 0 - 100"; 65 is @actors, 4, "Got all"; 66} 67 68{ 69 my @actors = Actor->salary_between(100, 200); 70 is @actors, 0, "None in Range 100 - 200"; 71} 72 73{ 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"; 77} 78 79{ 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"; 85} 86 87{ 88 ok my @actors = Actor->search(Film => $film), "Search by object"; 89 is @actors, 3, "3 actors in film 1"; 90} 91 92#---------------------------------------------------------------------- 93# Iterators 94#---------------------------------------------------------------------- 95 96sub test_normal_iterator { 97 my $it = $film->actors; 98 isa_ok $it, "Class::DBI::Iterator"; 99 is $it->count, 3, " - with 3 elements"; 100 my $i = 0; 101 while (my $film = $it->next) { 102 is $film->name, $act[ $i++ ]->name, "Get $i"; 103 } 104 ok !$it->next, "No more"; 105 is $it->first->name, $act[0]->name, "Get first"; 106} 107 108test_normal_iterator; 109{ 110 Film->has_many(actor_ids => [ Actor => 'id' ]); 111 my $it = $film->actor_ids; 112 isa_ok $it, "Class::DBI::Iterator"; 113 is $it->count, 3, " - with 3 elements"; 114 my $i = 0; 115 while (my $film_id = $it->next) { 116 is $film_id, $act[ $i++ ]->id, "Get id $i"; 117 } 118 ok !$it->next, "No more"; 119 is $it->first, $act[0]->id, "Get first"; 120} 121 122# make sure nothing gets clobbered; 123test_normal_iterator; 124 125{ 126 my @acts = $film->actors->slice(1, 2); 127 is @acts, 2, "Slice gives 2 actor"; 128 is $acts[0]->name, "Actor 2", "Actor 2"; 129 is $acts[1]->name, "Actor 3", "and actor 3"; 130} 131 132{ 133 my @acts = $film->actors->slice(1); 134 is @acts, 1, "Slice of 1 actor"; 135 is $acts[0]->name, "Actor 2", "Actor 2"; 136} 137 138{ 139 my @acts = $film->actors->slice(2, 8); 140 is @acts, 1, "Slice off the end"; 141 is $acts[0]->name, "Actor 3", "Gets last actor only"; 142} 143 144package Class::DBI::My::Iterator; 145 146use base 'Class::DBI::Iterator'; 147 148sub slice { qw/fred barney/ } 149 150package main; 151 152Actor->iterator_class('Class::DBI::My::Iterator'); 153 154{ 155 my @acts = $film->actors->slice(1, 2); 156 is @acts, 2, "Slice gives 2 results"; 157 is_deeply [sort map "$_", @acts], [qw/barney fred/], "Fred and Barney"; 158 159 ok $film->actors->delete_all, "Can delete via iterator"; 160 is $film->actors, 0, "no actors left"; 161 162 eval { $film->actors->delete_all }; 163 is $@, '', "Deleting again does no harm"; 164} 165 166