-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 3 commits
ea53afc
7effe29
aa7b11d
b80ec9a
06f6929
3cd8f66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
import _thread as thread | ||
import asyncio | ||
import base64 | ||
import contextlib | ||
import copy | ||
import datetime | ||
|
@@ -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 * | ||
from pymongo.operations import _Op | ||
|
||
sys.path[0:0] = [""] | ||
|
@@ -57,6 +60,7 @@ | |
unittest, | ||
) | ||
from test.asynchronous.pymongo_mocks import AsyncMockClient | ||
from test.test_binary import TestBinary | ||
|
||
from test.utils import ( | ||
NTHREADS, | ||
CMAPListener, | ||
|
@@ -2020,6 +2024,78 @@ def test_dict_hints_sort(self): | |
async def test_dict_hints_create_index(self): | ||
await self.db.t.create_index({"x": pymongo.ASCENDING}) | ||
|
||
@async_client_context.require_connection | ||
|
||
async def test_legacy_java_uuid_roundtrip(self): | ||
data = TestBinary.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_client_context.require_connection | ||
async def test_legacy_csharp_uuid_roundtrip(self): | ||
data = TestBinary.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_client_context.require_connection | ||
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.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to avoid * imports for a few reasons: https://stackoverflow.com/questions/2386714/why-is-import-bad