(self)
| 45 | test_key = case_spec.get("test_key") |
| 46 | |
| 47 | def run_test(self): |
| 48 | for test_case in case_spec.get("tests", []): |
| 49 | description = test_case["description"] |
| 50 | vector_exp = test_case.get("vector") |
| 51 | dtype_hex_exp = test_case["dtype_hex"] |
| 52 | dtype_alias_exp = test_case.get("dtype_alias") |
| 53 | padding_exp = test_case.get("padding", 0) |
| 54 | canonical_bson_exp = test_case.get("canonical_bson") |
| 55 | # Convert dtype hex string into bytes |
| 56 | dtype_exp = BinaryVectorDtype(int(dtype_hex_exp, 16).to_bytes(1, byteorder="little")) |
| 57 | |
| 58 | if test_case["valid"]: |
| 59 | # Convert bson string to bytes |
| 60 | cB_exp = binascii.unhexlify(canonical_bson_exp.encode("utf8")) |
| 61 | decoded_doc = decode(cB_exp) |
| 62 | binary_obs = decoded_doc[test_key] |
| 63 | |
| 64 | # Test round-tripping canonical bson. |
| 65 | self.assertEqual(encode(decoded_doc), cB_exp, description) |
| 66 | |
| 67 | # Test BSON to Binary Vector |
| 68 | vector_obs = binary_obs.as_vector() |
| 69 | self.assertEqual(vector_obs.dtype, dtype_exp, description) |
| 70 | if dtype_alias_exp: |
| 71 | self.assertEqual( |
| 72 | vector_obs.dtype, BinaryVectorDtype[dtype_alias_exp], description |
| 73 | ) |
| 74 | if dtype_exp in [BinaryVectorDtype.FLOAT32]: |
| 75 | [ |
| 76 | self.assertAlmostEqual(vector_obs.data[i], vector_exp[i], delta=1e-5) |
| 77 | for i in range(len(vector_exp)) |
| 78 | ] |
| 79 | else: |
| 80 | self.assertEqual(vector_obs.data, vector_exp, description) |
| 81 | # Test Binary Vector to BSON |
| 82 | vector_exp = Binary.from_vector(vector_exp, dtype_exp, padding_exp) |
| 83 | cB_obs = binascii.hexlify(encode({test_key: vector_exp})).decode().upper() |
| 84 | self.assertEqual(cB_obs, canonical_bson_exp, description) |
| 85 | |
| 86 | else: |
| 87 | """ |
| 88 | #### To prove correct in an invalid case (`valid:false`), one MUST |
| 89 | - (encoding case) if the vector field is present, raise an exception |
| 90 | when attempting to encode a document from the numeric values,dtype, and padding. |
| 91 | - (decoding case) if the canonical_bson field is present, raise an exception |
| 92 | when attempting to deserialize it into the corresponding |
| 93 | numeric values, as the field contains corrupted data. |
| 94 | """ |
| 95 | # Tests Binary.from_vector() |
| 96 | if vector_exp is not None: |
| 97 | with self.assertRaises((struct.error, ValueError), msg=description): |
| 98 | Binary.from_vector(vector_exp, dtype_exp, padding_exp) |
| 99 | |
| 100 | # Tests Binary.as_vector() |
| 101 | if canonical_bson_exp is not None: |
| 102 | with self.assertRaises((struct.error, ValueError), msg=description): |
| 103 | cB_exp = binascii.unhexlify(canonical_bson_exp.encode("utf8")) |
| 104 | decoded_doc = decode(cB_exp) |
nothing calls this directly
no test coverage detected