|
| 1 | +use pretty_assertions::assert_eq; |
| 2 | +use serde_json::json; |
| 3 | + |
| 4 | +use super::AllTypes; |
| 5 | + |
| 6 | +use bson::{doc, Bson, JavaScriptCodeWithScope, RawArrayBuf, RawBson, RawDocumentBuf}; |
| 7 | + |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn all_types_json() { |
| 12 | + let (mut v, _) = AllTypes::fixtures(); |
| 13 | + |
| 14 | + let code = match v.code { |
| 15 | + Bson::JavaScriptCode(ref c) => c.clone(), |
| 16 | + c => panic!("expected code, found {:?}", c), |
| 17 | + }; |
| 18 | + |
| 19 | + let code_w_scope = JavaScriptCodeWithScope { |
| 20 | + code: "hello world".to_string(), |
| 21 | + scope: doc! { "x": 1 }, |
| 22 | + }; |
| 23 | + let scope_json = serde_json::json!({ "x": 1 }); |
| 24 | + v.code_w_scope = code_w_scope.clone(); |
| 25 | + |
| 26 | + let json = serde_json::json!({ |
| 27 | + "x": 1, |
| 28 | + "y": 2, |
| 29 | + "s": "oke", |
| 30 | + "array": vec![ |
| 31 | + serde_json::json!(true), |
| 32 | + serde_json::json!("oke".to_string()), |
| 33 | + serde_json::json!({ "12": 24 }), |
| 34 | + ], |
| 35 | + "bson": 1234.5, |
| 36 | + "oid": { "$oid": v.oid.to_hex() }, |
| 37 | + "null": serde_json::Value::Null, |
| 38 | + "subdoc": { "k": true, "b": { "hello": "world" } }, |
| 39 | + "b": true, |
| 40 | + "d": 12.5, |
| 41 | + "binary": v.binary.bytes, |
| 42 | + "binary_old": { "$binary": { "base64": base64::encode(&v.binary_old.bytes), "subType": "02" } }, |
| 43 | + "binary_other": { "$binary": { "base64": base64::encode(&v.binary_old.bytes), "subType": "81" } }, |
| 44 | + "date": { "$date": { "$numberLong": v.date.timestamp_millis().to_string() } }, |
| 45 | + "regex": { "$regularExpression": { "pattern": v.regex.pattern, "options": v.regex.options } }, |
| 46 | + "ts": { "$timestamp": { "t": 123, "i": 456 } }, |
| 47 | + "i": { "a": v.i.a, "b": v.i.b }, |
| 48 | + "undefined": { "$undefined": true }, |
| 49 | + "code": { "$code": code }, |
| 50 | + "code_w_scope": { "$code": code_w_scope.code, "$scope": scope_json }, |
| 51 | + "decimal": { "$numberDecimalBytes": v.decimal.bytes() }, |
| 52 | + "symbol": { "$symbol": "ok" }, |
| 53 | + "min_key": { "$minKey": 1 }, |
| 54 | + "max_key": { "$maxKey": 1 }, |
| 55 | + }); |
| 56 | + |
| 57 | + assert_eq!(serde_json::to_value(&v).unwrap(), json); |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn owned_raw_bson() { |
| 62 | + #[derive(Serialize, Deserialize, Debug, PartialEq)] |
| 63 | + struct Foo { |
| 64 | + doc_buf: RawDocumentBuf, |
| 65 | + array_buf: RawArrayBuf, |
| 66 | + bson_array: RawBson, |
| 67 | + bson_doc: RawBson, |
| 68 | + bson_integer: RawBson, |
| 69 | + bson_string: RawBson, |
| 70 | + bson_bool: RawBson, |
| 71 | + bson_null: RawBson, |
| 72 | + bson_float: RawBson, |
| 73 | + } |
| 74 | + |
| 75 | + let json = json!({ |
| 76 | + "doc_buf": { |
| 77 | + "a": "key", |
| 78 | + "number": 12, |
| 79 | + "bool": false, |
| 80 | + "nu": null |
| 81 | + }, |
| 82 | + "array_buf": [ |
| 83 | + json!(1), |
| 84 | + json!("string"), |
| 85 | + ], |
| 86 | + "bson_array": [ |
| 87 | + json!(1), |
| 88 | + json!("string"), |
| 89 | + ], |
| 90 | + "bson_doc": { |
| 91 | + "first": true, |
| 92 | + "second": "string", |
| 93 | + }, |
| 94 | + "bson_integer": 12, |
| 95 | + "bson_string": "String", |
| 96 | + "bson_bool": true, |
| 97 | + "bson_null": null, |
| 98 | + "bson_float": 123.4 |
| 99 | + }); |
| 100 | + |
| 101 | + let mut doc_buf = RawDocumentBuf::new(); |
| 102 | + doc_buf.append("a", "key"); |
| 103 | + doc_buf.append("number", 12); |
| 104 | + doc_buf.append("bool", false); |
| 105 | + doc_buf.append("nu", RawBson::Null); |
| 106 | + |
| 107 | + let mut array_buf = RawArrayBuf::new(); |
| 108 | + array_buf.push(1); |
| 109 | + array_buf.push("string"); |
| 110 | + |
| 111 | + let mut bson_doc = RawDocumentBuf::new(); |
| 112 | + bson_doc.append("first", true); |
| 113 | + bson_doc.append("second", "string"); |
| 114 | + |
| 115 | + let expected = Foo { |
| 116 | + doc_buf, |
| 117 | + array_buf: array_buf.clone(), |
| 118 | + bson_array: RawBson::Array(array_buf), |
| 119 | + bson_doc: RawBson::Document(bson_doc), |
| 120 | + bson_integer: RawBson::Int32(12), |
| 121 | + bson_string: RawBson::String("String".to_string()), |
| 122 | + bson_bool: RawBson::Boolean(true), |
| 123 | + bson_null: RawBson::Null, |
| 124 | + bson_float: RawBson::Double(123.4), |
| 125 | + }; |
| 126 | + |
| 127 | + let f: Foo = serde_json::from_value(json.clone()).unwrap(); |
| 128 | + assert_eq!(f, expected); |
| 129 | + |
| 130 | + let round_trip = serde_json::to_value(&f).unwrap(); |
| 131 | + assert_eq!(round_trip, json); |
| 132 | +} |
0 commit comments