|
21 | 21 | import pytest |
22 | 22 |
|
23 | 23 | from google.cloud.firestore_v1.bson import ( |
| 24 | + BSONBinary, |
24 | 25 | BSONInt32, |
25 | 26 | BSONMaxKey, |
26 | 27 | BSONMinKey, |
@@ -267,3 +268,80 @@ def test_bson_int32_copy(): |
267 | 268 | def test_bson_int32_pickle(): |
268 | 269 | val = BSONInt32(42) |
269 | 270 | assert pickle.loads(pickle.dumps(val)) == val |
| 271 | + |
| 272 | + |
| 273 | +def test_bson_binary_custom_subtype(): |
| 274 | + val = BSONBinary(bytearray(b"world"), subtype=128) |
| 275 | + assert val.data == b"world" |
| 276 | + assert val.subtype == 128 |
| 277 | + |
| 278 | + |
| 279 | +def test_bson_binary_to_map_value(): |
| 280 | + assert BSONBinary(b"world", subtype=128)._to_map_value() == { |
| 281 | + "__binary__": b"\x80world" |
| 282 | + } |
| 283 | + |
| 284 | + |
| 285 | +def test_bson_binary_bytes_coercion(): |
| 286 | + assert bytes(BSONBinary(b"hello", subtype=1)) == b"hello" |
| 287 | + |
| 288 | + |
| 289 | +def test_bson_binary_repr(): |
| 290 | + assert ( |
| 291 | + repr(BSONBinary(b"world", subtype=128)) == "BSONBinary(b'world', subtype=128)" |
| 292 | + ) |
| 293 | + |
| 294 | + |
| 295 | +def test_bson_binary_boundaries(): |
| 296 | + bin_min = BSONBinary(b"test", subtype=1) |
| 297 | + bin_max = BSONBinary(b"test", subtype=255) |
| 298 | + assert bin_min.subtype == 1 |
| 299 | + assert bin_max.subtype == 255 |
| 300 | + |
| 301 | + |
| 302 | +@pytest.mark.parametrize( |
| 303 | + "data_input, subtype_input, exc_type, match_msg", |
| 304 | + [ |
| 305 | + ("not bytes", 1, TypeError, "must be bytes or bytearray"), |
| 306 | + (123, 1, TypeError, "must be bytes or bytearray"), |
| 307 | + (None, 1, TypeError, "must be bytes or bytearray"), |
| 308 | + (b"data", 256, ValueError, "must be between"), |
| 309 | + (b"data", 0, ValueError, "must be between"), |
| 310 | + (b"data", -1, ValueError, "must be between"), |
| 311 | + (b"data", True, TypeError, "subtype must be an int"), |
| 312 | + (b"data", False, TypeError, "subtype must be an int"), |
| 313 | + (b"data", "1", TypeError, "subtype must be an int"), |
| 314 | + ], |
| 315 | +) |
| 316 | +def test_bson_binary_invalid_inputs(data_input, subtype_input, exc_type, match_msg): |
| 317 | + with pytest.raises(exc_type, match=match_msg): |
| 318 | + BSONBinary(data_input, subtype=subtype_input) |
| 319 | + |
| 320 | + |
| 321 | +def test_bson_binary_equality(): |
| 322 | + bin1 = BSONBinary(b"abc", subtype=1) |
| 323 | + bin2 = BSONBinary(b"abc", subtype=1) |
| 324 | + bin3 = BSONBinary(b"abc", subtype=2) |
| 325 | + bin4 = BSONBinary(b"xyz", subtype=1) |
| 326 | + assert bin1 == bin2 |
| 327 | + assert bin1 != bin3 |
| 328 | + assert bin1 != bin4 |
| 329 | + assert bin1 != b"abc" |
| 330 | + |
| 331 | + |
| 332 | +def test_bson_binary_hash_and_dict_key(): |
| 333 | + bin1 = BSONBinary(b"abc", subtype=1) |
| 334 | + bin2 = BSONBinary(b"abc", subtype=1) |
| 335 | + assert hash(bin1) == hash(bin2) |
| 336 | + assert len({bin1, bin2}) == 1 |
| 337 | + |
| 338 | + |
| 339 | +def test_bson_binary_copy(): |
| 340 | + val = BSONBinary(b"hello", subtype=5) |
| 341 | + assert copy.copy(val) == val |
| 342 | + assert copy.deepcopy(val) == val |
| 343 | + |
| 344 | + |
| 345 | +def test_bson_binary_pickle(): |
| 346 | + val = BSONBinary(b"hello", subtype=5) |
| 347 | + assert pickle.loads(pickle.dumps(val)) == val |
0 commit comments