Skip to content

Commit 72e1c2e

Browse files
committed
PYTHON-5121 - Use canonical Extended JSON for BSON binary vector spec tests
1 parent 1145c9d commit 72e1c2e

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

test/bson_binary_vector/float32.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
{
3333
"description": "Infinity Vector FLOAT32",
3434
"valid": true,
35-
"vector": ["-inf", 0.0, "inf"],
35+
"vector": [{"$numberDouble": "-Infinity"}, 0.0, {"$numberDouble": "Infinity"} ],
3636
"dtype_hex": "0x27",
3737
"dtype_alias": "FLOAT32",
3838
"padding": 0,

test/test_bson_binary_vector.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
_TEST_PATH = Path(__file__).parent / "bson_binary_vector"
2828

2929

30+
def convert_extended_json(vector) -> float:
31+
if isinstance(vector, dict) and "$numberDouble" in vector:
32+
if vector["$numberDouble"] == "Infinity":
33+
return float("inf")
34+
elif vector["$numberDouble"] == "-Infinity":
35+
return float("-inf")
36+
return float(vector)
37+
38+
3039
class TestBSONBinaryVector(unittest.TestCase):
3140
"""Runs Binary Vector subtype tests.
3241
@@ -62,9 +71,9 @@ def run_test(self):
6271
cB_exp = binascii.unhexlify(canonical_bson_exp.encode("utf8"))
6372
decoded_doc = decode(cB_exp)
6473
binary_obs = decoded_doc[test_key]
65-
# Handle special float cases like '-inf'
74+
# Handle special extended JSON cases like 'Infinity'
6675
if dtype_exp in [BinaryVectorDtype.FLOAT32]:
67-
vector_exp = [float(x) for x in vector_exp]
76+
vector_exp = [convert_extended_json(x) for x in vector_exp]
6877

6978
# Test round-tripping canonical bson.
7079
self.assertEqual(encode(decoded_doc), cB_exp, description)

0 commit comments

Comments
 (0)