This repository was archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcursor.rs
84 lines (67 loc) · 2.35 KB
/
cursor.rs
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
use bson::{Bson, Document};
use mongodb::{Client, CommandType, ThreadedClient};
use mongodb::common::{ReadMode, ReadPreference};
use mongodb::coll::options::FindOptions;
use mongodb::db::ThreadedDatabase;
use mongodb::cursor::Cursor;
use mongodb::wire_protocol::flags::OpQueryFlags;
#[test]
fn cursor_features() {
let client = Client::connect("localhost", 27017).unwrap();
let db = client.db("test-client-cursor");
let coll = db.collection("cursor_test");
coll.drop().expect("Failed to drop collection.");
let docs = (0..10)
.map(|i| {
doc! { "foo": i as i64 }
})
.collect();
assert!(coll.insert_many(docs, None).is_ok());
let doc = Document::new();
let flags = OpQueryFlags::empty();
let mut options = FindOptions::new();
options.batch_size = Some(3);
let result = Cursor::query(
client.clone(),
"test-client-cursor.cursor_test".to_owned(),
flags,
doc,
options,
CommandType::Find,
false,
ReadPreference::new(ReadMode::Primary, None),
);
let mut cursor = match result {
Ok(c) => c,
Err(s) => panic!("{}", s),
};
let batch = cursor.drain_current_batch().expect(
"Failed to get current batch from cursor.",
);
assert_eq!(batch.len(), 3 as usize);
for (i, item) in batch.iter().enumerate() {
match item.get("foo") {
Some(&Bson::I64(j)) => assert_eq!(i as i64, j),
_ => panic!("Wrong value returned from Cursor#next_batch"),
};
}
let bson = match cursor.next() {
Some(Ok(b)) => b,
Some(Err(_)) => panic!("Received error on 'cursor.next()'"),
None => panic!("Nothing returned from Cursor#next"),
};
match bson.get("foo") {
Some(&Bson::I64(3)) => (),
_ => panic!("Wrong value returned from Cursor#next"),
};
assert!(cursor.has_next().expect("Failed to execute 'has_next()'."));
let vec = cursor.next_n(20).expect("Failed to get next 20 results.");
assert_eq!(vec.len(), 6 as usize);
assert!(!cursor.has_next().expect("Failed to execute 'has_next()'."));
for (i, item) in vec.iter().enumerate() {
match item.get("foo") {
Some(&Bson::I64(j)) => assert_eq!(4 + i as i64, j),
_ => panic!("Wrong value returned from Cursor#next_batch"),
};
}
}