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
|
# $Id: 01-col-inheritance.t 989 2005-09-23 19:58:01Z btrott $
# this is about the same test as t/09-resultset.t, but with lots of peek_next'ing
# going on, to test that new method
use strict;
use lib 't/lib';
$Data::ObjectDriver::DEBUG = 0;
use Test::More;
use DodTestUtil;
BEGIN { DodTestUtil->check_driver }
plan tests => 65;
setup_dbs({
global => [ qw( wines ) ],
});
use Wine;
use Storable;
my $wine = Wine->new;
$wine->name("Saumur Champigny, Le Grand Clos 2001");
$wine->rating(4);
## generate some binary data (SQL_BLOB / MEDIUMBLOB)
my $glouglou = { tanin => "beaucoup", caudalies => "4" };
$wine->binchar("xxx\0yyy");
$wine->content(Storable::nfreeze($glouglou));
ok($wine->save, 'Object saved successfully');
my $iter;
$iter = Data::ObjectDriver::Iterator->new(sub {});
my $wine_id = $wine->id;
undef $wine;
$wine = Wine->lookup($wine_id);
ok $wine;
is_deeply Storable::thaw($wine->content), $glouglou;
SKIP: {
skip "Please upgrade to DBD::SQLite 1.11", 1
if $DBD::SQLite::VERSION < 1.11;
is $wine->binchar, "xxx\0yyy";
};
Wine->bulk_insert(['name', 'rating'], [['Caymus', 4], ['Thunderbird', 1], ['Stags Leap', 3]]);
$wine = undef;
my ($result) = Wine->result({name => 'Caymus'});
is $result->peek_next->name, 'Caymus', 'before we start, peek_next says the first one is Caymus';
ok! $result->is_finished;
$wine = $result->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok ! $result->peek_next, "we're at the end of the set";
ok ! $result->next; #sets is_finished()
ok ! $result->peek_next, "we're *still* at the end of the set";
ok $result->is_finished;
# testing iterator
my ($iterator) = $result->iterator([$wine]);
is $iterator->peek_next->name, 'Caymus', 'before we start, peek_next says the first one is Caymus';
ok(! $iterator->is_finished );
$wine = $iterator->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok ! $iterator->peek_next, "we're at the end of the set";
ok( ! $iterator->next );
ok ! $iterator->peek_next, "we're *still* at the end of the set";
ok( $iterator->is_finished );
# testing bug in iterator, adding a limit where there was one before shouldn't invalidate results
($iterator) = $result->iterator([$wine]);
is $iterator->peek_next->name, 'Caymus', 'before we start, peek_next says the first one is Caymus';
$iterator->add_limit(1);
is $iterator->peek_next->name, 'Caymus', 'after adding limit, peek_next says the first one is Caymus';
ok(! $iterator->is_finished );
$wine = $iterator->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok ! $iterator->peek_next, "we're at the end of the set";
ok ! $iterator->next;
ok ! $iterator->peek_next, "we're *still* at the end of the set";
ok $iterator->is_finished;
($result) = Wine->result({}, { sort => 'name', direction => 'ascend' });
($iterator) = $result->iterator( [ $result->next, $result->next ] );
is $iterator->peek_next->name, 'Caymus', 'before we start, peek_next says the first one is Caymus';
$iterator->add_limit(1);
is $iterator->peek_next->name, 'Caymus', 'after adding limit, peek_next says the first one is Caymus';
ok! $iterator->is_finished ;
$wine = $iterator->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok ! $iterator->peek_next, "we're at the end of the set";
ok ! $iterator->next;
ok ! $iterator->peek_next, "we're *still* at the end of the set";
ok $iterator->is_finished;
# raising the limit should trigger a new search
($result) = Wine->result({}, { sort => 'name', direction => 'ascend' });
($iterator) = $result->iterator( [ $result->next, $result->next ] );
is $iterator->peek_next->name, 'Caymus', 'before we start, peek_next says the first one is Caymus';
$iterator->add_limit(9999);
is $iterator->peek_next->name, 'Caymus', 'after adding limit, peek_next says the first one is Caymus';
ok! $iterator->is_finished;
$wine = $iterator->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok $iterator->peek_next, "more to go";
ok $iterator->next, 'more to go';
ok ! $iterator->peek_next, "that was the last one, there are no more";
ok ! $iterator->is_finished, "we're not finished";
ok ! $iterator->next; #sets is_finished()
ok ! $iterator->peek_next, "that was the last one, there are no more";
ok $iterator->is_finished, "now we are finished";
# testing limit in args
($result) = Wine->result({}, { limit => 2, sort => 'name', direction => 'ascend' });
is $result->peek_next->name, 'Caymus', 'before we start, peek_next says the first one is Caymus';
ok! $result->is_finished ;
$wine = $result->next;
is $wine->name, 'Caymus';
is $result->peek_next->name, 'Saumur Champigny, Le Grand Clos 2001', 'the next one will be Saumur';
$wine = $result->next;
is $wine->name, 'Saumur Champigny, Le Grand Clos 2001';
ok ! $result->peek_next, "Saumur was the last one";
ok ! $result->next;
ok $result->is_finished;
ok ! $result->peek_next, "Saumur was really the last one";
# raising the limit should trigger a new search
($result) = Wine->result({}, { limit => 2, sort => 'name', direction => 'ascend' });
$result->add_limit(3);
is $result->next->name, 'Caymus';
is $result->peek_next->name, 'Saumur Champigny, Le Grand Clos 2001', 'the next one will be Saumur';
is $result->next->name, 'Saumur Champigny, Le Grand Clos 2001';
is $result->peek_next->name, 'Stags Leap', 'the next one will be Stags Leap';
is $result->next->name, 'Stags Leap';
ok ! $result->peek_next, "Stags Leap was the last one";
disconnect_all($wine);
teardown_dbs(qw( global ));
|