File: 09-resultset.t

package info (click to toggle)
libdata-objectdriver-perl 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 784 kB
  • sloc: perl: 3,795; sql: 64; makefile: 7
file content (176 lines) | stat: -rw-r--r-- 4,853 bytes parent folder | download | duplicates (3)
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
# $Id: 01-col-inheritance.t 989 2005-09-23 19:58:01Z btrott $

use strict;

use lib 't/lib';

$Data::ObjectDriver::DEBUG = 0;
use Test::More;
use DodTestUtil;

BEGIN { DodTestUtil->check_driver }

plan tests => 50;

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]]);


{
    my $result = Wine->result({});

    my $objs = $result->slice(0, 100);
    is @$objs, 4;

    my $rs = $result->slice(0, 2);
    is @$rs, 3;
    for my $r (@$rs) {
        isa_ok $r, 'Wine';
    }
}

$wine = undef; 
my ($result) = Wine->result({name => 'Caymus'});
ok! $result->is_finished;
$wine = $result->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok ! $result->next; #sets is_finished()
ok $result->is_finished;

# testing iterator
my ($iterator) = $result->iterator([$wine]);
ok(! $iterator->is_finished );
$wine = $iterator->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok( ! $iterator->next ); 
ok( $iterator->is_finished );

# testing bug in iterator, adding a limit where there was one before shouldn't invalidate results
($iterator) = $result->iterator([$wine]);
$iterator->add_limit(1);
ok(! $iterator->is_finished );
$wine = $iterator->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok ! $iterator->next; 
ok $iterator->is_finished;


($result) = Wine->result({}, { sort => 'name', direction => 'ascend' });
($iterator) = $result->iterator( [ $result->next, $result->next ] );
$iterator->add_limit(1);
ok! $iterator->is_finished ;
$wine = $iterator->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok ! $iterator->next; 
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 ] );
$iterator->add_limit(9999);
ok! $iterator->is_finished;
$wine = $iterator->next;
ok $wine, 'Found Caymus';
is $wine->name, 'Caymus';
ok $iterator->next, 'more to go';
ok ! $iterator->is_finished, "we're not finished";


# testing limit in args
($result) = Wine->result({}, { limit => 2, sort => 'name', direction => 'ascend' });
ok! $result->is_finished ;
$wine = $result->next;
is $wine->name, 'Caymus';
$wine = $result->next;
is $wine->name, 'Saumur Champigny, Le Grand Clos 2001';
ok ! $result->next; 
ok $result->is_finished;

# 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->next->name, 'Saumur Champigny, Le Grand Clos 2001';
is $result->next->name, 'Stags Leap';

# test slice again with _results_loaded
$result->rewind;
{
    my $rs = $result->slice(0, 2);
    for my $r (@$rs) {
        isa_ok $r, 'Wine';
    }

    my $objs;
    $objs = $result->slice(0, 100);
    is @$objs, 3;

    $objs = $result->slice(5, 10);
    is @$objs, 0;
}

# test add_term
{
    my $result = Wine->result({rating => { op => '<=', 'value' => 4}}, { sort => 'rating', direction => 'descend' });
    $result->add_term({rating => 3});
    is $result->next->rating, 3;
}
## now call add_term after loading objects
{
    my $result = Wine->result({rating => { op => '<=', 'value' => 4}}, { sort => 'rating', direction => 'descend' });
    $result->_load_results;
    $result->add_term({rating => 3});
    is $result->next->rating, 3;
}
## filtering with 'op', which does work if objects are not loaded
{
    my $result = Wine->result({rating => { op => '<=', 'value' => 4}}, { sort => 'rating', direction => 'descend' });
    $result->add_term({rating => { op => '<=', 'value' => 3}});
    is $result->next->rating, 3;
}
## filtering with 'op', which doesn't work now.
{
    my $result = Wine->result({rating => { op => '<=', 'value' => 4}}, { sort => 'rating', direction => 'descend' });
    $result->_load_results;
    $result->add_term({rating => { op => '<=', 'value' => 3}});
    note "calling next() after add_term() with 'op'" . $result->next; ## this should return the object which has "rating == 3".
}

disconnect_all($wine);
teardown_dbs(qw( global ));