Skip to content

Commit 08dbbf7

Browse files
committed
add unified format support
1 parent 92b3046 commit 08dbbf7

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

test/asynchronous/test_client_metadata.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
from __future__ import annotations
1515

1616
import asyncio
17+
import os
18+
import pathlib
1719
import time
1820
import unittest
1921
from test.asynchronous import AsyncIntegrationTest
22+
from test.asynchronous.unified_format import generate_test_classes
2023
from test.utils_shared import CMAPListener
2124
from typing import Any, Optional
2225

@@ -37,6 +40,17 @@
3740

3841
_IS_SYNC = False
3942

43+
# Location of JSON test specifications.
44+
if _IS_SYNC:
45+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "handshake", "unified")
46+
else:
47+
_TEST_PATH = os.path.join(
48+
pathlib.Path(__file__).resolve().parent.parent, "handshake", "unified"
49+
)
50+
51+
# Generate unified tests.
52+
globals().update(generate_test_classes(_TEST_PATH, module=__name__, RUN_ON_SERVERLESS=True))
53+
4054

4155
def _get_handshake_driver_info(request):
4256
assert "client" in request

test/asynchronous/unified_format.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
from pymongo.asynchronous.database import AsyncDatabase
7676
from pymongo.asynchronous.encryption import AsyncClientEncryption
7777
from pymongo.asynchronous.helpers import anext
78+
from pymongo.driver_info import DriverInfo
7879
from pymongo.encryption_options import _HAVE_PYMONGOCRYPT
7980
from pymongo.errors import (
8081
AutoReconnect,
@@ -703,6 +704,7 @@ async def _collectionOperation_createChangeStream(self, target, *args, **kwargs)
703704
async def _databaseOperation_runCommand(self, target, **kwargs):
704705
self.__raise_if_unsupported("runCommand", target, AsyncDatabase)
705706
# Ensure the first key is the command name.
707+
print(kwargs)
706708
ordered_command = SON([(kwargs.pop("command_name"), 1)])
707709
ordered_command.update(kwargs["command"])
708710
kwargs["command"] = ordered_command
@@ -840,6 +842,13 @@ async def _cursor_close(self, target, *args, **kwargs):
840842
self.__raise_if_unsupported("close", target, NonLazyCursor, AsyncCommandCursor)
841843
return await target.close()
842844

845+
async def _clientOperation_appendMetadata(self, target, *args, **kwargs):
846+
print("IN MY FUNC")
847+
print(kwargs)
848+
info_opts = kwargs["driver_info_options"]
849+
driver_info = DriverInfo(info_opts["name"], info_opts["version"], info_opts["platform"])
850+
target.append_metadata(driver_info)
851+
843852
async def _clientEncryptionOperation_createDataKey(self, target, *args, **kwargs):
844853
if "opts" in kwargs:
845854
kwargs.update(camel_to_snake_args(kwargs.pop("opts")))
@@ -925,11 +934,11 @@ async def run_entity_operation(self, spec):
925934
)
926935
else:
927936
arguments = {}
928-
929937
if isinstance(target, AsyncMongoClient):
930938
method_name = f"_clientOperation_{opname}"
931939
elif isinstance(target, AsyncDatabase):
932940
method_name = f"_databaseOperation_{opname}"
941+
print(f"{method_name=}")
933942
elif isinstance(target, AsyncCollection):
934943
method_name = f"_collectionOperation_{opname}"
935944
# contentType is always stored in metadata in pymongo.
@@ -976,6 +985,7 @@ async def run_entity_operation(self, spec):
976985
with pymongo.timeout(timeout):
977986
result = await cmd(**dict(arguments))
978987
else:
988+
print(f"{cmd=} {dict=} {arguments=}")
979989
result = await cmd(**dict(arguments))
980990
except Exception as exc:
981991
# Ignore all operation errors but to avoid masking bugs don't
@@ -1238,6 +1248,7 @@ async def run_special_operation(self, spec):
12381248

12391249
async def run_operations(self, spec):
12401250
for op in spec:
1251+
print(f"{op=}")
12411252
if op["object"] == "testRunner":
12421253
await self.run_special_operation(op)
12431254
else:
@@ -1440,6 +1451,7 @@ async def _run_scenario(self, spec, uri=None):
14401451
await self.check_log_messages(spec["operations"], expect_log_messages)
14411452
else:
14421453
# process operations
1454+
print(f"{spec['operations']=}")
14431455
await self.run_operations(spec["operations"])
14441456

