Skip to content
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

PYTHON-4737 Migrate test_binary.py to async #1863

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 TestBinary as BinaryBase
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 = BinaryBase.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 = BinaryBase.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
73 changes: 0 additions & 73 deletions test/test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
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


Expand Down Expand Up @@ -197,25 +196,6 @@ 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

Expand Down Expand Up @@ -257,59 +237,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
75 changes: 74 additions & 1 deletion test/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,12 +32,14 @@
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] = [""]
Expand All @@ -56,6 +59,7 @@
unittest,
)
from test.pymongo_mocks import MockClient
from test.test_binary import TestBinary as BinaryBase
from test.utils import (
NTHREADS,
CMAPListener,
Expand Down Expand Up @@ -1978,6 +1982,75 @@ def test_dict_hints_sort(self):
def test_dict_hints_create_index(self):
self.db.t.create_index({"x": pymongo.ASCENDING})

def test_legacy_java_uuid_roundtrip(self):
data = BinaryBase.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 = BinaryBase.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."""
Expand Down
Loading