Skip to content

Commit

Permalink
Add fallbacks in case driver module not PEP 249-compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi committed Oct 30, 2024
1 parent c461544 commit daf3a73
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,17 @@ def calculate_commenter_data(

commenter_data = {
"db_driver": f"{db_driver}:{db_version.split(' ')[0]}",
"dbapi_threadsafety": self.connect_module.threadsafety,
"dbapi_level": self.connect_module.apilevel,
"driver_paramstyle": self.connect_module.paramstyle,
# PEP 249-compliant drivers should have the following attributes.
# We can assume apilevel "1.0" if not given.
# We use "unknown" for others to prevent uncaught AttributeError.
# https://peps.python.org/pep-0249/#globals
"dbapi_threadsafety": getattr(
self.connect_module, "threadsafety", "unknown"
),
"dbapi_level": getattr(self.connect_module, "apilevel", "1.0"),
"driver_paramstyle": getattr(
self.connect_module, "paramstyle", "unknown"
),
}

if self.database_system == "postgresql":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,35 @@ def test_executemany_comment(self):
r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)

def test_executemany_comment_non_pep_249_compliant(self):
class MockConnectModule:
def __getattr__(self, name):
if name == "__name__":
return "test"
if name == "__version__":
return mock.MagicMock()
if name == "__libpq_version__":
return 123
raise AttributeError("attribute missing")

connect_module = MockConnectModule()
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"postgresql",
enable_commenter=True,
connect_module=connect_module,
commenter_options={"db_driver": False},
)
mock_connection = db_integration.wrapped_connection(
mock_connect, {}, {}
)
cursor = mock_connection.cursor()
cursor.executemany("Select 1;")
self.assertRegex(
cursor.query,
r"Select 1 /\*dbapi_level='1.0',dbapi_threadsafety='unknown',driver_paramstyle='unknown',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)

def test_compatible_build_version_psycopg_psycopg2_libpq(self):
connect_module = mock.MagicMock()
connect_module.__name__ = "test"
Expand Down

0 comments on commit daf3a73

Please sign in to comment.