1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
# $Id: 01-col-inheritance.t 989 2005-09-23 19:58:01Z btrott $
use strict;
use lib 't/lib';
use lib 't/lib/cached';
use Test::More;
use Test::Exception;
use version;
use DodTestUtil;
BEGIN {
DodTestUtil->check_driver;
unless (eval { require Cache::Memory }) {
plan skip_all => 'Tests require Cache::Memory';
}
}
plan tests => 68;
use Wine;
use Recipe;
use Ingredient;
setup_dbs({
global => [ qw( wines recipes ingredients) ],
});
# method installation
{
my $w = Wine->new;
ok $w->name("name");
ok $w->has_column("name");
ok ! $w->has_column("inexistent");
dies_ok { $w->inexistent("hell") } "dies on setting inexistent column : 'inexistent()'";
dies_ok { $w->column('inexistent') } "dies on setting inexistent column : 'column()'";
}
# refresh
{
my $old ='Cul de Veau à la Sauge'; # tastes good !
my $new ='At first my tests ran on Recipe, sorry (Yann)';
my $w1 = Wine->new;
$w1->name($old);
ok $w1->save;
my $id = $w1->id;
my $w2 = Wine->lookup($id);
$w2->name($new);
$w2->save;
cmp_ok $w1->name, 'eq', $old, "Old name not updated...";
cmp_ok $w2->name, 'eq', $new, "... but new name is set";
$w1->refresh;
cmp_ok $w1->name, 'eq', $new, "Refreshed";
is $w1->remove, 1, 'Remove correct number of rows';
is $w2->remove, '0E0', 'Remove correct number of rows';
}
# Constructor testing
{
my $w = Wine->new(name=>'Mouton Rothschild', rating=> 4);
ok ($w, 'constructed a new Wine');
is ($w->name, 'Mouton Rothschild', 'name constructor');
is ($w->rating, 4, 'rating constructor');
}
# lookup with hash (single pk)
{
my $w = Wine->new;
$w->name("Veuve Cliquot");
$w->save;
my $id = $w->id;
undef $w;
# lookup test
lives_ok { $w = Wine->lookup({ id => $id })} "Alive !";
cmp_ok $w->name, 'eq', 'Veuve Cliquot', "simple data test";
ok $w;
is $w->remove, 1, 'Remove correct number of rows';
}
## lookup_multi give a sorted result set
{
my @ids;
for (1 .. 14) {
my $w = Wine->new(name => "wine-$_");
$w->save;
push @ids, $w->id;
}
if (eval { require List::Util }) {
@ids = List::Util::shuffle @ids;
} else {
@ids = reverse @ids;
}
my @got = map { $_->id } @{ Wine->lookup_multi(\@ids) };
is_deeply \@got, \@ids, "Sorted result set";
}
# lookups with hash (multiple pk)
{
my $r = Recipe->new;
$r->title("Good one");
ok $r->save;
my $rid = $r->recipe_id;
ok $rid;
my $i = Ingredient->new;
$i->recipe_id($rid);
$i->quantity(1);
$i->name('Chouchenn');
ok $i->save;
my $id = $i->id;
undef $i;
# lookup test
dies_ok { $i = Ingredient->lookup({ id => $id, quantity => 1 })} "Use Search !";
lives_ok { $i = Ingredient->lookup({ id => $id, recipe_id => $rid })} "Alive";
cmp_ok $i->name, 'eq', 'Chouchenn', "simple data test";
# lookup_multi with hash (multiple pk)
lives_ok { $i = Ingredient->lookup_multi(
[{ id => $id, recipe_id => $rid }])
} "Alive";
is scalar @$i, 1;
# add a second ingredient
my $i2 = Ingredient->new(
recipe_id => $rid,
quantity => 1,
name => 'honey',
);
$i2->save;
my $id2 = $i2->id;
lives_ok { $i = Ingredient->lookup_multi(
[{ id => $id, recipe_id => $rid }, { id => $id2, recipe_id => $rid } ])
} "Alive";
is scalar @$i, 2;
is $r->remove, 1, 'Remove correct number of rows';
is $i->[0]->remove, 1, 'Remove correct number or rows';
is $i->[1]->remove, 1, 'Remove correct number or rows';
}
# replace
{
my $r = Recipe->new;
$r->title("to replace");
ok $r->replace;
ok(my $rid = $r->recipe_id);
my $r2 = Recipe->new;
$r2->recipe_id($rid);
$r2->title('new title');
ok $r2->replace;
## check
$r = Recipe->lookup($rid);
is $r->title, 'new title';
$r2 = Recipe->new;
$r2->recipe_id($rid);
ok $r2->replace;
## check
$r = Recipe->lookup($rid);
is $r->title, undef;
}
# let's test atomicity of replace
{
my $r = Recipe->new;
$r->title("to replace");
$r->insert;
## too long title:
# Oh! right it's a feature :(
# http://www.sqlite.org/faq.html#q3
#$r->title(join '', ("0123456789" x 6));
#dies_ok { $r->replace };
#$r->refresh;
my $id = $r->recipe_id;
$r->title('replaced');
$r->recipe_id("lamer");
dies_ok { $r->replace };
$r = Recipe->lookup($id);
ok $r;
is $r->title, "to replace";
# emulate a driver which doesn't support REPLACE INTO
{
no warnings 'redefine';
local *Data::ObjectDriver::Driver::DBD::SQLite::can_replace = sub { 0 };
$r->title('replaced');
$r->recipe_id("lamer");
dies_ok { $r->replace };
$r = Recipe->lookup($id);
ok $r;
is $r->title, "to replace";
# emulate a driver which doesn't support REPLACE INTO
}
}
# is_changed interface
{
my $w = Wine->new;
$w->name("Veuve Cliquot");
$w->save;
ok ! $w->is_changed;
$w->name("veuve champenoise");
ok $w->is_changed;
ok $w->is_changed('name');
ok ! $w->is_changed('content');
}
# Remove counts
{
# Clear out the wine table
ok (Wine->remove(), 'delete all from Wine table');
is (Wine->remove({name=>'moooo'}), 0E0, 'No rows deleted');
my @bad_wines = qw(Thunderbird MadDog Franzia);
foreach my $name (@bad_wines) {
my $w = Wine->new;
$w->name($name);
ok $w->save, "Saving bad_wine $name";
}
is (Wine->remove(), scalar(@bad_wines), 'removing all bad wine');
# Do it again with direct remove from the DB
foreach my $name (@bad_wines) {
my $w = Wine->new;
$w->name($name);
ok $w->save, "Saving bad_wine $name";
}
# note sqlite is stupid and doesn't return the number of affected rows
# quick hack because I can't rely on version.pm to be installed everywhere
SKIP: {
skip "SQLite only", 1 if DodTestUtil::driver ne 'SQLite';
my ($sqlite_version) = Wine->driver->rw_handle->{sqlite_version} =~ /(\d+(?:\.\d+))/;
my $count = version->parse("v$sqlite_version") > version->parse("v3.5") ? scalar @bad_wines : "0E0";
is (Wine->remove({}, { nofetch => 1 }), $count, 'removing all bad wine');
}
}
# different utilities
{
my $w1 = Wine->new;
$w1->name("Chateau la pompe");
$w1->insert;
my $w3 = Wine->new;
$w3->name("different");
$w3->insert;
my $w2 = Wine->lookup($w1->id);
ok $w1->is_same($w1);
ok $w2->is_same($w1);
ok $w1->is_same($w2);
ok !$w1->is_same($w3);
ok !$w3->is_same($w2);
like $w1->pk_str, qr/\d+/;
}
# Test the new flag for persistent store insertion
{
my $w = Wine->new(name => 'flag test', rating=> 4);
ok !$w->object_is_stored, "this object needs to be saved!";
$w->save;
ok $w->object_is_stored, "this object is now saved";
my $w2 = Wine->lookup( $w->id );
ok $w2->object_is_stored, "an object fetched from the database is by definition NOT ephemeral";
$w2->remove;
ok !$w2->object_is_stored, "unmarked saved";
}
END {
disconnect_all(qw/Wine Recipe/);
teardown_dbs(qw( global ));
}
|