Skip to content

Commit

Permalink
chore(pymongo): add testing to pymongo patch (#10745)
Browse files Browse the repository at this point in the history
Tests patching and unpatching pymongo, following the new changes to add
support for pymongo v4.9.1 in [feat(pymongo): add support for
v4.9](#10718)

Additionally, adds fix for an issue with `assert_wrapped` that
previously assumed wrapped object always comes from `wrapt`, when it can
come from internal wrapping function.

## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [x] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)

---------

Co-authored-by: Munir Abdinur <[email protected]>
Co-authored-by: Munir Abdinur <[email protected]>
  • Loading branch information
3 people committed Sep 23, 2024
1 parent 3bffd02 commit 1e8ba1c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
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)
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)

0 comments on commit 1e8ba1c

Please sign in to comment.