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

chore(pymongo): add testing to pymongo patch #10745

Merged
merged 26 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fbcd595
chore(pymongo): test 4.9.1
mabdinur Sep 19, 2024
e347b6d
fix pymongo imports
mabdinur Sep 19, 2024
4b40801
rn
mabdinur Sep 19, 2024
d0a2565
fix imports to use synchronous
mabdinur Sep 19, 2024
baab551
fix imports for pymongo<4.9
mabdinur Sep 19, 2024
1ec0b39
pin pytest version used in mongoengine
mabdinur Sep 19, 2024
267c658
Merge branch 'main' into munir/test-pymongo-4-9
mabdinur Sep 19, 2024
42e8a18
newline
mabdinur Sep 19, 2024
4076433
Merge branch 'main' into munir/test-pymongo-4-9
quinna-h Sep 20, 2024
958a6a2
Update tests
quinna-h Sep 20, 2024
c8ae857
Lint for style
quinna-h Sep 20, 2024
55c4b77
Add fix to assert_wrapped
quinna-h Sep 20, 2024
db43f80
modify test_pymongo_patch
quinna-h Sep 20, 2024
2b74685
Merge branch 'main' into quinna/add-tests-pymongo-patch
quinna-h Sep 20, 2024
87c33be
minor formatting
quinna-h Sep 20, 2024
5bd99b7
Merge remote-tracking branch 'refs/remotes/origin/quinna/add-tests-py…
quinna-h Sep 20, 2024
3916fd3
Merge branch 'munir/test-pymongo-4-9' into quinna/add-tests-pymongo-p…
mabdinur Sep 20, 2024
4cfdc70
Merge branch 'main' into munir/test-pymongo-4-9
mabdinur Sep 20, 2024
9e1d04e
Merge branch 'munir/test-pymongo-4-9' into quinna/add-tests-pymongo-p…
mabdinur Sep 20, 2024
770c530
Merge branch 'main' into munir/test-pymongo-4-9
mabdinur Sep 23, 2024
f6fad57
Fix nits
quinna-h Sep 23, 2024
e3a899d
Merge branch 'munir/test-pymongo-4-9' into quinna/add-tests-pymongo-p…
quinna-h Sep 23, 2024
80e4438
Merge branch 'main' into quinna/add-tests-pymongo-patch
quinna-h Sep 23, 2024
1722006
Merge branch 'main' into quinna/add-tests-pymongo-patch
quinna-h Sep 23, 2024
af4a23d
Merge branch 'main' into quinna/add-tests-pymongo-patch
quinna-h Sep 23, 2024
d92d9b0
Add assert
quinna-h Sep 23, 2024
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
7 changes: 5 additions & 2 deletions tests/contrib/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def assert_not_module_imported(self, modname):
assert not self.module_imported(modname), "{} module is imported".format(modname)

def is_wrapped(self, obj):
return isinstance(obj, wrapt.ObjectProxy)
return isinstance(obj, wrapt.ObjectProxy) or hasattr(obj, "__dd_wrapped__")

def assert_wrapped(self, obj):
"""
Expand All @@ -64,7 +64,10 @@ def assert_not_double_wrapped(self, obj):
This is useful for asserting idempotence.
"""
self.assert_wrapped(obj)
self.assert_not_wrapped(obj.__wrapped__)

wrapped = obj.__wrapped__ if isinstance(obj, wrapt.ObjectProxy) else obj.__dd_wrapped__

self.assert_not_wrapped(wrapped)


def raise_if_no_attrs(f):
Expand Down
4 changes: 2 additions & 2 deletions tests/contrib/pymongo/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from ddtrace import Pin
from ddtrace.contrib.internal.pymongo.client import normalize_filter
from ddtrace.contrib.internal.pymongo.patch import _CHECKOUT_FN_NAME
from ddtrace.contrib.pymongo.patch import patch
from ddtrace.contrib.pymongo.patch import unpatch
from ddtrace.contrib.internal.pymongo.patch import patch
from ddtrace.contrib.internal.pymongo.patch import unpatch
from ddtrace.ext import SpanTypes
from tests.opentracer.utils import init_tracer
from tests.utils import DummyTracer
Expand Down
76 changes: 66 additions & 10 deletions tests/contrib/pymongo/test_pymongo_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@
# script. If you want to make changes to it, you should make sure that you have
# removed the ``_generated`` suffix from the file name, to prevent the content
# from being overwritten by future re-generations.

from ddtrace.contrib.pymongo import get_version
from ddtrace.contrib.pymongo.patch import get_version
from ddtrace.contrib.pymongo.patch import patch
from ddtrace.contrib.pymongo.patch import pymongo
from ddtrace.contrib.pymongo.patch import unpatch
from tests.contrib.patch import PatchTestCase


try:
from ddtrace.contrib.pymongo.patch import unpatch
except ImportError:
unpatch = None
from tests.contrib.patch import PatchTestCase
_VERSION = pymongo.version_tuple

if _VERSION >= (4, 9):
from pymongo.synchronous.pool import Connection
from pymongo.synchronous.server import Server
from pymongo.synchronous.topology import Topology
elif _VERSION >= (4, 5):
from pymongo.pool import Connection
from pymongo.server import Server
from pymongo.topology import Topology
else:
from pymongo.pool import SocketInfo as Connection
from pymongo.server import Server
from pymongo.topology import Topology


class TestPymongoPatch(PatchTestCase.Base):
Expand All @@ -22,10 +33,55 @@ class TestPymongoPatch(PatchTestCase.Base):
__get_version__ = get_version

def assert_module_patched(self, pymongo):
pass
self.assert_wrapped(pymongo.MongoClient.__init__)
self.assert_wrapped(Topology.select_server)

if _VERSION >= (3, 12):
self.assert_wrapped(Server.run_operation)
elif _VERSION >= (3, 9):
self.assert_wrapped(Server.run_operation_with_response)
else:
self.assert_wrapped(Server.send_message_with_response)

if _VERSION >= (4, 5):
self.assert_wrapped(Server.checkout)
else:
self.assert_wrapped(Server.get_socket)
self.assert_wrapped(Connection.command)
self.assert_wrapped(Connection.write_command)

def assert_not_module_patched(self, pymongo):
pass
self.assert_not_wrapped(pymongo.MongoClient.__init__)
self.assert_not_wrapped(Topology.select_server)
if _VERSION >= (3, 12):
self.assert_not_wrapped(Server.run_operation)
elif _VERSION >= (3, 9):
self.assert_not_wrapped(Server.run_operation_with_response)
else:
self.assert_not_wrapped(Server.send_message_with_response)

if _VERSION >= (4, 5):
self.assert_not_wrapped(Server.checkout)
else:
self.assert_not_wrapped(Server.get_socket)

self.assert_not_wrapped(Connection.command)
self.assert_not_wrapped(Connection.write_command)

def assert_not_module_double_patched(self, pymongo):
pass
self.assert_not_double_wrapped(pymongo.MongoClient.__init__)
self.assert_not_double_wrapped(Topology.select_server)
quinna-h marked this conversation as resolved.
Show resolved Hide resolved
self.assert_not_double_wrapped(Connection.command)
self.assert_not_double_wrapped(Connection.write_command)

if _VERSION >= (3, 12):
self.assert_not_double_wrapped(Server.run_operation)
elif _VERSION >= (3, 9):
self.assert_not_double_wrapped(Server.run_operation_with_response)
else:
self.assert_not_double_wrapped(Server.send_message_with_response)

if _VERSION >= (4, 5):
self.assert_not_double_wrapped(Server.checkout)
else:
self.assert_not_double_wrapped(Server.get_socket)
Loading