This repository has been archived by the owner on Dec 11, 2017. It is now read-only.
/
cursor.t
171 lines (156 loc) · 4.5 KB
/
cursor.t
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
use Mojo::Base -strict;
use Test::More;
use Mango;
use Mojo::IOLoop;
plan skip_all => 'set TEST_ONLINE to enable this test'
unless $ENV{TEST_ONLINE};
# Add some documents to fetch
my $mango = Mango->new($ENV{TEST_ONLINE});
my $collection = $mango->db->collection('cursor_test');
$collection->remove;
my $oids = $collection->insert([{test => 3}, {test => 1}, {test => 2}]);
is scalar @$oids, 3, 'three documents inserted';
# Fetch documents blocking
my $cursor = $collection->find({})->batch_size(2);
my @docs;
ok !$cursor->id, 'no cursor id';
push @docs, $cursor->next;
ok $cursor->id, 'cursor has id';
push @docs, $cursor->next;
push @docs, $cursor->next;
ok !$cursor->next, 'no more documents';
@docs = sort { $a->{test} <=> $b->{test} } @docs;
is $docs[0]{test}, 1, 'right document';
is $docs[1]{test}, 2, 'right document';
is $docs[2]{test}, 3, 'right document';
# Fetch all documents blocking
my $docs = $collection->find({})->batch_size(2)->all;
@$docs = sort { $a->{test} <=> $b->{test} } @$docs;
is $docs->[0]{test}, 1, 'right document';
is $docs->[1]{test}, 2, 'right document';
is $docs->[2]{test}, 3, 'right document';
# Fetch two documents blocking
$docs = $collection->find({})->limit(2)->sort({test => 1})->all;
@$docs = sort { $a->{test} <=> $b->{test} } @$docs;
is scalar @$docs, 2, 'two documents';
is $docs->[0]{test}, 1, 'right document';
is $docs->[1]{test}, 2, 'right document';
# Count documents blocking
is $collection->find({foo => 'bar'})->count, 0, 'no documents';
is $collection->find({})->skip(1)->limit(1)->count, 1, 'one document';
is $collection->find({})->count, 3, 'three documents';
# Count documents non-blocking
my ($fail, @count);
my $delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$collection->find({})->count($delay->begin);
},
sub {
my ($delay, $err, $count) = @_;
$fail = $err;
push @count, $count;
$collection->find({foo => 'bar'})->count($delay->begin);
},
sub {
my ($delay, $err, $count) = @_;
$fail ||= $err;
push @count, $count;
}
);
$delay->wait;
ok !$mango->is_active, 'no operations in progress';
ok !$fail, 'no error';
is_deeply \@count, [3, 0], 'right number of documents';
# Fetch documents non-blocking
$cursor = $collection->find({})->batch_size(2);
@docs = ();
$fail = undef;
$delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
$fail = $err;
push @docs, $doc;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
$fail ||= $err;
push @docs, $doc;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
$fail ||= $err;
push @docs, $doc;
}
);
$delay->wait;
ok !$mango->is_active, 'no operations in progress';
ok !$fail, 'no error';
@docs = sort { $a->{test} <=> $b->{test} } @docs;
is $docs[0]{test}, 1, 'right document';
is $docs[1]{test}, 2, 'right document';
is $docs[2]{test}, 3, 'right document';
# Fetch all documents non-blocking
@docs = ();
$collection->find({})->batch_size(2)->all(
sub {
my ($collection, $err, $docs) = @_;
@docs = @$docs;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$mango->is_active, 'no operations in progress';
@docs = sort { $a->{test} <=> $b->{test} } @docs;
is $docs[0]{test}, 1, 'right document';
is $docs[1]{test}, 2, 'right document';
is $docs[2]{test}, 3, 'right document';
# Fetch subset of documents sorted
$docs = $collection->find({})->fields({_id => 0})->sort({test => 1})->all;
is_deeply $docs, [{test => 1}, {test => 2}, {test => 3}], 'right subset';
# Rewind cursor blocking
$cursor = $collection->find({});
ok !$cursor->id, 'no cursor id';
$cursor->rewind;
my $doc = $cursor->next;
ok $doc, 'found a document';
$cursor->rewind;
is_deeply $cursor->next, $doc, 'found same document again';
# Rewind cursor non-blocking
$fail = undef;
@docs = ();
$cursor = $collection->find({});
$delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
$fail = $err;
push @docs, $doc;
$cursor->rewind($delay->begin);
},
sub {
my $delay = shift;
$cursor->next($delay->begin);
},
sub {
my ($delay, $err, $doc) = @_;
$fail ||= $err;
push @docs, $doc;
},
);
$delay->wait;
ok !$mango->is_active, 'no operations in progress';
ok !$fail, 'no error';
is_deeply $docs[0], $docs[1], 'found same document again';
# Remove all documents from collection
is $collection->remove->{n}, 3, 'three documents removed';
done_testing();