Skip to content

PYTHON-4737 Migrate test_binary.py to async #1863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion test/asynchronous/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import _thread as thread
import asyncio
import base64
import contextlib
import copy
import datetime
Expand All @@ -31,13 +32,15 @@
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] = [""]
Expand All @@ -57,6 +60,7 @@
unittest,
)
from test.asynchronous.pymongo_mocks import AsyncMockClient
from test.test_binary import BinaryData
from test.utils import (
NTHREADS,
CMAPListener,
Expand Down Expand Up @@ -2020,6 +2024,75 @@ def test_dict_hints_sort(self):
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."""
Expand Down
156 changes: 40 additions & 116 deletions test/test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,49 @@
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 TestBinary(unittest.TestCase):
csharp_data: bytes
java_data: bytes
class BinaryData:
# 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"DQ5Mzg1LTFiNDItNDRlYS04ZGFhLTgxNDgyMjFjOWRlNAAAbAAAAAdf"
b"aWQAUCBQxkVm+XdxJ9tRBW5ld2d1aWQAEAAAAANjQBn/aQuNfRyfNyx"
b"29COkAm5ld2d1aWRzdHJpbmcAJQAAADdkOGQwYjY5LWZmMTktNDA2My"
b"1hNDIzLWY0NzYyYzM3OWYxYwAAbAAAAAdfaWQAUCBQxkVm+XdxJ9tSB"
b"W5ld2d1aWQAEAAAAAMtSv/Et1cAQUFHUYevqxaLAm5ld2d1aWRzdHJp"
b"bmcAJQAAADQxMDA1N2I3LWM0ZmYtNGEyZC04YjE2LWFiYWY4NzUxNDc"
b"0MQAA"
)
java_data = base64.b64decode(from_java)

# Generated by the .net driver
from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priopuTEaXIa7arYOCFAJuZXdndWlkc3RyaW5nA"
b"CUAAAA4YTZiMmEyMS02ZThhLTQ2NGMtOTcyMS1hZWRhYWQ4MzgyMT"
b"QAAGQAAAAQX2lkAAQAAAAFbmV3Z3VpZAAQAAAAA98eg0CFpGlPihP"
b"MwOmYGOMCbmV3Z3VpZHN0cmluZwAlAAAANDA4MzFlZGYtYTQ4NS00"
b"ZjY5LThhMTMtY2NjMGU5OTgxOGUzAAA="
)
cls.csharp_data = base64.b64decode(from_csharp)

class TestBinary(unittest.TestCase):
def test_binary(self):
a_string = "hello world"
a_binary = Binary(b"hello world")
Expand Down Expand Up @@ -159,7 +155,7 @@ def test_uuid_subtype_4(self):

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"]))
Expand Down Expand Up @@ -197,27 +193,8 @@ def test_legacy_java_uuid(self):
)
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))
Expand Down Expand Up @@ -257,59 +234,6 @@ def test_legacy_csharp_uuid(self):
)
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)

Expand Down
Loading
Loading