This repository has been archived by the owner on Dec 11, 2017. It is now read-only.
/
collection.t
468 lines (434 loc) · 14.8 KB
/
collection.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use Mojo::Base -strict;
use Test::More;
plan skip_all => 'set TEST_ONLINE to enable this test'
unless $ENV{TEST_ONLINE};
use Mango;
use Mango::BSON qw(bson_code bson_doc bson_oid bson_true);
use Mojo::IOLoop;
# Clean up before start
my $mango = Mango->new($ENV{TEST_ONLINE});
my $collection = $mango->db->collection('collection_test');
$collection->drop
if grep { $_ eq 'collection_test' } @{$mango->db->collection_names};
# Collection names
is $collection->name, 'collection_test', 'right collection name';
is $collection->full_name, join('.', $mango->db->name, $collection->name),
'right full collection name';
# Index names
is $collection->build_index_name({foo => 1}), 'foo', 'right index name';
is $collection->build_index_name(bson_doc(foo => 1, bar => -1)), 'foo_bar',
'right index name';
is $collection->build_index_name(bson_doc(foo => 1, 'bar.baz' => -1)),
'foo_bar.baz', 'right index name';
is $collection->build_index_name(bson_doc(foo => 1, bar => -1, baz => '2d')),
'foo_bar_baz', 'right index name';
# Insert documents blocking
my $oids = $collection->insert([{foo => 'bar'}, {foo => 'baz'}]);
isa_ok $oids->[0], 'Mango::BSON::ObjectID', 'right class';
isa_ok $oids->[1], 'Mango::BSON::ObjectID', 'right class';
is $collection->find_one($oids->[0])->{foo}, 'bar', 'right value';
is $collection->find_one($oids->[1])->{foo}, 'baz', 'right value';
# Get collection statistics blocking
is $collection->stats->{count}, 2, 'right number of documents';
# Get collection statistics non-blocking
my ($fail, $result) = @_;
$collection->stats(
sub {
my ($collection, $err, $stats) = @_;
$fail = $err;
$result = $stats;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is $result->{count}, 2, 'right number of documents';
# Update documents blocking
is $collection->update({}, {'$set' => {bar => 'works'}}, {multi => 1})->{n},
2, 'two documents updated';
is $collection->update({}, {'$set' => {baz => 'too'}})->{n}, 1,
'one document updated';
is $collection->find_one($oids->[0])->{bar}, 'works', 'right value';
is $collection->find_one($oids->[1])->{bar}, 'works', 'right value';
is $collection->update({missing => 1}, {now => 'there'}, {upsert => 1})->{n},
1, 'one document updated';
is $collection->update({missing => 1}, {now => 'there'}, {upsert => 1})->{n},
1, 'one document updated';
is $collection->remove({now => 'there'}, {single => 1})->{n}, 1,
'one document removed';
is $collection->remove({now => 'there'}, {single => 1})->{n}, 1,
'one document removed';
# Remove one document blocking
is $collection->remove({foo => 'baz'})->{n}, 1, 'one document removed';
ok $collection->find_one($oids->[0]), 'document still exists';
ok !$collection->find_one($oids->[1]), 'no document';
is $collection->remove->{n}, 1, 'one document removed';
ok !$collection->find_one($oids->[0]), 'no document';
# Find and modify document blocking
my $oid = $collection->insert({atomic => 1});
is $collection->find_one($oid)->{atomic}, 1, 'right document';
my $doc = $collection->find_and_modify(
{query => {atomic => 1}, update => {'$set' => {atomic => 2}}});
is $doc->{atomic}, 1, 'right document';
is $collection->find_one($oid)->{atomic}, 2, 'right document';
is $collection->remove({atomic => 2})->{n}, 1, 'removed one document';
# Find and modify document non-blocking
$oid = $collection->insert({atomic => 1});
is $collection->find_one($oid)->{atomic}, 1, 'right document';
($fail, $result) = ();
$collection->find_and_modify(
{query => {atomic => 1}, update => {'$set' => {atomic => 2}}} => sub {
my ($collection, $err, $doc) = @_;
$fail = $err;
$result = $doc;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is $result->{atomic}, 1, 'right document';
is $collection->find_one($oid)->{atomic}, 2, 'right document';
is $collection->remove({atomic => 2})->{n}, 1, 'removed one document';
# Aggregate collection blocking
$collection->insert([{more => 1}, {more => 2}, {more => 3}]);
my $docs = $collection->aggregate(
[{'$group' => {_id => undef, total => {'$sum' => '$more'}}}]);
is $docs->[0]{total}, 6, 'right result';
is $collection->remove({more => {'$exists' => 1}})->{n}, 3,
'three documents removed';
# Aggregate collection non-blocking
$collection->insert([{more => 1}, {more => 2}, {more => 3}]);
($fail, $result) = ();
$collection->aggregate(
[{'$group' => {_id => undef, total => {'$sum' => '$more'}}}] => sub {
my ($collection, $err, $docs) = @_;
$fail = $err;
$result = $docs;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is $result->[0]{total}, 6, 'right result';
is $collection->remove({more => {'$exists' => 1}})->{n}, 3,
'three documents removed';
# Aggregate with cursors and collections
SKIP: {
my $version = $mango->db->command('buildInfo')->{versionArray};
skip 'MongoDB 2.5 required!', 11 unless join('.', @$version[0, 1]) >= '2.5';
# Aggregate with cursor
$collection->insert({stuff => $_}) for 1 .. 30;
my $cursor = $collection->aggregate([{'$match' => {stuff => {'$gt' => 0}}}],
{cursor => {}});
ok !$cursor->id, 'no cursor id';
is scalar @{$cursor->all}, 30, 'thirty documents found';
is $collection->remove->{n}, 30, 'thirty documents removed';
# Aggregate with collections
$collection->insert({stuff => $_}) for 1 .. 30;
my $out = $collection->aggregate(
[
{'$match' => {stuff => {'$gt' => 0}}},
{'$out' => 'collection_test_results'}
]
);
is $out->name, 'collection_test_results', 'right name';
is $out->find->count, 30, 'thirty documents found';
$out->drop;
is $collection->remove->{n}, 30, 'thirty documents removed';
# Aggregate with cursor blocking (multiple batches)
$collection->insert({stuff => $_}) for 1 .. 30;
$cursor = $collection->aggregate([{'$match' => {stuff => {'$gt' => 0}}}],
{cursor => {batchSize => 5}});
ok $cursor->id, 'cursor has id';
is scalar @{$cursor->all}, 30, 'thirty documents found';
is $collection->remove->{n}, 30, 'thirty documents removed';
# Aggregate with cursor non-blocking (multiple batches)
$collection->insert({stuff => $_}) for 1 .. 30;
($fail, $result) = ();
my $delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$collection->aggregate(
[{'$match' => {stuff => {'$gt' => 0}}}],
{cursor => {batchSize => 5}},
$delay->begin
);
},
sub {
my ($delay, $err, $cursor) = @_;
$fail = $err;
$cursor->all($delay->begin);
},
sub {
my ($delay, $err, $docs) = @_;
$fail ||= $err;
$result = $docs;
}
);
$delay->wait;
is scalar @$result, 30, 'thirty documents found';
is $collection->remove->{n}, 30, 'thirty documents removed';
}
# Save document blocking
$oid = $collection->save({update => 'me'});
$doc = $collection->find_one($oid);
is $doc->{update}, 'me', 'right document';
$doc->{update} = 'too';
is $collection->save($doc), $oid, 'same object id';
$doc = $collection->find_one($oid);
is $doc->{update}, 'too', 'right document';
is $collection->remove({_id => $oid})->{n}, 1, 'one document removed';
$oid = bson_oid;
$doc = bson_doc _id => $oid, save => 'me';
is $collection->save($doc), $oid, 'same object id';
$doc = $collection->find_one($oid);
is $doc->{save}, 'me', 'right document';
is $collection->remove({_id => $oid})->{n}, 1, 'one document removed';
# Save document non-blocking
($fail, $result) = ();
$collection->save(
{update => 'me'} => sub {
my ($collection, $err, $oid) = @_;
$fail = $err;
$result = $oid;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
$doc = $collection->find_one($result);
is $doc->{update}, 'me', 'right document';
$doc->{update} = 'too';
$oid = $result;
($fail, $result) = ();
$collection->save(
$doc => sub {
my ($collection, $err, $oid) = @_;
$fail = $err;
$result = $oid;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is $oid, $result, 'same object id';
$doc = $collection->find_one($oid);
is $doc->{update}, 'too', 'right document';
is $collection->remove({_id => $oid})->{n}, 1, 'one document removed';
$oid = bson_oid;
$doc = bson_doc _id => $oid, save => 'me';
($fail, $result) = ();
$collection->save(
$doc => sub {
my ($collection, $err, $oid) = @_;
$fail = $err;
$result = $oid;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is $oid, $result, 'same object id';
$doc = $collection->find_one($oid, {_id => 0});
is_deeply $doc, {save => 'me'}, 'right document';
is $collection->remove({_id => $oid})->{n}, 1, 'one document removed';
# Drop collection blocking
$oid = $collection->insert({just => 'works'});
is $collection->find_one($oid)->{just}, 'works', 'right document';
$collection->drop;
ok !$collection->find_one($oid), 'no document';
# Drop collection non-blocking
$oid = $collection->insert({just => 'works'});
is $collection->find_one($oid)->{just}, 'works', 'right document';
$fail = undef;
$collection->drop(
sub {
my ($collection, $err) = @_;
$fail = $err;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
ok !$collection->find_one($oid), 'no document';
# Ensure and drop index blocking
$collection->insert({test => 23, foo => 'bar'});
$collection->insert({test => 23, foo => 'baz'});
is $collection->find->count, 2, 'two documents';
$collection->ensure_index({test => 1}, {unique => \1, dropDups => \1});
is $collection->find->count, 1, 'one document';
is $collection->index_information->{test}{unique}, bson_true,
'index is unique';
$collection->drop_index('test');
is $collection->index_information->{test}, undef, 'no index';
$collection->drop;
# Ensure and drop index non-blocking
$collection->insert({test => 23, foo => 'bar'});
$collection->insert({test => 23, foo => 'baz'});
is $collection->find->count, 2, 'two documents';
($fail, $result) = ();
my $delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$collection->ensure_index(
({test => 1}, {unique => \1, dropDups => \1}) => $delay->begin);
},
sub {
my ($delay, $err) = @_;
$fail = $err;
$collection->index_information($delay->begin);
},
sub {
my ($delay, $err, $info) = @_;
$fail ||= $err;
$result = $info;
}
);
$delay->wait;
ok !$fail, 'no error';
is $collection->find->count, 1, 'one document';
is $result->{test}{unique}, bson_true, 'index is unique';
($fail, $result) = ();
$delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$collection->drop_index(test => $delay->begin);
},
sub {
my ($delay, $err) = @_;
$fail = $err;
$collection->index_information($delay->begin);
},
sub {
my ($delay, $err, $info) = @_;
$fail ||= $err;
$result = $info;
}
);
$delay->wait;
ok !$fail, 'no error';
is $result->{test}, undef, 'no index';
$collection->drop;
# Create capped collection blocking
$collection->create({capped => \1, max => 2, size => 100000});
$collection->insert([{test => 1}, {test => 2}]);
is $collection->find({})->count, 2, 'two documents';
$collection->insert({test => 3});
is $collection->find->count, 2, 'two documents';
$collection->drop;
# Create capped collection non-blocking
$fail = undef;
$collection->create(
{capped => \1, max => 2, size => 100000} => sub {
my ($collection, $err) = @_;
$fail = $err;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
$collection->insert([{test => 1}, {test => 2}]);
is $collection->find({})->count, 2, 'two documents';
$collection->insert({test => 3});
is $collection->find->count, 2, 'two documents';
$collection->drop;
# Perform map/reduce blocking
my $map = <<EOF;
function () {
this.tags.forEach(function(z) {
emit(z, 1);
});
}
EOF
my $reduce = <<EOF;
function (key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i];
}
return total;
}
EOF
$collection->insert({x => 1, tags => [qw(dog cat)]});
$collection->insert({x => 2, tags => ['cat']});
$collection->insert({x => 3, tags => [qw(mouse cat dog)]});
$collection->insert({x => 4, tags => []});
my $out
= $collection->map_reduce($map, $reduce, {out => 'collection_test_results'});
$collection->drop;
$docs = $out->find->sort({value => -1})->all;
is_deeply $docs->[0], {_id => 'cat', value => 3}, 'right document';
is_deeply $docs->[1], {_id => 'dog', value => 2}, 'right document';
is_deeply $docs->[2], {_id => 'mouse', value => 1}, 'right document';
$out->drop;
# Perform map/reduce non-blocking
$collection->insert({x => 1, tags => [qw(dog cat)]});
$collection->insert({x => 2, tags => ['cat']});
$collection->insert({x => 3, tags => [qw(mouse cat dog)]});
$collection->insert({x => 4, tags => []});
($fail, $result) = ();
$collection->map_reduce(
($map, $reduce, {out => 'collection_test_results'}) => sub {
my ($collection, $err, $out) = @_;
$fail = $err;
$result = $out;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
$collection->drop;
$docs = $result->find->sort({value => -1})->all;
is_deeply $docs->[0], {_id => 'cat', value => 3}, 'right document';
is_deeply $docs->[1], {_id => 'dog', value => 2}, 'right document';
is_deeply $docs->[2], {_id => 'mouse', value => 1}, 'right document';
$result->drop;
# Perform inline map/reduce blocking
$collection->insert({x => 1, tags => [qw(dog cat)]});
$collection->insert({x => 2, tags => ['cat']});
$collection->insert({x => 3, tags => [qw(mouse cat dog)]});
$collection->insert({x => 4, tags => []});
$docs = $collection->map_reduce(bson_code($map), bson_code($reduce),
{out => {inline => 1}});
$collection->drop;
is_deeply $docs->[0], {_id => 'cat', value => 3}, 'right document';
is_deeply $docs->[1], {_id => 'dog', value => 2}, 'right document';
is_deeply $docs->[2], {_id => 'mouse', value => 1}, 'right document';
# Perform inline map/reduce non-blocking
$collection->insert({x => 1, tags => [qw(dog cat)]});
$collection->insert({x => 2, tags => ['cat']});
$collection->insert({x => 3, tags => [qw(mouse cat dog)]});
$collection->insert({x => 4, tags => []});
($fail, $result) = ();
$collection->map_reduce(
(bson_code($map), bson_code($reduce), {out => {inline => 1}}) => sub {
my ($collection, $err, $docs) = @_;
$fail = $err;
$result = $docs;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
$collection->drop;
is_deeply $result->[0], {_id => 'cat', value => 3}, 'right document';
is_deeply $result->[1], {_id => 'dog', value => 2}, 'right document';
is_deeply $result->[2], {_id => 'mouse', value => 1}, 'right document';
# Interrupted non-blocking remove
my $port = Mojo::IOLoop->generate_port;
$mango = Mango->new("mongodb://localhost:$port");
my $id = Mojo::IOLoop->server((port => $port) => sub { $_[1]->close });
($fail, $result) = ();
$mango->db->collection('collection_test')->remove(
sub {
my ($collection, $err, $doc) = @_;
$fail = $err;
$result = $doc;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
Mojo::IOLoop->remove($id);
like $fail, qr/Premature connection close/, 'right error';
ok !$result->{n}, 'remove was not successful';
done_testing();