14451457
# process expectEvents

test/test_client_metadata.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
from __future__ import annotations
1515

1616
import asyncio
17+
import os
18+
import pathlib
1719
import time
1820
import unittest
1921
from test import IntegrationTest
22+
from test.unified_format import generate_test_classes
2023
from test.utils_shared import CMAPListener
2124
from typing import Any, Optional
2225

@@ -37,6 +40,17 @@
3740

3841
_IS_SYNC = True
3942

43+
# Location of JSON test specifications.
44+
if _IS_SYNC:
45+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "handshake", "unified")
46+
else:
47+
_TEST_PATH = os.path.join(
48+
pathlib.Path(__file__).resolve().parent.parent, "handshake", "unified"
49+
)
50+
51+
# Generate unified tests.
52+
globals().update(generate_test_classes(_TEST_PATH, module=__name__, RUN_ON_SERVERLESS=True))
53+
4054

4155
def _get_handshake_driver_info(request):
4256
assert "client" in request

test/unified_format.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from bson.objectid import ObjectId
6868
from gridfs import GridFSBucket, GridOut, NoFile
6969
from pymongo import ASCENDING, CursorType, MongoClient, _csot
70+
from pymongo.driver_info import DriverInfo
7071
from pymongo.encryption_options import _HAVE_PYMONGOCRYPT
7172
from pymongo.errors import (
7273
AutoReconnect,
@@ -700,6 +701,7 @@ def _collectionOperation_createChangeStream(self, target, *args, **kwargs):
700701
def _databaseOperation_runCommand(self, target, **kwargs):
701702
self.__raise_if_unsupported("runCommand", target, Database)
702703
# Ensure the first key is the command name.
704+
print(kwargs)
703705
ordered_command = SON([(kwargs.pop("command_name"), 1)])
704706
ordered_command.update(kwargs["command"])
705707
kwargs["command"] = ordered_command
@@ -837,6 +839,13 @@ def _cursor_close(self, target, *args, **kwargs):
837839
self.__raise_if_unsupported("close", target, NonLazyCursor, CommandCursor)
838840
return target.close()
839841

842+
def _clientOperation_appendMetadata(self, target, *args, **kwargs):
843+
print("IN MY FUNC")
844+
print(kwargs)
845+
info_opts = kwargs["driver_info_options"]
846+
driver_info = DriverInfo(info_opts["name"], info_opts["version"], info_opts["platform"])
847+
target.append_metadata(driver_info)
848+
840849
def _clientEncryptionOperation_createDataKey(self, target, *args, **kwargs):
841850
if "opts" in kwargs:
842851
kwargs.update(camel_to_snake_args(kwargs.pop("opts")))
@@ -916,11 +925,11 @@ def run_entity_operation(self, spec):
916925
)
917926
else:
918927
arguments = {}
919-
920928
if isinstance(target, MongoClient):
921929
method_name = f"_clientOperation_{opname}"
922930
elif isinstance(target, Database):
923931
method_name = f"_databaseOperation_{opname}"
932+
print(f"{method_name=}")
924933
elif isinstance(target, Collection):
925934
method_name = f"_collectionOperation_{opname}"
926935
# contentType is always stored in metadata in pymongo.
@@ -967,6 +976,7 @@ def run_entity_operation(self, spec):
967976
with pymongo.timeout(timeout):
968977
result = cmd(**dict(arguments))
969978
else:
979+
print(f"{cmd=} {dict=} {arguments=}")
970980
result = cmd(**dict(arguments))
971981
except Exception as exc:
972982
# Ignore all operation errors but to avoid masking bugs don't
@@ -1225,6 +1235,7 @@ def run_special_operation(self, spec):
12251235

12261236
def run_operations(self, spec):
12271237
for op in spec:
1238+
print(f"{op=}")
12281239
if op["object"] == "testRunner":
12291240
self.run_special_operation(op)
12301241
else:
@@ -1425,6 +1436,7 @@ def _run_scenario(self, spec, uri=None):
14251436
self.check_log_messages(spec["operations"], expect_log_messages)
14261437
else:
14271438
# process operations
1439+
print(f"{spec['operations']=}")
14281440
self.run_operations(spec["operations"])
14291441

14301442
# process expectEvents

0 commit comments

Comments
 (0)