Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bartv committed Jun 24, 2024
1 parent 4a70b8b commit fd5f5f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,13 @@ def _instrument(self, **kwargs):
)

def _uninstrument(self, **__):
for method in [
"execute",
"executemany",
"fetch",
"fetchval",
"fetchrow",
]:
unwrap(asyncpg.connection.Connection, method)

for method in [
"fetch",
"forward",
"fetchrow",
"__anext__",
for cls, methods in [
(asyncpg.connection.Connection, ("execute", "executemany", "fetch", "fetchval", "fetchrow")),
(asyncpg.cursor.Cursor, ("forward", "fetch", "fetchrow")),
(asyncpg.cursor.CursorIterator, ("__anext__", ))
]:
unwrap(asyncpg.cursor, method)
for method_name in methods:
unwrap(cls, method_name)

async def _do_execute(self, func, instance, args, kwargs):
exception = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from asyncpg import Connection
from asyncpg import Connection, cursor

from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor
from opentelemetry.test.test_base import TestBase

from wrapt import ObjectProxy


class TestAsyncPGInstrumentation(TestBase):
def test_duplicated_instrumentation_can_be_uninstrumented(self):
Expand Down Expand Up @@ -34,3 +36,21 @@ def test_duplicated_uninstrumentation(self):
self.assertFalse(
hasattr(method, "_opentelemetry_ext_asyncpg_applied")
)

def test_cursor_instrumentation(self):
def assert_wrapped(assert_fnc):
for cls, methods in [
(cursor.Cursor, ("forward", "fetch", "fetchrow")),
(cursor.CursorIterator, ("__anext__", ))
]:
for method_name in methods:
method = getattr(cls, method_name, None)
assert_fnc(isinstance(method, ObjectProxy), f"{method} isinstance {type(method)}")


assert_wrapped(self.assertFalse)
AsyncPGInstrumentor().instrument()
assert_wrapped(self.assertTrue)
AsyncPGInstrumentor().uninstrument()
assert_wrapped(self.assertFalse)

0 comments on commit fd5f5f7

Please sign in to comment.