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
|
# $Id$
use strict;
use Data::Dumper;
use lib 't/lib';
use lib 't/lib/cached';
use Test::More;
use Test::Exception;
use Scalar::Util;
use DodTestUtil;
BEGIN {
DodTestUtil->check_driver;
unless (eval { require Cache::Memory }) {
plan skip_all => 'Tests require Cache::Memory';
}
}
plan tests => 19;
use Recipe;
use Ingredient;
setup_dbs({
global => [ qw( recipes ingredients ) ],
});
my $r = Recipe->new;
$r->title("Spaghetti");
$r->save;
my $i = Ingredient->new;
$i->name("Oregano");
$i->recipe_id($r->recipe_id);
ok( $i->save, "Saved first ingredient" );
$i = Ingredient->new;
$i->name("Salt");
$i->recipe_id($r->recipe_id);
ok( $i->save, "Saved second ingredient" );
$i = Ingredient->new;
$i->name("Onion");
$i->recipe_id($r->recipe_id);
ok( $i->save, "Saved third ingredient" );
my $load_count = 0;
my $trigger = sub { $load_count++ };
Ingredient->add_trigger( 'post_load', $trigger );
$load_count = 0;
Ingredient->driver->clear_cache;
my $iter = Ingredient->search();
$iter->end;
is( $load_count, 3, "Default behavior: load all objects with plain search method" );
$load_count = 0;
Ingredient->driver->clear_cache;
$iter = Ingredient->search( undef, { window_size => 1 });
$i = $iter->();
$iter->end;
is( $load_count, 1, "1 ingredient loaded when window size = 1" );
$load_count = 0;
Ingredient->driver->clear_cache;
$iter = Ingredient->search( undef, { window_size => 2 });
$i = $iter->();
$iter->end;
is( $load_count, 2, "2 ingredients loaded" );
$load_count = 0;
Ingredient->driver->clear_cache;
$iter = Ingredient->search( undef, { window_size => 1, sort => "name", direction => "asc" });
my $i1 = $iter->();
ok($i1, "First row from windowed select returned");
is( $i1->name, "Onion", "Name is 'Onion'" );
my $i2 = $iter->();
ok( $i2, "Second row from windowed select returned");
is( $i2->name, "Oregano", "Name is 'Oregano'" );
ok( $iter->(), "Third row from windowed select returned" );
ok( ! $iter->(), "No more rows, which is okay" );
is( $load_count, 3, "3 objects loaded");
$iter->end;
$load_count = 0;
Ingredient->driver->clear_cache;
$iter = Ingredient->search( undef, { window_size => 5, limit => 2, sort => "name", direction => "asc" });
$i1 = $iter->();
ok($i1, "First row from windowed select returned");
is( $i1->name, "Onion", "Name is 'Onion'" );
$i2 = $iter->();
ok( $i2, "Second row from windowed select returned");
is( $i2->name, "Oregano", "Name is 'Oregano'" );
ok( !$iter->(), "No third row; limit argument respected" );
is( $load_count, 2, "2 objects loaded; limit argument respected");
$iter->end;
disconnect_all($r, $i);
teardown_dbs(qw( global ));
print Dumper( Data::ObjectDriver->profiler->query_log ) if $ENV{DOD_PROFILE};
|