PYTHON-4737 Migrate test_binary.py to async (#1863)

This commit is contained in:
Iris 2024-09-30 11:17:57 -07:00 committed by GitHub
parent 7742b7f24f
commit 1e395de9c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 189 additions and 119 deletions

View File

@ -17,6 +17,7 @@ from __future__ import annotations
import _thread as thread
import asyncio
import base64
import contextlib
import copy
import datetime
@ -31,13 +32,15 @@ import subprocess
import sys
import threading
import time
from typing import Iterable, Type, no_type_check
import uuid
from typing import Any, Iterable, Type, no_type_check
from unittest import mock
from unittest.mock import patch
import pytest
import pytest_asyncio
from bson.binary import CSHARP_LEGACY, JAVA_LEGACY, PYTHON_LEGACY, Binary, UuidRepresentation
from pymongo.operations import _Op
sys.path[0:0] = [""]
@ -57,6 +60,7 @@ from test.asynchronous import (
unittest,
)
from test.asynchronous.pymongo_mocks import AsyncMockClient
from test.test_binary import BinaryData
from test.utils import (
NTHREADS,
CMAPListener,
@ -2020,6 +2024,75 @@ class TestClient(AsyncIntegrationTest):
async def test_dict_hints_create_index(self):
await self.db.t.create_index({"x": pymongo.ASCENDING})
async def test_legacy_java_uuid_roundtrip(self):
data = BinaryData.java_data
docs = bson.decode_all(data, CodecOptions(SON[str, Any], False, JAVA_LEGACY))
await async_client_context.client.pymongo_test.drop_collection("java_uuid")
db = async_client_context.client.pymongo_test
coll = db.get_collection("java_uuid", CodecOptions(uuid_representation=JAVA_LEGACY))
await coll.insert_many(docs)
self.assertEqual(5, await coll.count_documents({}))
async for d in coll.find():
self.assertEqual(d["newguid"], uuid.UUID(d["newguidstring"]))
coll = db.get_collection("java_uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
async for d in coll.find():
self.assertNotEqual(d["newguid"], d["newguidstring"])
await async_client_context.client.pymongo_test.drop_collection("java_uuid")
async def test_legacy_csharp_uuid_roundtrip(self):
data = BinaryData.csharp_data
docs = bson.decode_all(data, CodecOptions(SON[str, Any], False, CSHARP_LEGACY))
await async_client_context.client.pymongo_test.drop_collection("csharp_uuid")
db = async_client_context.client.pymongo_test
coll = db.get_collection("csharp_uuid", CodecOptions(uuid_representation=CSHARP_LEGACY))
await coll.insert_many(docs)
self.assertEqual(5, await coll.count_documents({}))
async for d in coll.find():
self.assertEqual(d["newguid"], uuid.UUID(d["newguidstring"]))
coll = db.get_collection("csharp_uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
async for d in coll.find():
self.assertNotEqual(d["newguid"], d["newguidstring"])
await async_client_context.client.pymongo_test.drop_collection("csharp_uuid")
async def test_uri_to_uuid(self):
uri = "mongodb://foo/?uuidrepresentation=csharpLegacy"
client = await self.async_single_client(uri, connect=False)
self.assertEqual(client.pymongo_test.test.codec_options.uuid_representation, CSHARP_LEGACY)
async def test_uuid_queries(self):
db = async_client_context.client.pymongo_test
coll = db.test
await coll.drop()
uu = uuid.uuid4()
await coll.insert_one({"uuid": Binary(uu.bytes, 3)})
self.assertEqual(1, await coll.count_documents({}))
# Test regular UUID queries (using subtype 4).
coll = db.get_collection(
"test", CodecOptions(uuid_representation=UuidRepresentation.STANDARD)
)
self.assertEqual(0, await coll.count_documents({"uuid": uu}))
await coll.insert_one({"uuid": uu})
self.assertEqual(2, await coll.count_documents({}))
docs = await coll.find({"uuid": uu}).to_list()
self.assertEqual(1, len(docs))
self.assertEqual(uu, docs[0]["uuid"])
# Test both.
uu_legacy = Binary.from_uuid(uu, UuidRepresentation.PYTHON_LEGACY)
predicate = {"uuid": {"$in": [uu, uu_legacy]}}
self.assertEqual(2, await coll.count_documents(predicate))
docs = await coll.find(predicate).to_list()
self.assertEqual(2, len(docs))
await coll.drop()
class TestExhaustCursor(AsyncIntegrationTest):
"""Test that clients properly handle errors from exhaust cursors."""

View File

@ -34,53 +34,49 @@ from bson.binary import *
from bson.codec_options import CodecOptions
from bson.son import SON
from pymongo.common import validate_uuid_representation
from pymongo.synchronous.mongo_client import MongoClient
from pymongo.write_concern import WriteConcern
class BinaryData:
# Generated by the Java driver
from_java = (
b"bAAAAAdfaWQAU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AAAODVkMjAzZDMtN2JkMC00ZWE1LWE3YjktOGFlMjM5MmRiNTY1A"
b"ABkAAAAEF9pZAACAAAABW5ld2d1aWQAEAAAAAPRmIO2auc/Tprq1Z"
b"oQ1oNYAm5ld2d1aWRzdHJpbmcAJQAAAGI2ODM5OGQxLWU3NmEtNGU"
b"zZi05YWVhLWQ1OWExMGQ2ODM1OAAAZAAAABBfaWQAAwAAAAVuZXdn"
b"dWlkABAAAAADISpriopuTEaXIa7arYOCFAJuZXdndWlkc3RyaW5nA"
b"CUAAAA4YTZiMmEyMS02ZThhLTQ2NGMtOTcyMS1hZWRhYWQ4MzgyMT"
b"QAAGQAAAAQX2lkAAQAAAAFbmV3Z3VpZAAQAAAAA98eg0CFpGlPihP"
b"MwOmYGOMCbmV3Z3VpZHN0cmluZwAlAAAANDA4MzFlZGYtYTQ4NS00"
b"ZjY5LThhMTMtY2NjMGU5OTgxOGUzAAA="
)
csharp_data = base64.b64decode(from_csharp)
class TestBinary(unittest.TestCase):
csharp_data: bytes
java_data: bytes
@classmethod
def setUpClass(cls):
# Generated by the Java driver
from_java = (
b"bAAAAAdfaWQAUCBQxkVm+XdxJ9tOBW5ld2d1aWQAEAAAAAMIQkfACFu"
b"Z/0RustLOU/G6Am5ld2d1aWRzdHJpbmcAJQAAAGZmOTk1YjA4LWMwND"
b"ctNDIwOC1iYWYxLTUzY2VkMmIyNmU0NAAAbAAAAAdfaWQAUCBQxkVm+"
b"XdxJ9tPBW5ld2d1aWQAEAAAAANgS/xhRXXv8kfIec+dYdyCAm5ld2d1"
b"aWRzdHJpbmcAJQAAAGYyZWY3NTQ1LTYxZmMtNGI2MC04MmRjLTYxOWR"
b"jZjc5Yzg0NwAAbAAAAAdfaWQAUCBQxkVm+XdxJ9tQBW5ld2d1aWQAEA"
b"AAAAPqREIbhZPUJOSdHCJIgaqNAm5ld2d1aWRzdHJpbmcAJQAAADI0Z"
b"DQ5Mzg1LTFiNDItNDRlYS04ZGFhLTgxNDgyMjFjOWR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00"
b"ZjY5LThhMTMtY2NjMGU5OTgxOGUzAAA="
)
cls.csharp_data = base64.b64decode(from_csharp)
def test_binary(self):
a_string = "hello world"
a_binary = Binary(b"hello world")
@ -159,7 +155,7 @@ class TestBinary(unittest.TestCase):
def test_legacy_java_uuid(self):
# Test decoding
data = self.java_data
data = BinaryData.java_data
docs = bson.decode_all(data, CodecOptions(SON[str, Any], False, PYTHON_LEGACY))
for d in docs:
self.assertNotEqual(d["newguid"], uuid.UUID(d["newguidstring"]))
@ -197,27 +193,8 @@ class TestBinary(unittest.TestCase):
)
self.assertEqual(data, encoded)
@client_context.require_connection
def test_legacy_java_uuid_roundtrip(self):
data = self.java_data
docs = bson.decode_all(data, CodecOptions(SON[str, Any], False, JAVA_LEGACY))
client_context.client.pymongo_test.drop_collection("java_uuid")
db = client_context.client.pymongo_test
coll = db.get_collection("java_uuid", CodecOptions(uuid_representation=JAVA_LEGACY))
coll.insert_many(docs)
self.assertEqual(5, coll.count_documents({}))
for d in coll.find():
self.assertEqual(d["newguid"], uuid.UUID(d["newguidstring"]))
coll = db.get_collection("java_uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
for d in coll.find():
self.assertNotEqual(d["newguid"], d["newguidstring"])
client_context.client.pymongo_test.drop_collection("java_uuid")
def test_legacy_csharp_uuid(self):
data = self.csharp_data
data = BinaryData.csharp_data
# Test decoding
docs = bson.decode_all(data, CodecOptions(SON[str, Any], False, PYTHON_LEGACY))
@ -257,59 +234,6 @@ class TestBinary(unittest.TestCase):
)
self.assertEqual(data, encoded)
@client_context.require_connection
def test_legacy_csharp_uuid_roundtrip(self):
data = self.csharp_data
docs = bson.decode_all(data, CodecOptions(SON[str, Any], False, CSHARP_LEGACY))
client_context.client.pymongo_test.drop_collection("csharp_uuid")
db = client_context.client.pymongo_test
coll = db.get_collection("csharp_uuid", CodecOptions(uuid_representation=CSHARP_LEGACY))
coll.insert_many(docs)
self.assertEqual(5, coll.count_documents({}))
for d in coll.find():
self.assertEqual(d["newguid"], uuid.UUID(d["newguidstring"]))
coll = db.get_collection("csharp_uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
for d in coll.find():
self.assertNotEqual(d["newguid"], d["newguidstring"])
client_context.client.pymongo_test.drop_collection("csharp_uuid")
def test_uri_to_uuid(self):
uri = "mongodb://foo/?uuidrepresentation=csharpLegacy"
client = MongoClient(uri, connect=False)
self.assertEqual(client.pymongo_test.test.codec_options.uuid_representation, CSHARP_LEGACY)
@client_context.require_connection
def test_uuid_queries(self):
db = client_context.client.pymongo_test
coll = db.test
coll.drop()
uu = uuid.uuid4()
coll.insert_one({"uuid": Binary(uu.bytes, 3)})
self.assertEqual(1, coll.count_documents({}))
# Test regular UUID queries (using subtype 4).
coll = db.get_collection(
"test", CodecOptions(uuid_representation=UuidRepresentation.STANDARD)
)
self.assertEqual(0, coll.count_documents({"uuid": uu}))
coll.insert_one({"uuid": uu})
self.assertEqual(2, coll.count_documents({}))
docs = list(coll.find({"uuid": uu}))
self.assertEqual(1, len(docs))
self.assertEqual(uu, docs[0]["uuid"])
# Test both.
uu_legacy = Binary.from_uuid(uu, UuidRepresentation.PYTHON_LEGACY)
predicate = {"uuid": {"$in": [uu, uu_legacy]}}
self.assertEqual(2, coll.count_documents(predicate))
docs = list(coll.find(predicate))
self.assertEqual(2, len(docs))
coll.drop()
def test_pickle(self):
b1 = Binary(b"123", 2)

View File

@ -17,6 +17,7 @@ from __future__ import annotations
import _thread as thread
import asyncio
import base64
import contextlib
import copy
import datetime
@ -31,12 +32,14 @@ import subprocess
import sys
import threading
import time
from typing import Iterable, Type, no_type_check
import uuid
from typing import Any, Iterable, Type, no_type_check
from unittest import mock
from unittest.mock import patch
import pytest
from bson.binary import CSHARP_LEGACY, JAVA_LEGACY, PYTHON_LEGACY, Binary, UuidRepresentation
from pymongo.operations import _Op
sys.path[0:0] = [""]
@ -56,6 +59,7 @@ from test import (
unittest,
)
from test.pymongo_mocks import MockClient
from test.test_binary import BinaryData
from test.utils import (
NTHREADS,
CMAPListener,
@ -1978,6 +1982,75 @@ class TestClient(IntegrationTest):
def test_dict_hints_create_index(self):
self.db.t.create_index({"x": pymongo.ASCENDING})
def test_legacy_java_uuid_roundtrip(self):
data = BinaryData.java_data
docs = bson.decode_all(data, CodecOptions(SON[str, Any], False, JAVA_LEGACY))
client_context.client.pymongo_test.drop_collection("java_uuid")
db = client_context.client.pymongo_test
coll = db.get_collection("java_uuid", CodecOptions(uuid_representation=JAVA_LEGACY))
coll.insert_many(docs)
self.assertEqual(5, coll.count_documents({}))
for d in coll.find():
self.assertEqual(d["newguid"], uuid.UUID(d["newguidstring"]))
coll = db.get_collection("java_uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
for d in coll.find():
self.assertNotEqual(d["newguid"], d["newguidstring"])
client_context.client.pymongo_test.drop_collection("java_uuid")
def test_legacy_csharp_uuid_roundtrip(self):
data = BinaryData.csharp_data
docs = bson.decode_all(data, CodecOptions(SON[str, Any], False, CSHARP_LEGACY))
client_context.client.pymongo_test.drop_collection("csharp_uuid")
db = client_context.client.pymongo_test
coll = db.get_collection("csharp_uuid", CodecOptions(uuid_representation=CSHARP_LEGACY))
coll.insert_many(docs)
self.assertEqual(5, coll.count_documents({}))
for d in coll.find():
self.assertEqual(d["newguid"], uuid.UUID(d["newguidstring"]))
coll = db.get_collection("csharp_uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
for d in coll.find():
self.assertNotEqual(d["newguid"], d["newguidstring"])
client_context.client.pymongo_test.drop_collection("csharp_uuid")
def test_uri_to_uuid(self):
uri = "mongodb://foo/?uuidrepresentation=csharpLegacy"
client = self.single_client(uri, connect=False)
self.assertEqual(client.pymongo_test.test.codec_options.uuid_representation, CSHARP_LEGACY)
def test_uuid_queries(self):
db = client_context.client.pymongo_test
coll = db.test
coll.drop()
uu = uuid.uuid4()
coll.insert_one({"uuid": Binary(uu.bytes, 3)})
self.assertEqual(1, coll.count_documents({}))
# Test regular UUID queries (using subtype 4).
coll = db.get_collection(
"test", CodecOptions(uuid_representation=UuidRepresentation.STANDARD)
)
self.assertEqual(0, coll.count_documents({"uuid": uu}))
coll.insert_one({"uuid": uu})
self.assertEqual(2, coll.count_documents({}))
docs = coll.find({"uuid": uu}).to_list()
self.assertEqual(1, len(docs))
self.assertEqual(uu, docs[0]["uuid"])
# Test both.
uu_legacy = Binary.from_uuid(uu, UuidRepresentation.PYTHON_LEGACY)
predicate = {"uuid": {"$in": [uu, uu_legacy]}}
self.assertEqual(2, coll.count_documents(predicate))
docs = coll.find(predicate).to_list()
self.assertEqual(2, len(docs))
coll.drop()
class TestExhaustCursor(IntegrationTest):
"""Test that clients properly handle errors from exhaust cursors."""