Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/bson_binary_vector/float32.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{
"description": "Infinity Vector FLOAT32",
"valid": true,
"vector": ["-inf", 0.0, "inf"],
"vector": [{"$numberDouble": "-Infinity"}, 0.0, {"$numberDouble": "Infinity"} ],
"dtype_hex": "0x27",
"dtype_alias": "FLOAT32",
"padding": 0,
Expand Down
13 changes: 11 additions & 2 deletions test/test_bson_binary_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
_TEST_PATH = Path(__file__).parent / "bson_binary_vector"


def convert_extended_json(vector) -> float:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we load the entire JSON file using json_util.loads() instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new json_util.load() method for this exact purpose.

if isinstance(vector, dict) and "$numberDouble" in vector:
if vector["$numberDouble"] == "Infinity":
return float("inf")
elif vector["$numberDouble"] == "-Infinity":
return float("-inf")
return float(vector)


class TestBSONBinaryVector(unittest.TestCase):
"""Runs Binary Vector subtype tests.

Expand Down Expand Up @@ -62,9 +71,9 @@ def run_test(self):
cB_exp = binascii.unhexlify(canonical_bson_exp.encode("utf8"))
decoded_doc = decode(cB_exp)
binary_obs = decoded_doc[test_key]
# Handle special float cases like '-inf'
# Handle special extended JSON cases like 'Infinity'
if dtype_exp in [BinaryVectorDtype.FLOAT32]:
vector_exp = [float(x) for x in vector_exp]
vector_exp = [convert_extended_json(x) for x in vector_exp]

# Test round-tripping canonical bson.
self.assertEqual(encode(decoded_doc), cB_exp, description)
Expand Down
Loading