| •1 |
use crate::{ |
| 2 |
doc, |
| 3 |
document::ValueAccessError, |
| 4 |
oid::ObjectId, |
| 5 |
spec::BinarySubtype, |
| 6 |
tests::LOCK, |
| 7 |
Binary, |
| 8 |
Bson, |
| 9 |
Document, |
| 10 |
Timestamp, |
| 11 |
}; |
| 12 |
use time::OffsetDateTime; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn ordered_insert() { |
| 16 |
let _guard = LOCK.run_concurrently(); |
| 17 |
let mut doc = Document::new(); |
| 18 |
doc.insert("first".to_owned(), Bson::Int32(1)); |
| 19 |
doc.insert("second".to_owned(), Bson::String("foo".to_owned())); |
| 20 |
doc.insert("alphanumeric".to_owned(), Bson::String("bar".to_owned())); |
| 21 |
|
| 22 |
let expected_keys = vec![ |
| 23 |
"first".to_owned(), |
| 24 |
"second".to_owned(), |
| 25 |
"alphanumeric".to_owned(), |
| 26 |
]; |
| 27 |
|
| 28 |
let keys: Vec<_> = doc.iter().map(|(key, _)| key.to_owned()).collect(); |
| 29 |
assert_eq!(expected_keys, keys); |
| 30 |
} |
| 31 |
|
| 32 |
#[test] |
| 33 |
fn ordered_insert_shorthand() { |
| 34 |
let _guard = LOCK.run_concurrently(); |
| 35 |
let mut doc = Document::new(); |
| 36 |
doc.insert("first", 1i32); |
| 37 |
doc.insert("second", "foo"); |
| 38 |
doc.insert("alphanumeric", "bar".to_owned()); |
| 39 |
|
| 40 |
let expected_keys = vec![ |
| 41 |
"first".to_owned(), |
| 42 |
"second".to_owned(), |
| 43 |
"alphanumeric".to_owned(), |
| 44 |
]; |
| 45 |
|
| 46 |
let keys: Vec<_> = doc.iter().map(|(key, _)| key.to_owned()).collect(); |
| 47 |
assert_eq!(expected_keys, keys); |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn test_getters() { |
| 52 |
let _guard = LOCK.run_concurrently(); |
| 53 |
let datetime = OffsetDateTime::now_utc(); |
| 54 |
let cloned_dt = crate::DateTime::from_time_0_3(datetime); |
| 55 |
let binary = vec![0, 1, 2, 3, 4]; |
| 56 |
let mut doc = doc! { |
| 57 |
"floating_point": 10.0, |
| 58 |
"string": "a value", |
| 59 |
"array": [10, 20, 30], |
| 60 |
"doc": { "key": 1 }, |
| 61 |
"bool": true, |
| 62 |
"i32": 1i32, |
| 63 |
"i64": 1i64, |
| 64 |
"datetime": cloned_dt, |
| 65 |
"binary": Binary { subtype: BinarySubtype::Generic, bytes: binary.clone() } |
| 66 |
}; |
| 67 |
|
| 68 |
assert_eq!(None, doc.get("nonsense")); |
| 69 |
assert_eq!(Err(ValueAccessError::NotPresent), doc.get_str("nonsense")); |
| 70 |
assert_eq!( |
| 71 |
Err(ValueAccessError::UnexpectedType), |
| 72 |
doc.get_str("floating_point") |
| 73 |
); |
| 74 |
|
| 75 |
assert_eq!(Some(&Bson::Double(10.0)), doc.get("floating_point")); |
| 76 |
assert_eq!(Ok(10.0), doc.get_f64("floating_point")); |
| 77 |
|
| 78 |
assert_eq!( |
| 79 |
Some(&Bson::String("a value".to_string())), |
| 80 |
doc.get("string") |
| 81 |
); |
| 82 |
assert_eq!(Ok("a value"), doc.get_str("string")); |
| 83 |
|
| 84 |
let array = vec![Bson::Int32(10), Bson::Int32(20), Bson::Int32(30)]; |
| 85 |
assert_eq!(Some(&Bson::Array(array.clone())), doc.get("array")); |
| 86 |
assert_eq!(Ok(&array), doc.get_array("array")); |
| 87 |
|
| 88 |
let embedded = doc! { "key": 1 }; |
| 89 |
assert_eq!(Some(&Bson::Document(embedded.clone())), doc.get("doc")); |
| 90 |
assert_eq!(Ok(&embedded), doc.get_document("doc")); |
| 91 |
|
| 92 |
assert_eq!(Some(&Bson::Boolean(true)), doc.get("bool")); |
| 93 |
assert_eq!(Ok(true), doc.get_bool("bool")); |
| 94 |
|
| 95 |
doc.insert("null".to_string(), Bson::Null); |
| 96 |
assert_eq!(Some(&Bson::Null), doc.get("null")); |
| 97 |
assert!(doc.is_null("null")); |
| 98 |
assert!(!doc.is_null("array")); |
| 99 |
|
| 100 |
assert_eq!(Some(&Bson::Int32(1)), doc.get("i32")); |
| 101 |
assert_eq!(Ok(1i32), doc.get_i32("i32")); |
| 102 |
|
| 103 |
assert_eq!(Some(&Bson::Int64(1)), doc.get("i64")); |
| 104 |
assert_eq!(Ok(1i64), doc.get_i64("i64")); |
| 105 |
|
| 106 |
doc.insert( |
| 107 |
"timestamp".to_string(), |
| 108 |
Bson::Timestamp(Timestamp { |
| 109 |
time: 0, |
| 110 |
increment: 100, |
| 111 |
}), |
| 112 |
); |
| 113 |
assert_eq!( |
| 114 |
Some(&Bson::Timestamp(Timestamp { |
| 115 |
time: 0, |
| 116 |
increment: 100 |
| 117 |
})), |
| 118 |
doc.get("timestamp") |
| 119 |
); |
| 120 |
assert_eq!( |
| 121 |
Ok(Timestamp { |
| 122 |
time: 0, |
| 123 |
increment: 100, |
| 124 |
}), |
| 125 |
doc.get_timestamp("timestamp") |
| 126 |
); |
| 127 |
|
| 128 |
let dt = crate::DateTime::from_time_0_3(datetime); |
| 129 |
assert_eq!(Some(&Bson::DateTime(dt)), doc.get("datetime")); |
| 130 |
assert_eq!(Ok(&dt), doc.get_datetime("datetime")); |
| 131 |
|
| 132 |
let object_id = ObjectId::new(); |
| 133 |
doc.insert("_id".to_string(), Bson::ObjectId(object_id)); |
| 134 |
assert_eq!(Some(&Bson::ObjectId(object_id)), doc.get("_id")); |
| 135 |
assert_eq!(Ok(object_id), doc.get_object_id("_id")); |
| 136 |
|
| 137 |
assert_eq!( |
| 138 |
Some(&Bson::Binary(Binary { |
| 139 |
subtype: BinarySubtype::Generic, |
| 140 |
bytes: binary.clone() |
| 141 |
})), |
| 142 |
doc.get("binary") |
| 143 |
); |
| 144 |
assert_eq!(Ok(&binary), doc.get_binary_generic("binary")); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn remove() { |
| 149 |
let _guard = LOCK.run_concurrently(); |
| 150 |
|
| 151 |
let mut doc = Document::new(); |
| 152 |
doc.insert("first", 1i32); |
| 153 |
doc.insert("second", "foo"); |
| 154 |
doc.insert("third", "bar".to_owned()); |
| 155 |
doc.insert("fourth", "bar".to_owned()); |
| 156 |
|
| 157 |
let mut expected_keys = vec![ |
| 158 |
"first".to_owned(), |
| 159 |
"second".to_owned(), |
| 160 |
"third".to_owned(), |
| 161 |
"fourth".to_owned(), |
| 162 |
]; |
| 163 |
|
| 164 |
let keys: Vec<_> = doc.iter().map(|(key, _)| key.to_owned()).collect(); |
| 165 |
assert_eq!(expected_keys, keys); |
| 166 |
|
| 167 |
assert_eq!(doc.remove("none"), None); |
| 168 |
|
| 169 |
assert!(doc.remove("second").is_some()); |
| 170 |
expected_keys.remove(1); |
| 171 |
let keys: Vec<_> = doc.iter().map(|(key, _)| key.to_owned()).collect(); |
| 172 |
assert_eq!(keys, expected_keys); |
| 173 |
|
| 174 |
assert!(doc.remove("first").is_some()); |
| 175 |
expected_keys.remove(0); |
| 176 |
let keys: Vec<_> = doc.iter().map(|(key, _)| key.to_owned()).collect(); |
| 177 |
assert_eq!(keys, expected_keys); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn entry() { |
| 182 |
let _guard = LOCK.run_concurrently(); |
| 183 |
let mut doc = doc! { |
| 184 |
"first": 1i32, |
| 185 |
"second": "foo", |
| 186 |
"alphanumeric": "bar", |
| 187 |
}; |
| 188 |
|
| 189 |
{ |
| 190 |
let first_entry = doc.entry("first".to_owned()); |
| 191 |
assert_eq!(first_entry.key(), "first"); |
| 192 |
|
| 193 |
let v = first_entry.or_insert_with(|| { |
| 194 |
Bson::Timestamp(Timestamp { |
| 195 |
time: 0, |
| 196 |
increment: 27, |
| 197 |
}) |
| 198 |
}); |
| 199 |
assert_eq!(v, &mut Bson::Int32(1)); |
| 200 |
} |
| 201 |
|
| 202 |
{ |
| 203 |
let fourth_entry = doc.entry("fourth".to_owned()); |
| 204 |
assert_eq!(fourth_entry.key(), "fourth"); |
| 205 |
|
| 206 |
let v = fourth_entry.or_insert(Bson::Null); |
| 207 |
assert_eq!(v, &mut Bson::Null); |
| 208 |
} |
| 209 |
|
| 210 |
assert_eq!( |
| 211 |
doc, |
| 212 |
doc! { |
| 213 |
"first": 1i32, |
| 214 |
"second": "foo", |
| 215 |
"alphanumeric": "bar", |
| 216 |
"fourth": Bson::Null, |
| 217 |
}, |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn extend() { |
| 223 |
let _guard = LOCK.run_concurrently(); |
| 224 |
let mut doc1 = doc! { |
| 225 |
"first": 1, |
| 226 |
"second": "data", |
| 227 |
"subdoc": doc! { "a": 1, "b": 2 }, |
| 228 |
}; |
| 229 |
|
| 230 |
let doc2 = doc! { |
| 231 |
"third": "abcdefg", |
| 232 |
"first": 2, |
| 233 |
"subdoc": doc! { "c": 3 }, |
| 234 |
}; |
| 235 |
|
| 236 |
doc1.extend(doc2); |
| 237 |
|
| 238 |
assert_eq!( |
| 239 |
doc1, |
| 240 |
doc! { |
| 241 |
"first": 2, |
| 242 |
"second": "data", |
| 243 |
"third": "abcdefg", |
| 244 |
"subdoc": doc! { "c": 3 }, |
| 245 |
}, |
| 246 |
); |
| 247 |
} |