PYTHON-5781 Increase code coverage for network_layer.py#2774
PYTHON-5781 Increase code coverage for network_layer.py#2774aclark4life wants to merge 6 commits intomongodb:masterfrom
Conversation
Add test/test_network_layer.py with 56 unit tests covering: - PyMongoProtocol: process_header (including ProtocolError paths), process_compression_header, get_buffer (all state branches), buffer_updated (state machine), close/connection_lost lifecycle - NetworkingInterfaceBase: abstract method NotImplementedError raises - NetworkingInterface: socket delegation methods - AsyncNetworkingInterface: transport/protocol delegation - sendall: trivial delegation - _async_socket_receive: success and connection-closed paths
There was a problem hiding this comment.
Pull request overview
Adds a dedicated unit test suite to increase coverage of pymongo/network_layer.py, focusing on logic that can be validated without live sockets or a running MongoDB server.
Changes:
- Introduces
test/test_network_layer.pywith unit tests forPyMongoProtocolstate transitions and message parsing helpers. - Adds delegation/behavior tests for
NetworkingInterfaceBase,NetworkingInterface, andAsyncNetworkingInterface. - Adds tests for small helper functions (
sendall,_async_socket_receive) using mocks.
| def test_returns_op_code_and_compressor_id(self): | ||
| async def _test(): | ||
| proto = await _make_protocol() | ||
| # op_code=2013, unknown int=0, compressor_id=1 (snappy) |
There was a problem hiding this comment.
The comment describing the OP_COMPRESSED header layout is inaccurate: the second int in <iiB is the uncompressed message size (per the wire protocol), not an “unknown int”. Updating the comment will avoid confusion for future readers maintaining these tests.
| # op_code=2013, unknown int=0, compressor_id=1 (snappy) | |
| # op_code=2013, uncompressed message size=0, compressor_id=1 (snappy) |
Pass memoryview instead of bytearray to NetworkingInterface.recv_into, matching the declared type signature (bytes | memoryview[int]).
- Add test/asynchronous/test_network_layer.py as the async source of truth with AsyncUnitTest and _IS_SYNC = False - Regenerate test/test_network_layer.py via synchro from the async source - Register test_network_layer.py in synchro converted_tests (alpha order) - Add "AsyncMock": "MagicMock" replacement after AsyncMockPool to avoid substring collision in translate_docstrings
| "SpecRunnerTask": "SpecRunnerThread", | ||
| "AsyncMockConnection": "MockConnection", | ||
| "AsyncMockPool": "MockPool", | ||
| "AsyncMock": "MagicMock", |
There was a problem hiding this comment.
The replacement mapping "AsyncMock": "MagicMock" is overly broad and will affect unrelated identifiers during synchro conversion (e.g. AsyncMockMonitor in test/asynchronous/pymongo_mocks.py, which currently becomes SyncMockMonitor in test/pymongo_mocks.py). This risks changing many generated sync test/module symbols unintentionally depending on replacement ordering. Consider narrowing the replacement to the specific unittest.mock type usage (e.g. only rewrite the from unittest.mock import AsyncMock import and/or AsyncMock( call sites), or add explicit replacements for the non-unittest symbols you need to preserve before introducing a generic AsyncMock rule.
| "AsyncMock": "MagicMock", | |
| r"\bAsyncMock\b": "MagicMock", |
PYTHON-5781
Changes in this PR
Adds
test/test_network_layer.pywith unit tests forpymongo/network_layer.py. Tests coverPyMongoProtocolstate machine (process_headerincluding allProtocolErrorpaths,process_compression_header,get_bufferall state branches,buffer_updated,close,connection_lost),NetworkingInterfaceBaseabstract methods,NetworkingInterfacesocket delegation,AsyncNetworkingInterfacetransport/protocol delegation,sendall, and_async_socket_receive.Test Plan
Added unit tests using mock transports and
asyncio.run()to exercise the asyncio-dependent protocol code. No live socket or MongoDB server required.Checklist
Checklist for Author
Checklist for Reviewer