Skip to content

Commit 1bd3220

Browse files
committed
Added bson_corpus tests. Needs more, and review of json_util
1 parent 505c157 commit 1bd3220

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

bson/json_util.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,17 +610,20 @@ def _parse_canonical_binary(doc: Any, json_options: JSONOptions) -> Union[Binary
610610

611611

612612
def _parse_canonical_binary_vector(doc: Any, dummy0: Any) -> BinaryVector:
613-
binary = doc["$binaryVector"]
614-
b64 = binary["base64"]
615-
dtype = getattr(DTYPES, binary["dtype"])
616-
padding = binary["padding"]
613+
if "dtype" in doc:
614+
b64 = doc["$binaryVector"]
615+
else:
616+
doc = doc["$binaryVector"]
617+
b64 = doc["base64"]
618+
dtype = getattr(DTYPES, doc["dtype"])
619+
padding = doc["padding"]
617620
if not isinstance(b64, str):
618621
raise TypeError(f"$binaryVector base64 must be a string: {doc}")
619622
if not isinstance(dtype, DTYPES):
620623
raise TypeError(f"$binaryVector dtype must a member of bson.vector.DTYPES: {doc}")
621624
if not isinstance(padding, str) or len(padding) > 2:
622625
raise TypeError(f"$binaryVector padding must be a string at most 2 characters: {doc}")
623-
if len(binary) != 3:
626+
if len(doc) != 3:
624627
raise TypeError(
625628
f'$binaryVector must include only "base64", "dtype", and "padding" components: {doc}'
626629
)

test/bson_corpus/vector.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"description": "BinaryVector type [!! MORE TESTS REQUIRED. json_utils may need tweaking",
3+
"bson_type": "0x05",
4+
"test_key": "x",
5+
"valid": [
6+
{
7+
"description": "DTYPES.INT8 (Zero-length)",
8+
"canonical_bson": "0f0000000578000000000009030000",
9+
"canonical_extjson": "{\"x\": {\"$binaryVector\": {\"base64\": \"\", \"dtype\": \"INT8\", \"padding\": \"00\"}}}"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)