1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
|
# Copyright 2019-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test support for callbacks to encode/decode custom types."""
from __future__ import annotations
import datetime
import sys
import tempfile
from collections import OrderedDict
from decimal import Decimal
from random import random
from typing import Any, Tuple, Type, no_type_check
from bson.decimal128 import DecimalDecoder, DecimalEncoder
from gridfs.asynchronous.grid_file import AsyncGridIn, AsyncGridOut
sys.path[0:0] = [""]
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest
from bson import (
_BUILT_IN_TYPES,
RE_TYPE,
Decimal128,
_bson_to_dict,
_dict_to_bson,
decode,
decode_all,
decode_file_iter,
decode_iter,
encode,
)
from bson.codec_options import (
CodecOptions,
TypeCodec,
TypeDecoder,
TypeEncoder,
TypeRegistry,
)
from bson.errors import InvalidDocument
from bson.int64 import Int64
from bson.raw_bson import RawBSONDocument
from pymongo.asynchronous.collection import ReturnDocument
from pymongo.asynchronous.helpers import anext
from pymongo.errors import DuplicateKeyError
from pymongo.message import _CursorAddress
_IS_SYNC = False
DECIMAL_CODECOPTS = CodecOptions(type_registry=TypeRegistry([DecimalEncoder(), DecimalDecoder()]))
class UndecipherableInt64Type:
def __init__(self, value):
self.value = value
def __eq__(self, other):
if isinstance(other, type(self)):
return self.value == other.value
# Does not compare equal to integers.
return False
class UndecipherableIntDecoder(TypeDecoder):
bson_type = Int64
def transform_bson(self, value):
return UndecipherableInt64Type(value)
class UndecipherableIntEncoder(TypeEncoder):
python_type = UndecipherableInt64Type
def transform_python(self, value):
return Int64(value.value)
UNINT_DECODER_CODECOPTS = CodecOptions(
type_registry=TypeRegistry(
[
UndecipherableIntDecoder(),
]
)
)
UNINT_CODECOPTS = CodecOptions(
type_registry=TypeRegistry([UndecipherableIntDecoder(), UndecipherableIntEncoder()])
)
class UppercaseTextDecoder(TypeDecoder):
bson_type = str
def transform_bson(self, value):
return value.upper()
UPPERSTR_DECODER_CODECOPTS = CodecOptions(
type_registry=TypeRegistry(
[
UppercaseTextDecoder(),
]
)
)
def type_obfuscating_decoder_factory(rt_type):
class ResumeTokenToNanDecoder(TypeDecoder):
bson_type = rt_type
def transform_bson(self, value):
return "NaN"
return ResumeTokenToNanDecoder
class CustomBSONTypeTests:
@no_type_check
def roundtrip(self, doc):
bsonbytes = encode(doc, codec_options=self.codecopts)
rt_document = decode(bsonbytes, codec_options=self.codecopts)
self.assertEqual(doc, rt_document)
def test_encode_decode_roundtrip(self):
self.roundtrip({"average": Decimal("56.47")})
self.roundtrip({"average": {"b": Decimal("56.47")}})
self.roundtrip({"average": [Decimal("56.47")]})
self.roundtrip({"average": [[Decimal("56.47")]]})
self.roundtrip({"average": [{"b": Decimal("56.47")}]})
@no_type_check
def test_decode_all(self):
documents = []
for dec in range(3):
documents.append({"average": Decimal(f"56.4{dec}")})
bsonstream = b""
for doc in documents:
bsonstream += encode(doc, codec_options=self.codecopts)
self.assertEqual(decode_all(bsonstream, self.codecopts), documents)
@no_type_check
def test__bson_to_dict(self):
document = {"average": Decimal("56.47")}
rawbytes = encode(document, codec_options=self.codecopts)
decoded_document = _bson_to_dict(rawbytes, self.codecopts)
self.assertEqual(document, decoded_document)
@no_type_check
def test__dict_to_bson(self):
document = {"average": Decimal("56.47")}
rawbytes = encode(document, codec_options=self.codecopts)
encoded_document = _dict_to_bson(document, False, self.codecopts)
self.assertEqual(encoded_document, rawbytes)
def _generate_multidocument_bson_stream(self):
inp_num = [str(random() * 100)[:4] for _ in range(10)]
docs = [{"n": Decimal128(dec)} for dec in inp_num]
edocs = [{"n": Decimal(dec)} for dec in inp_num]
bsonstream = b""
for doc in docs:
bsonstream += encode(doc)
return edocs, bsonstream
@no_type_check
def test_decode_iter(self):
expected, bson_data = self._generate_multidocument_bson_stream()
for expected_doc, decoded_doc in zip(expected, decode_iter(bson_data, self.codecopts)):
self.assertEqual(expected_doc, decoded_doc)
@no_type_check
def test_decode_file_iter(self):
expected, bson_data = self._generate_multidocument_bson_stream()
fileobj = tempfile.TemporaryFile()
fileobj.write(bson_data)
fileobj.seek(0)
for expected_doc, decoded_doc in zip(expected, decode_file_iter(fileobj, self.codecopts)):
self.assertEqual(expected_doc, decoded_doc)
fileobj.close()
class TestCustomPythonBSONTypeToBSONMonolithicCodec(CustomBSONTypeTests, unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.codecopts = DECIMAL_CODECOPTS
class TestCustomPythonBSONTypeToBSONMultiplexedCodec(CustomBSONTypeTests, unittest.TestCase):
@classmethod
def setUpClass(cls):
codec_options = CodecOptions(
type_registry=TypeRegistry((DecimalEncoder(), DecimalDecoder()))
)
cls.codecopts = codec_options
class TestBSONFallbackEncoder(unittest.TestCase):
def _get_codec_options(self, fallback_encoder):
type_registry = TypeRegistry(fallback_encoder=fallback_encoder)
return CodecOptions(type_registry=type_registry)
def test_simple(self):
codecopts = self._get_codec_options(lambda x: Decimal128(x))
document = {"average": Decimal("56.47")}
bsonbytes = encode(document, codec_options=codecopts)
exp_document = {"average": Decimal128("56.47")}
exp_bsonbytes = encode(exp_document)
self.assertEqual(bsonbytes, exp_bsonbytes)
def test_erroring_fallback_encoder(self):
codecopts = self._get_codec_options(lambda _: 1 / 0)
# fallback converter should not be invoked when encoding known types.
encode(
{"a": 1, "b": Decimal128("1.01"), "c": {"arr": ["abc", 3.678]}}, codec_options=codecopts
)
# expect an error when encoding a custom type.
document = {"average": Decimal("56.47")}
with self.assertRaises(ZeroDivisionError):
encode(document, codec_options=codecopts)
def test_noop_fallback_encoder(self):
codecopts = self._get_codec_options(lambda x: x)
document = {"average": Decimal("56.47")}
with self.assertRaises(InvalidDocument):
encode(document, codec_options=codecopts)
def test_type_unencodable_by_fallback_encoder(self):
def fallback_encoder(value):
try:
return Decimal128(value)
except:
raise TypeError("cannot encode type %s" % (type(value)))
codecopts = self._get_codec_options(fallback_encoder)
document = {"average": Decimal}
with self.assertRaises(TypeError):
encode(document, codec_options=codecopts)
def test_call_only_once_for_not_handled_big_integers(self):
called_with = []
def fallback_encoder(value):
called_with.append(value)
return value
codecopts = self._get_codec_options(fallback_encoder)
document = {"a": {"b": {"c": 2 << 65}}}
msg = "MongoDB can only handle up to 8-byte ints"
with self.assertRaises(OverflowError, msg=msg):
encode(document, codec_options=codecopts)
self.assertEqual(called_with, [2 << 65])
class TestBSONTypeEnDeCodecs(unittest.TestCase):
def test_instantiation(self):
msg = "Can't instantiate abstract class"
def run_test(base, attrs, fail):
codec = type("testcodec", (base,), attrs)
if fail:
with self.assertRaisesRegex(TypeError, msg):
codec()
else:
codec()
class MyType:
pass
run_test(
TypeEncoder,
{
"python_type": MyType,
},
fail=True,
)
run_test(TypeEncoder, {"transform_python": lambda s, x: x}, fail=True)
run_test(
TypeEncoder, {"transform_python": lambda s, x: x, "python_type": MyType}, fail=False
)
run_test(
TypeDecoder,
{
"bson_type": Decimal128,
},
fail=True,
)
run_test(TypeDecoder, {"transform_bson": lambda s, x: x}, fail=True)
run_test(
TypeDecoder, {"transform_bson": lambda s, x: x, "bson_type": Decimal128}, fail=False
)
run_test(TypeCodec, {"bson_type": Decimal128, "python_type": MyType}, fail=True)
run_test(
TypeCodec,
{"transform_bson": lambda s, x: x, "transform_python": lambda s, x: x},
fail=True,
)
run_test(
TypeCodec,
{
"python_type": MyType,
"transform_python": lambda s, x: x,
"transform_bson": lambda s, x: x,
"bson_type": Decimal128,
},
fail=False,
)
def test_type_checks(self):
self.assertTrue(issubclass(TypeCodec, TypeEncoder))
self.assertTrue(issubclass(TypeCodec, TypeDecoder))
self.assertFalse(issubclass(TypeDecoder, TypeEncoder))
self.assertFalse(issubclass(TypeEncoder, TypeDecoder))
class TestBSONCustomTypeEncoderAndFallbackEncoderTandem(unittest.TestCase):
TypeA: Any
TypeB: Any
fallback_encoder_A2B: Any
fallback_encoder_A2BSON: Any
B2BSON: Type[TypeEncoder]
B2A: Type[TypeEncoder]
A2B: Type[TypeEncoder]
@classmethod
def setUpClass(cls):
class TypeA:
def __init__(self, x):
self.value = x
class TypeB:
def __init__(self, x):
self.value = x
# transforms A, and only A into B
def fallback_encoder_A2B(value):
assert isinstance(value, TypeA)
return TypeB(value.value)
# transforms A, and only A into something encodable
def fallback_encoder_A2BSON(value):
assert isinstance(value, TypeA)
return value.value
# transforms B into something encodable
class B2BSON(TypeEncoder):
python_type = TypeB
def transform_python(self, value):
return value.value
# transforms A into B
# technically, this isn't a proper type encoder as the output is not
# BSON-encodable.
class A2B(TypeEncoder):
python_type = TypeA
def transform_python(self, value):
return TypeB(value.value)
# transforms B into A
# technically, this isn't a proper type encoder as the output is not
# BSON-encodable.
class B2A(TypeEncoder):
python_type = TypeB
def transform_python(self, value):
return TypeA(value.value)
cls.TypeA = TypeA
cls.TypeB = TypeB
cls.fallback_encoder_A2B = staticmethod(fallback_encoder_A2B)
cls.fallback_encoder_A2BSON = staticmethod(fallback_encoder_A2BSON)
cls.B2BSON = B2BSON
cls.B2A = B2A
cls.A2B = A2B
def test_encode_fallback_then_custom(self):
codecopts = CodecOptions(
type_registry=TypeRegistry([self.B2BSON()], fallback_encoder=self.fallback_encoder_A2B)
)
testdoc = {"x": self.TypeA(123)}
expected_bytes = encode({"x": 123})
self.assertEqual(encode(testdoc, codec_options=codecopts), expected_bytes)
def test_encode_custom_then_fallback(self):
codecopts = CodecOptions(
type_registry=TypeRegistry([self.B2A()], fallback_encoder=self.fallback_encoder_A2BSON)
)
testdoc = {"x": self.TypeB(123)}
expected_bytes = encode({"x": 123})
self.assertEqual(encode(testdoc, codec_options=codecopts), expected_bytes)
def test_chaining_encoders_fails(self):
codecopts = CodecOptions(type_registry=TypeRegistry([self.A2B(), self.B2BSON()]))
with self.assertRaises(InvalidDocument):
encode({"x": self.TypeA(123)}, codec_options=codecopts)
def test_infinite_loop_exceeds_max_recursion_depth(self):
codecopts = CodecOptions(
type_registry=TypeRegistry([self.B2A()], fallback_encoder=self.fallback_encoder_A2B)
)
# Raises max recursion depth exceeded error
with self.assertRaises(RuntimeError):
encode({"x": self.TypeA(100)}, codec_options=codecopts)
class TestTypeRegistry(unittest.TestCase):
types: Tuple[object, object]
codecs: Tuple[Type[TypeCodec], Type[TypeCodec]]
fallback_encoder: Any
@classmethod
def setUpClass(cls):
class MyIntType:
def __init__(self, x):
assert isinstance(x, int)
self.x = x
class MyStrType:
def __init__(self, x):
assert isinstance(x, str)
self.x = x
class MyIntCodec(TypeCodec):
@property
def python_type(self):
return MyIntType
@property
def bson_type(self):
return int
def transform_python(self, value):
return value.x
def transform_bson(self, value):
return MyIntType(value)
class MyStrCodec(TypeCodec):
@property
def python_type(self):
return MyStrType
@property
def bson_type(self):
return str
def transform_python(self, value):
return value.x
def transform_bson(self, value):
return MyStrType(value)
def fallback_encoder(value):
return value
cls.types = (MyIntType, MyStrType)
cls.codecs = (MyIntCodec, MyStrCodec)
cls.fallback_encoder = fallback_encoder
def test_simple(self):
codec_instances = [codec() for codec in self.codecs]
def assert_proper_initialization(type_registry, codec_instances):
self.assertEqual(
type_registry._encoder_map,
{
self.types[0]: codec_instances[0].transform_python,
self.types[1]: codec_instances[1].transform_python,
},
)
self.assertEqual(
type_registry._decoder_map,
{int: codec_instances[0].transform_bson, str: codec_instances[1].transform_bson},
)
self.assertEqual(type_registry._fallback_encoder, self.fallback_encoder)
type_registry = TypeRegistry(codec_instances, self.fallback_encoder)
assert_proper_initialization(type_registry, codec_instances)
type_registry = TypeRegistry(
fallback_encoder=self.fallback_encoder, type_codecs=codec_instances
)
assert_proper_initialization(type_registry, codec_instances)
# Ensure codec list held by the type registry doesn't change if we
# mutate the initial list.
codec_instances_copy = list(codec_instances)
codec_instances.pop(0)
self.assertListEqual(type_registry._TypeRegistry__type_codecs, codec_instances_copy)
def test_simple_separate_codecs(self):
class MyIntEncoder(TypeEncoder):
python_type = self.types[0]
def transform_python(self, value):
return value.x
class MyIntDecoder(TypeDecoder):
bson_type = int
def transform_bson(self, value):
return self.types[0](value)
codec_instances: list = [MyIntDecoder(), MyIntEncoder()]
type_registry = TypeRegistry(codec_instances)
self.assertEqual(
type_registry._encoder_map,
{MyIntEncoder.python_type: codec_instances[1].transform_python},
)
self.assertEqual(
type_registry._decoder_map,
{MyIntDecoder.bson_type: codec_instances[0].transform_bson},
)
def test_initialize_fail(self):
err_msg = "Expected an instance of TypeEncoder, TypeDecoder, or TypeCodec, got .* instead"
with self.assertRaisesRegex(TypeError, err_msg):
TypeRegistry(self.codecs) # type: ignore[arg-type]
with self.assertRaisesRegex(TypeError, err_msg):
TypeRegistry([type("AnyType", (object,), {})()])
err_msg = f"fallback_encoder {True!r} is not a callable"
with self.assertRaisesRegex(TypeError, err_msg):
TypeRegistry([], True) # type: ignore[arg-type]
err_msg = "fallback_encoder {!r} is not a callable".format("hello")
with self.assertRaisesRegex(TypeError, err_msg):
TypeRegistry(fallback_encoder="hello") # type: ignore[arg-type]
def test_type_registry_codecs(self):
codec_instances = [codec() for codec in self.codecs]
type_registry = TypeRegistry(codec_instances)
self.assertEqual(type_registry.codecs, codec_instances)
def test_type_registry_fallback(self):
type_registry = TypeRegistry(fallback_encoder=self.fallback_encoder)
self.assertEqual(type_registry.fallback_encoder, self.fallback_encoder)
def test_type_registry_repr(self):
codec_instances = [codec() for codec in self.codecs]
type_registry = TypeRegistry(codec_instances)
r = f"TypeRegistry(type_codecs={codec_instances!r}, fallback_encoder={None!r})"
self.assertEqual(r, repr(type_registry))
def test_type_registry_eq(self):
codec_instances = [codec() for codec in self.codecs]
self.assertEqual(TypeRegistry(codec_instances), TypeRegistry(codec_instances))
codec_instances_2 = [codec() for codec in self.codecs]
self.assertNotEqual(TypeRegistry(codec_instances), TypeRegistry(codec_instances_2))
def test_builtin_types_override_fails(self):
def run_test(base, attrs):
msg = (
r"TypeEncoders cannot change how built-in types "
r"are encoded \(encoder .* transforms type .*\)"
)
for pytype in _BUILT_IN_TYPES:
attrs.update({"python_type": pytype, "transform_python": lambda x: x})
codec = type("testcodec", (base,), attrs)
codec_instance = codec()
with self.assertRaisesRegex(TypeError, msg):
TypeRegistry(
[
codec_instance,
]
)
# Test only some subtypes as not all can be subclassed.
if pytype in [
bool,
type(None),
RE_TYPE,
]:
continue
class MyType(pytype): # type: ignore
pass
attrs.update({"python_type": MyType, "transform_python": lambda x: x})
codec = type("testcodec", (base,), attrs)
codec_instance = codec()
with self.assertRaisesRegex(TypeError, msg):
TypeRegistry(
[
codec_instance,
]
)
run_test(TypeEncoder, {})
run_test(TypeCodec, {"bson_type": Decimal128, "transform_bson": lambda x: x})
class TestCollectionWCustomType(AsyncIntegrationTest):
async def asyncSetUp(self):
await super().asyncSetUp()
await self.db.test.drop()
async def asyncTearDown(self):
await self.db.test.drop()
async def test_overflow_int_w_custom_decoder(self):
type_registry = TypeRegistry(fallback_encoder=lambda val: str(val))
codec_options = CodecOptions(type_registry=type_registry)
collection = self.db.get_collection("test", codec_options=codec_options)
await collection.insert_one({"_id": 1, "data": 2**520})
ret = await collection.find_one()
self.assertEqual(ret["data"], str(2**520))
async def test_command_errors_w_custom_type_decoder(self):
db = self.db
test_doc = {"_id": 1, "data": "a"}
test = db.get_collection("test", codec_options=UNINT_DECODER_CODECOPTS)
result = await test.insert_one(test_doc)
self.assertEqual(result.inserted_id, test_doc["_id"])
with self.assertRaises(DuplicateKeyError):
await test.insert_one(test_doc)
async def test_find_w_custom_type_decoder(self):
db = self.db
input_docs = [{"x": Int64(k)} for k in [1, 2, 3]]
for doc in input_docs:
await db.test.insert_one(doc)
test = db.get_collection("test", codec_options=UNINT_DECODER_CODECOPTS)
async for doc in test.find({}, batch_size=1):
self.assertIsInstance(doc["x"], UndecipherableInt64Type)
async def test_find_w_custom_type_decoder_and_document_class(self):
async def run_test(doc_cls):
db = self.db
input_docs = [{"x": Int64(k)} for k in [1, 2, 3]]
for doc in input_docs:
await db.test.insert_one(doc)
test = db.get_collection(
"test",
codec_options=CodecOptions(
type_registry=TypeRegistry([UndecipherableIntDecoder()]), document_class=doc_cls
),
)
async for doc in test.find({}, batch_size=1):
self.assertIsInstance(doc, doc_cls)
self.assertIsInstance(doc["x"], UndecipherableInt64Type)
for doc_cls in [RawBSONDocument, OrderedDict]:
await run_test(doc_cls)
async def test_aggregate_w_custom_type_decoder(self):
db = self.db
await db.test.insert_many(
[
{"status": "in progress", "qty": Int64(1)},
{"status": "complete", "qty": Int64(10)},
{"status": "in progress", "qty": Int64(1)},
{"status": "complete", "qty": Int64(10)},
{"status": "in progress", "qty": Int64(1)},
]
)
test = db.get_collection("test", codec_options=UNINT_DECODER_CODECOPTS)
pipeline: list = [
{"$match": {"status": "complete"}},
{"$group": {"_id": "$status", "total_qty": {"$sum": "$qty"}}},
]
result = await test.aggregate(pipeline)
res = (await result.to_list())[0]
self.assertEqual(res["_id"], "complete")
self.assertIsInstance(res["total_qty"], UndecipherableInt64Type)
self.assertEqual(res["total_qty"].value, 20)
async def test_distinct_w_custom_type(self):
await self.db.drop_collection("test")
test = self.db.get_collection("test", codec_options=UNINT_CODECOPTS)
values = [
UndecipherableInt64Type(1),
UndecipherableInt64Type(2),
UndecipherableInt64Type(3),
{"b": UndecipherableInt64Type(3)},
]
await test.insert_many({"a": val} for val in values)
self.assertEqual(values, await test.distinct("a"))
async def test_find_one_and__w_custom_type_decoder(self):
db = self.db
c = db.get_collection("test", codec_options=UNINT_DECODER_CODECOPTS)
await c.insert_one({"_id": 1, "x": Int64(1)})
doc = await c.find_one_and_update(
{"_id": 1}, {"$inc": {"x": 1}}, return_document=ReturnDocument.AFTER
)
self.assertEqual(doc["_id"], 1)
self.assertIsInstance(doc["x"], UndecipherableInt64Type)
self.assertEqual(doc["x"].value, 2)
doc = await c.find_one_and_replace(
{"_id": 1}, {"x": Int64(3), "y": True}, return_document=ReturnDocument.AFTER
)
self.assertEqual(doc["_id"], 1)
self.assertIsInstance(doc["x"], UndecipherableInt64Type)
self.assertEqual(doc["x"].value, 3)
self.assertEqual(doc["y"], True)
doc = await c.find_one_and_delete({"y": True})
self.assertEqual(doc["_id"], 1)
self.assertIsInstance(doc["x"], UndecipherableInt64Type)
self.assertEqual(doc["x"].value, 3)
self.assertIsNone(await c.find_one())
class TestGridFileCustomType(AsyncIntegrationTest):
async def asyncSetUp(self):
await super().asyncSetUp()
await self.db.drop_collection("fs.files")
await self.db.drop_collection("fs.chunks")
async def test_grid_out_custom_opts(self):
db = self.db.with_options(codec_options=UPPERSTR_DECODER_CODECOPTS)
one = AsyncGridIn(
db.fs,
_id=5,
filename="my_file",
chunkSize=1000,
metadata={"foo": "red", "bar": "blue"},
bar=3,
baz="hello",
)
await one.write(b"hello world")
await one.close()
two = AsyncGridOut(db.fs, 5)
await two.open()
self.assertEqual("my_file", two.name)
self.assertEqual("my_file", two.filename)
self.assertEqual(5, two._id)
self.assertEqual(11, two.length)
self.assertEqual(1000, two.chunk_size)
self.assertIsInstance(two.upload_date, datetime.datetime)
self.assertEqual({"foo": "red", "bar": "blue"}, two.metadata)
self.assertEqual(3, two.bar)
for attr in [
"_id",
"name",
"content_type",
"length",
"chunk_size",
"upload_date",
"aliases",
"metadata",
"md5",
]:
self.assertRaises(AttributeError, setattr, two, attr, 5)
class ChangeStreamsWCustomTypesTestMixin:
@no_type_check
async def change_stream(self, *args, **kwargs):
stream = await self.watched_target.watch(*args, max_await_time_ms=1, **kwargs)
self.addAsyncCleanup(stream.close)
return stream
@no_type_check
async def insert_and_check(self, change_stream, insert_doc, expected_doc):
await self.input_target.insert_one(insert_doc)
change = await anext(change_stream)
self.assertEqual(change["fullDocument"], expected_doc)
@no_type_check
async def kill_change_stream_cursor(self, change_stream):
# Cause a cursor not found error on the next getMore.
cursor = change_stream._cursor
address = _CursorAddress(cursor.address, cursor._ns)
client = self.input_target.database.client
await client._close_cursor_now(cursor.cursor_id, address)
@no_type_check
async def test_simple(self):
codecopts = CodecOptions(
type_registry=TypeRegistry([UndecipherableIntEncoder(), UppercaseTextDecoder()])
)
await self.create_targets(codec_options=codecopts)
input_docs = [
{"_id": UndecipherableInt64Type(1), "data": "hello"},
{"_id": 2, "data": "world"},
{"_id": UndecipherableInt64Type(3), "data": "!"},
]
expected_docs = [
{"_id": 1, "data": "HELLO"},
{"_id": 2, "data": "WORLD"},
{"_id": 3, "data": "!"},
]
change_stream = await self.change_stream()
await self.insert_and_check(change_stream, input_docs[0], expected_docs[0])
await self.kill_change_stream_cursor(change_stream)
await self.insert_and_check(change_stream, input_docs[1], expected_docs[1])
await self.kill_change_stream_cursor(change_stream)
await self.insert_and_check(change_stream, input_docs[2], expected_docs[2])
@no_type_check
async def test_custom_type_in_pipeline(self):
codecopts = CodecOptions(
type_registry=TypeRegistry([UndecipherableIntEncoder(), UppercaseTextDecoder()])
)
await self.create_targets(codec_options=codecopts)
input_docs = [
{"_id": UndecipherableInt64Type(1), "data": "hello"},
{"_id": 2, "data": "world"},
{"_id": UndecipherableInt64Type(3), "data": "!"},
]
expected_docs = [{"_id": 2, "data": "WORLD"}, {"_id": 3, "data": "!"}]
# UndecipherableInt64Type should be encoded with the TypeRegistry.
change_stream = await self.change_stream(
[{"$match": {"documentKey._id": {"$gte": UndecipherableInt64Type(2)}}}]
)
await self.input_target.insert_one(input_docs[0])
await self.insert_and_check(change_stream, input_docs[1], expected_docs[0])
await self.kill_change_stream_cursor(change_stream)
await self.insert_and_check(change_stream, input_docs[2], expected_docs[1])
@no_type_check
async def test_break_resume_token(self):
# Get one document from a change stream to determine resumeToken type.
await self.create_targets()
change_stream = await self.change_stream()
await self.input_target.insert_one({"data": "test"})
change = await anext(change_stream)
resume_token_decoder = type_obfuscating_decoder_factory(type(change["_id"]["_data"]))
# Custom-decoding the resumeToken type breaks resume tokens.
codecopts = CodecOptions(
type_registry=TypeRegistry([resume_token_decoder(), UndecipherableIntEncoder()])
)
# Re-create targets, change stream and proceed.
await self.create_targets(codec_options=codecopts)
docs = [{"_id": 1}, {"_id": 2}, {"_id": 3}]
change_stream = await self.change_stream()
await self.insert_and_check(change_stream, docs[0], docs[0])
await self.kill_change_stream_cursor(change_stream)
await self.insert_and_check(change_stream, docs[1], docs[1])
await self.kill_change_stream_cursor(change_stream)
await self.insert_and_check(change_stream, docs[2], docs[2])
@no_type_check
async def test_document_class(self):
async def run_test(doc_cls):
codecopts = CodecOptions(
type_registry=TypeRegistry([UppercaseTextDecoder(), UndecipherableIntEncoder()]),
document_class=doc_cls,
)
await self.create_targets(codec_options=codecopts)
change_stream = await self.change_stream()
doc = {"a": UndecipherableInt64Type(101), "b": "xyz"}
await self.input_target.insert_one(doc)
change = await anext(change_stream)
self.assertIsInstance(change, doc_cls)
self.assertEqual(change["fullDocument"]["a"], 101)
self.assertEqual(change["fullDocument"]["b"], "XYZ")
for doc_cls in [OrderedDict, RawBSONDocument]:
await run_test(doc_cls)
class TestCollectionChangeStreamsWCustomTypes(
AsyncIntegrationTest, ChangeStreamsWCustomTypesTestMixin
):
@async_client_context.require_change_streams
async def asyncSetUp(self):
await super().asyncSetUp()
await self.db.test.delete_many({})
async def asyncTearDown(self):
await self.input_target.drop()
async def create_targets(self, *args, **kwargs):
self.watched_target = self.db.get_collection("test", *args, **kwargs)
self.input_target = self.watched_target
# Ensure the collection exists and is empty.
await self.input_target.insert_one({})
await self.input_target.delete_many({})
class TestDatabaseChangeStreamsWCustomTypes(
AsyncIntegrationTest, ChangeStreamsWCustomTypesTestMixin
):
@async_client_context.require_version_min(4, 2, 0)
@async_client_context.require_change_streams
async def asyncSetUp(self):
await super().asyncSetUp()
await self.db.test.delete_many({})
async def asyncTearDown(self):
await self.input_target.drop()
await self.client.drop_database(self.watched_target)
async def create_targets(self, *args, **kwargs):
self.watched_target = self.client.get_database(self.db.name, *args, **kwargs)
self.input_target = self.watched_target.test
# Insert a record to ensure db, coll are created.
await self.input_target.insert_one({"data": "dummy"})
class TestClusterChangeStreamsWCustomTypes(
AsyncIntegrationTest, ChangeStreamsWCustomTypesTestMixin
):
@async_client_context.require_version_min(4, 2, 0)
@async_client_context.require_change_streams
async def asyncSetUp(self):
await super().asyncSetUp()
await self.db.test.delete_many({})
async def asyncTearDown(self):
await self.input_target.drop()
await self.client.drop_database(self.db)
async def create_targets(self, *args, **kwargs):
codec_options = kwargs.pop("codec_options", None)
if codec_options:
kwargs["type_registry"] = codec_options.type_registry
kwargs["document_class"] = codec_options.document_class
self.watched_target = await self.async_rs_client(*args, **kwargs)
self.input_target = self.watched_target[self.db.name].test
# Insert a record to ensure db, coll are created.
await self.input_target.insert_one({"data": "dummy"})
if __name__ == "__main__":
unittest.main()
|