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