test(mysql): provide concrete DBAPI connection attributes in mocks#4116
test(mysql): provide concrete DBAPI connection attributes in mocks#4116rite7sh wants to merge 11 commits intoopen-telemetry:mainfrom
Conversation
|
This PR only updates tests and does not change runtime behavior or public APIs. |
instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py
Outdated
Show resolved
Hide resolved
instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py
Outdated
Show resolved
Hide resolved
| cnx.host = "localhost" | ||
| cnx.port = 3306 | ||
|
|
||
| cursor = mock.MagicMock() |
There was a problem hiding this comment.
Since some of this code is open coded in the tests, maybe create an helper and use it there and in the tests that requires it?
There was a problem hiding this comment.
@xrmx gotcha I’ve updated the helper to pass attributes directly via MagicMock() and refactored the repeated setup into a small reusable helper to avoid duplication across tests.
Let me know if you'd prefer a different structure or naming here.
There was a problem hiding this comment.
I meant to call this helper in the tests where you are adding the very same thing but open coded. If you can't do that then try to split this helper in smaller pieces you can reuse in the tests.
There was a problem hiding this comment.
@xrmx Yep, I went with the helper approach you suggested - refactored the repeated MagicMock setup into a shared make_mysql_commenter_mocks helper and updated all SQL commenter tests to use it.
This cleans up a lot of duplication . Let me know if you'd like anything changed or tweaked
|
Please also do a |
- Introduce make_mysql_commenter_mocks helper - Remove duplicated MagicMock setup - Preserve test assertions and behavior - Improve readability and maintainability
|
@tammy-baylis-swi Thanks for the heads up,I ran |
| apilevel="123", | ||
| paramstyle="test", | ||
| mock_connect_module, mock_connection, mock_cursor = ( | ||
| make_mysql_commenter_mocks() |
There was a problem hiding this comment.
I'm looking at one of the last runs of "Run Tests" / tox -e py310-test-instrumentation-mysql-1 on this PR and it looks like some of the warnings are still being emitted, for example:
instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py::TestMysqlIntegration::test_instrument_with_dbapi_sqlcomment_not_enabled_default
-------------------------------- live log call ---------------------------------
WARNING opentelemetry.attributes:__init__.py:111 Invalid type MagicMock for attribute 'db.name' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
WARNING opentelemetry.attributes:__init__.py:111 Invalid type MagicMock for attribute 'net.peer.name' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
WARNING opentelemetry.attributes:__init__.py:111 Invalid type MagicMock for attribute 'net.peer.port' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
PASSED [ 86%]
so these changes don't quite fix the original issue. Maybe check/change how the mock.patch is being applied below before the instrument call to make sure your helper outputs are being used.
Description
This PR updates the MySQL instrumentation integration tests to provide concrete DBAPI connection attributes (
host,port,database) in mocked connection and cursor objects.Previously, several tests emitted OpenTelemetry warnings due to
MagicMockvalues being used for attributes such asdb.name,net.peer.name, andnet.peer.port. These warnings were noisy but did not cause test failures.By setting realistic primitive values in the mocks, the tests now run without attribute type warnings, improving signal-to-noise ratio while keeping behavior unchanged.
Fixes #4114
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Ran MySQL instrumentation integration tests locally: tox -e py310-test-instrumentation-mysql-1
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.