Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
78 changes: 77 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 *
Copy link
Member

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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Importing TestBinary directly will actually cause pytest/unittest to pick up and run TestBinary in this file too. Instead we need to mangle to name, like: from test.test_binary import TestBinary as BinaryBase or move the test data out of TestBinary into to a global var that we can import.

from test.utils import (
NTHREADS,
CMAPListener,
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I beleive we can remove the require_connections now that this is in test_client.

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."""
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
78 changes: 77 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 *
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
from test.utils import (
NTHREADS,
CMAPListener,
Expand Down Expand Up @@ -1978,6 +1982,78 @@ def test_dict_hints_sort(self):
def test_dict_hints_create_index(self):
self.db.t.create_index({"x": pymongo.ASCENDING})

@client_context.require_connection
def test_legacy_java_uuid_roundtrip(self):
data = TestBinary.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")

@client_context.require_connection
def test_legacy_csharp_uuid_roundtrip(self):
data = TestBinary.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)

@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 = 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