(self)
| 492 | self.assertEqual(doc, json_util.loads(ext_json_str, json_options=options)) |
| 493 | |
| 494 | def test_binary(self): |
| 495 | bin_type_dict = {"bin": b"\x00\x01\x02\x03\x04"} |
| 496 | md5_type_dict = { |
| 497 | "md5": Binary(b" n7\x18\xaf\t/\xd1\xd1/\x80\xca\xe7q\xcc\xac", MD5_SUBTYPE) |
| 498 | } |
| 499 | custom_type_dict = {"custom": Binary(b"hello", USER_DEFINED_SUBTYPE)} |
| 500 | |
| 501 | self.round_trip(bin_type_dict) |
| 502 | self.round_trip(md5_type_dict) |
| 503 | self.round_trip(custom_type_dict) |
| 504 | |
| 505 | # Binary with subtype 0 is decoded into bytes in Python 3. |
| 506 | bin = json_util.loads('{"bin": {"$binary": "AAECAwQ=", "$type": "00"}}')["bin"] |
| 507 | self.assertEqual(type(bin), bytes) |
| 508 | |
| 509 | # PYTHON-443 ensure old type formats are supported |
| 510 | json_bin_dump = json_util.dumps(bin_type_dict, json_options=LEGACY_JSON_OPTIONS) |
| 511 | self.assertIn('"$type": "00"', json_bin_dump) |
| 512 | self.assertEqual( |
| 513 | bin_type_dict, json_util.loads('{"bin": {"$type": 0, "$binary": "AAECAwQ="}}') |
| 514 | ) |
| 515 | json_bin_dump = json_util.dumps(md5_type_dict, json_options=LEGACY_JSON_OPTIONS) |
| 516 | # Check order. |
| 517 | self.assertEqual( |
| 518 | '{"md5": {"$binary": "IG43GK8JL9HRL4DK53HMrA==", "$type": "05"}}', json_bin_dump |
| 519 | ) |
| 520 | |
| 521 | self.assertEqual( |
| 522 | md5_type_dict, |
| 523 | json_util.loads('{"md5": {"$type": 5, "$binary": "IG43GK8JL9HRL4DK53HMrA=="}}'), |
| 524 | ) |
| 525 | |
| 526 | json_bin_dump = json_util.dumps(custom_type_dict, json_options=LEGACY_JSON_OPTIONS) |
| 527 | self.assertIn('"$type": "80"', json_bin_dump) |
| 528 | self.assertEqual( |
| 529 | custom_type_dict, |
| 530 | json_util.loads('{"custom": {"$type": 128, "$binary": "aGVsbG8="}}'), |
| 531 | ) |
| 532 | |
| 533 | # Handle mongoexport where subtype >= 128 |
| 534 | self.assertEqual( |
| 535 | 128, |
| 536 | json_util.loads('{"custom": {"$type": "ffffff80", "$binary": "aGVsbG8="}}')[ |
| 537 | "custom" |
| 538 | ].subtype, |
| 539 | ) |
| 540 | |
| 541 | self.assertEqual( |
| 542 | 255, |
| 543 | json_util.loads('{"custom": {"$type": "ffffffff", "$binary": "aGVsbG8="}}')[ |
| 544 | "custom" |
| 545 | ].subtype, |
| 546 | ) |
| 547 | |
| 548 | def test_code(self): |
| 549 | self.round_trip({"code": Code("function x() { return 1; }")}) |
nothing calls this directly
no test coverage detected