| 1 |
use std::iter::FromIterator; |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
oid::ObjectId, |
| 5 |
raw::RawJavaScriptCodeWithScope, |
| 6 |
spec::BinarySubtype, |
| 7 |
tests::LOCK, |
| 8 |
Binary, |
| 9 |
Bson, |
| 10 |
DateTime, |
| 11 |
DbPointer, |
| 12 |
Decimal128, |
| 13 |
Document, |
| 14 |
JavaScriptCodeWithScope, |
| 15 |
RawArrayBuf, |
| 16 |
RawBson, |
| 17 |
RawDocumentBuf, |
| 18 |
Regex, |
| 19 |
Timestamp, |
| 20 |
}; |
| 21 |
|
| 22 |
use pretty_assertions::assert_eq; |
| 23 |
|
| 24 |
fn append_test(expected: Document, append: impl FnOnce(&mut RawDocumentBuf)) { |
| 25 |
let bytes = crate::to_vec(&expected).unwrap(); |
| 26 |
let mut buf = RawDocumentBuf::new(); |
| 27 |
append(&mut buf); |
| 28 |
assert_eq!(buf.as_bytes(), bytes); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn i32() { |
| 33 |
let expected = doc! { |
| 34 |
"a": -1_i32, |
| 35 |
"b": 123_i32, |
| 36 |
"c": 0_i32 |
| 37 |
}; |
| 38 |
append_test(expected, |doc| { |
| 39 |
doc.append("a", -1_i32); |
| 40 |
doc.append("b", 123_i32); |
| 41 |
doc.append("c", 0_i32); |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn i64() { |
| 47 |
let expected = doc! { |
| 48 |
"a": -1_i64, |
| 49 |
"b": 123_i64, |
| 50 |
"c": 0_i64 |
| 51 |
}; |
| 52 |
append_test(expected, |doc| { |
| 53 |
doc.append("a", -1_i64); |
| 54 |
doc.append("b", 123_i64); |
| 55 |
doc.append("c", 0_i64); |
| 56 |
}); |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn str() { |
| 61 |
let expected = doc! { |
| 62 |
"first": "the quick", |
| 63 |
"second": "brown fox", |
| 64 |
"third": "jumped over", |
| 65 |
"last": "the lazy sheep dog", |
| 66 |
}; |
| 67 |
append_test(expected, |doc| { |
| 68 |
doc.append("first", "the quick"); |
| 69 |
doc.append("second", "brown fox"); |
| 70 |
doc.append("third", "jumped over"); |
| 71 |
doc.append("last", "the lazy sheep dog"); |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn double() { |
| 77 |
let expected = doc! { |
| 78 |
"positive": 12.5, |
| 79 |
"0": 0.0, |
| 80 |
"negative": -123.24, |
| 81 |
"nan": f64::NAN, |
| 82 |
"inf": f64::INFINITY, |
| 83 |
}; |
| 84 |
append_test(expected, |doc| { |
| 85 |
doc.append("positive", 12.5); |
| 86 |
doc.append("0", 0.0); |
| 87 |
doc.append("negative", -123.24); |
| 88 |
doc.append("nan", f64::NAN); |
| 89 |
doc.append("inf", f64::INFINITY); |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn boolean() { |
| 95 |
let expected = doc! { |
| 96 |
"true": true, |
| 97 |
"false": false, |
| 98 |
}; |
| 99 |
append_test(expected, |doc| { |
| 100 |
doc.append("true", true); |
| 101 |
doc.append("false", false); |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn null() { |
| 107 |
let expected = doc! { |
| 108 |
"null": null, |
| 109 |
}; |
| 110 |
append_test(expected, |doc| { |
| 111 |
doc.append("null", RawBson::Null); |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn document() { |
| 117 |
let expected = doc! { |
| 118 |
"empty": {}, |
| 119 |
"subdoc": { |
| 120 |
"a": 1_i32, |
| 121 |
"b": true, |
| 122 |
} |
| 123 |
}; |
| 124 |
append_test(expected, |doc| { |
| 125 |
doc.append("empty", RawDocumentBuf::new()); |
| 126 |
let mut buf = RawDocumentBuf::new(); |
| 127 |
buf.append("a", 1_i32); |
| 128 |
buf.append("b", true); |
| 129 |
doc.append("subdoc", buf); |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
#[test] |
| 134 |
fn array() { |
| 135 |
let expected = doc! { |
| 136 |
"empty": [], |
| 137 |
"array": [ |
| 138 |
true, |
| 139 |
"string", |
| 140 |
{ "a": "subdoc" }, |
| 141 |
123_i32 |
| 142 |
] |
| 143 |
}; |
| 144 |
append_test(expected, |doc| { |
| 145 |
doc.append("empty", RawArrayBuf::new()); |
| 146 |
let mut buf = RawArrayBuf::new(); |
| 147 |
buf.push(true); |
| 148 |
buf.push("string"); |
| 149 |
let mut subdoc = RawDocumentBuf::new(); |
| 150 |
subdoc.append("a", "subdoc"); |
| 151 |
buf.push(subdoc); |
| 152 |
buf.push(123_i32); |
| 153 |
doc.append("array", buf); |
| 154 |
}); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn oid() { |
| 159 |
let _guard = LOCK.run_concurrently(); |
| 160 |
|
| 161 |
let oid = ObjectId::new(); |
| 162 |
let expected = doc! { |
| 163 |
"oid": oid, |
| 164 |
}; |
| 165 |
append_test(expected, |doc| doc.append("oid", oid)); |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn datetime() { |
| 170 |
let dt = DateTime::now(); |
| 171 |
let old = DateTime::from_millis(-1); |
| 172 |
|
| 173 |
let expected = doc! { |
| 174 |
"now": dt, |
| 175 |
"old": old |
| 176 |
}; |
| 177 |
|
| 178 |
append_test(expected, |doc| { |
| 179 |
doc.append("now", dt); |
| 180 |
doc.append("old", old); |
| 181 |
}); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn timestamp() { |
| 186 |
let ts = Timestamp { |
| 187 |
time: 123, |
| 188 |
increment: 2, |
| 189 |
}; |
| 190 |
|
| 191 |
let expected = doc! { |
| 192 |
"ts": ts, |
| 193 |
}; |
| 194 |
|
| 195 |
append_test(expected, |doc| { |
| 196 |
doc.append("ts", ts); |
| 197 |
}); |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn binary() { |
| 202 |
let bytes = vec![1, 2, 3, 4]; |
| 203 |
|
| 204 |
let bin = Binary { |
| 205 |
bytes: bytes.clone(), |
| 206 |
subtype: BinarySubtype::Generic, |
| 207 |
}; |
| 208 |
|
| 209 |
let old = Binary { |
| 210 |
bytes, |
| 211 |
subtype: BinarySubtype::BinaryOld, |
| 212 |
}; |
| 213 |
|
| 214 |
let expected = doc! { |
| 215 |
"generic": bin.clone(), |
| 216 |
"binary_old": old.clone(), |
| 217 |
}; |
| 218 |
|
| 219 |
append_test(expected, |doc| { |
| 220 |
doc.append("generic", bin); |
| 221 |
doc.append("binary_old", old); |
| 222 |
}); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn min_max_key() { |
| 227 |
let expected = doc! { |
| 228 |
"min": Bson::MinKey, |
| 229 |
"max": Bson::MaxKey |
| 230 |
}; |
| 231 |
|
| 232 |
append_test(expected, |doc| { |
| 233 |
doc.append("min", RawBson::MinKey); |
| 234 |
doc.append("max", RawBson::MaxKey); |
| 235 |
}); |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn undefined() { |
| 240 |
let expected = doc! { |
| 241 |
"undefined": Bson::Undefined, |
| 242 |
}; |
| 243 |
|
| 244 |
append_test(expected, |doc| { |
| 245 |
doc.append("undefined", RawBson::Undefined); |
| 246 |
}); |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn regex() { |
| 251 |
let expected = doc! { |
| 252 |
"regex": Regex::new("some pattern", "abc"), |
| 253 |
}; |
| 254 |
|
| 255 |
append_test(expected, |doc| { |
| 256 |
doc.append("regex", Regex::new("some pattern", "abc")); |
| 257 |
}); |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn code() { |
| 262 |
let code_w_scope = JavaScriptCodeWithScope { |
| 263 |
code: "some code".to_string(), |
| 264 |
scope: doc! { "a": 1_i32, "b": true }, |
| 265 |
}; |
| 266 |
|
| 267 |
let expected = doc! { |
| 268 |
"code": Bson::JavaScriptCode("some code".to_string()), |
| 269 |
"code_w_scope": code_w_scope, |
| 270 |
}; |
| 271 |
|
| 272 |
append_test(expected, |doc| { |
| 273 |
doc.append("code", RawBson::JavaScriptCode("some code".to_string())); |
| 274 |
|
| 275 |
let mut scope = RawDocumentBuf::new(); |
| 276 |
scope.append("a", 1_i32); |
| 277 |
scope.append("b", true); |
| 278 |
doc.append( |
| 279 |
"code_w_scope", |
| 280 |
RawJavaScriptCodeWithScope { |
| 281 |
code: "some code".to_string(), |
| 282 |
scope, |
| 283 |
}, |
| 284 |
); |
| 285 |
}); |
| 286 |
} |
| 287 |
|
| 288 |
#[test] |
| 289 |
fn symbol() { |
| 290 |
let expected = doc! { |
| 291 |
"symbol": Bson::Symbol("symbol".to_string()) |
| 292 |
}; |
| 293 |
|
| 294 |
append_test(expected, |doc| { |
| 295 |
doc.append("symbol", RawBson::Symbol("symbol".to_string())); |
| 296 |
}); |
| 297 |
} |
| 298 |
|
| 299 |
#[test] |
| 300 |
fn dbpointer() { |
| 301 |
let _guard = LOCK.run_concurrently(); |
| 302 |
|
| 303 |
let id = ObjectId::new(); |
| 304 |
|
| 305 |
let expected = doc! { |
| 306 |
"symbol": Bson::DbPointer(DbPointer { |
| 307 |
namespace: "ns".to_string(), |
| 308 |
id |
| 309 |
}) |
| 310 |
}; |
| 311 |
|
| 312 |
append_test(expected, |doc| { |
| 313 |
doc.append( |
| 314 |
"symbol", |
| 315 |
RawBson::DbPointer(DbPointer { |
| 316 |
namespace: "ns".to_string(), |
| 317 |
id, |
| 318 |
}), |
| 319 |
); |
| 320 |
}); |
| 321 |
} |
| 322 |
|
| 323 |
#[test] |
| 324 |
fn decimal128() { |
| 325 |
let decimal = Decimal128 { bytes: [1; 16] }; |
| 326 |
let expected = doc! { |
| 327 |
"decimal": decimal |
| 328 |
}; |
| 329 |
|
| 330 |
append_test(expected, |doc| { |
| 331 |
doc.append("decimal", decimal); |
| 332 |
}); |
| 333 |
} |
| 334 |
|
| 335 |
#[test] |
| 336 |
fn general() { |
| 337 |
let dt = DateTime::now(); |
| 338 |
let expected = doc! { |
| 339 |
"a": true, |
| 340 |
"second key": 123.4, |
| 341 |
"third": 15_i64, |
| 342 |
"32": -100101_i32, |
| 343 |
"subdoc": { |
| 344 |
"a": "subkey", |
| 345 |
"another": { "subdoc": dt } |
| 346 |
}, |
| 347 |
"array": [1_i64, true, { "doc": 23_i64 }, ["another", "array"]], |
| 348 |
}; |
| 349 |
|
| 350 |
append_test(expected, |doc| { |
| 351 |
doc.append("a", true); |
| 352 |
doc.append("second key", 123.4); |
| 353 |
doc.append("third", 15_i64); |
| 354 |
doc.append("32", -100101_i32); |
| 355 |
|
| 356 |
let mut subdoc = RawDocumentBuf::new(); |
| 357 |
subdoc.append("a", "subkey"); |
| 358 |
|
| 359 |
let mut subsubdoc = RawDocumentBuf::new(); |
| 360 |
subsubdoc.append("subdoc", dt); |
| 361 |
subdoc.append("another", subsubdoc); |
| 362 |
doc.append("subdoc", subdoc); |
| 363 |
|
| 364 |
let mut array = RawArrayBuf::new(); |
| 365 |
array.push(1_i64); |
| 366 |
array.push(true); |
| 367 |
|
| 368 |
let mut array_subdoc = RawDocumentBuf::new(); |
| 369 |
array_subdoc.append("doc", 23_i64); |
| 370 |
array.push(array_subdoc); |
| 371 |
|
| 372 |
let mut sub_array = RawArrayBuf::new(); |
| 373 |
sub_array.push("another"); |
| 374 |
sub_array.push("array"); |
| 375 |
array.push(sub_array); |
| 376 |
|
| 377 |
doc.append("array", array); |
| 378 |
}); |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn from_iter() { |
| 383 |
let doc_buf = RawDocumentBuf::from_iter([ |
| 384 |
( |
| 385 |
"array", |
| 386 |
RawBson::Array(RawArrayBuf::from_iter([ |
| 387 |
RawBson::Boolean(true), |
| 388 |
RawBson::Document(RawDocumentBuf::from_iter([ |
| 389 |
("ok", RawBson::Boolean(false)), |
| 390 |
("other", RawBson::String("hello".to_string())), |
| 391 |
])), |
| 392 |
])), |
| 393 |
), |
| 394 |
("bool", RawBson::Boolean(true)), |
| 395 |
("string", RawBson::String("some string".to_string())), |
| 396 |
]); |
| 397 |
|
| 398 |
let doc = doc! { |
| 399 |
"array": [ |
| 400 |
true, |
| 401 |
{ |
| 402 |
"ok": false, |
| 403 |
"other": "hello" |
| 404 |
} |
| 405 |
], |
| 406 |
"bool": true, |
| 407 |
"string": "some string" |
| 408 |
}; |
| 409 |
|
| 410 |
let expected = doc! { "expected": doc }; |
| 411 |
append_test(expected, |doc| { |
| 412 |
doc.append("expected", doc_buf); |
| 413 |
}); |
| 414 |
} |
| 415 |
|
| 416 |
#[test] |
| 417 |
fn array_buf() { |
| 418 |
let mut arr_buf = RawArrayBuf::new(); |
| 419 |
arr_buf.push(true); |
| 420 |
|
| 421 |
let mut doc_buf = RawDocumentBuf::new(); |
| 422 |
doc_buf.append("x", 3_i32); |
| 423 |
doc_buf.append("string", "string"); |
| 424 |
arr_buf.push(doc_buf); |
| 425 |
|
| 426 |
let mut sub_arr = RawArrayBuf::new(); |
| 427 |
sub_arr.push("a string"); |
| 428 |
arr_buf.push(sub_arr); |
| 429 |
|
| 430 |
let arr = rawbson!([ |
| 431 |
true, |
| 432 |
{ "x": 3_i32, "string": "string" }, |
| 433 |
[ "a string" ] |
| 434 |
]); |
| 435 |
|
| 436 |
assert_eq!(arr_buf.as_ref(), arr.as_array().unwrap()); |
| 437 |
} |