PYTHON-5782 Coverage increase, remove old code in message.py#2772
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a new unit test module to increase coverage of pymongo/message.py by exercising message-building utilities and related pure-Python helper functions without requiring a live MongoDB server.
Changes:
- Adds
test/test_message.pywith unit tests for message helpers (read preference wrapping, command generation, write-result conversion). - Adds tests covering OP_MSG / OP_QUERY / OP_GET_MORE message construction in both compressed and uncompressed paths.
- Adds tests for error conversion and document-too-large error formatting.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
a6c8f8e to
a8d9f92
Compare
4baf2a6 to
7b31987
Compare
message.py
message.pymessage.py
message.pymessage.py
- Replace _MockCtx with real ZlibContext(-1) to satisfy the SnappyContext | ZlibContext | ZstdContext union type - Annotate docs list as list to satisfy the Mapping invariance check - Add # type: ignore[arg-type] on _MockConn call sites for _gen_get_more_command (mocking AsyncConnection | Connection fully would require a much heavier stub)
Address Copilot feedback: add tests for legacy upsert _id fallback in _convert_write_result and for the zlib-compressed _op_msg path.
Guard zlib compression test with _have_zlib() skip condition.
- Use keyword args in all _gen_find_command and _gen_get_more_command calls - Fix test_op_msg_max_doc_size_matches_largest_encoded_doc to use docs of different sizes so the max() assertion is meaningful - Use assertRaisesRegex with the exact message in test_raise_document_too_large_update_generic_message - Remove !r from the DocumentTooLarge message in _raise_document_too_large so it matches
Contributor
|
Typecheck is failing. |
NoahStapp
approved these changes
Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PYTHON-5782
Changes in this PR
Adds
test/test_message.pywith unit tests forpymongo/message.py.Also removes a dead code branch in
_convert_write_result()— a legacy compatibility shim for MongoDB < 2.6 that manually reconstructed the upserted_idwhen it wasn't an ObjectId. MongoDB 2.6+ always returns theupsertedfield directly, so this branch was unreachable.Also fixes a bug in
_raise_document_too_large()where the error message used{operation!r}(repr formatting), producing'update' command document too largeinstead ofupdate command document too large. This mismatched the C extension's%s-formatted equivalent and broke the new test.Tests cover:
Result / error conversion
_convert_exception()— converts anExceptioninto anerrmsg/errtypefailure document for event publishing_convert_client_bulk_exception()— same as above but also captures the errorcode, used by the client-level bulk write API_convert_write_result()— converts a legacy GLE (Get Last Error) write result into write-command format, handling insert/update/delete/upsert, wtimeout, and errInfo branches_raise_document_too_large()— raisesDocumentTooLargewith a size-aware message for inserts or a generic message for other operationsWire-format message construction
_op_msg()— builds an OP_MSG wire message, injecting$dband$readPreference, and temporarily popping document-sequence fields before encodingCommand builders
_gen_find_command()— builds afindcommand document from a query spec, applying projections, skip/limit, batch size, read concern, collation, and cursor options_gen_get_more_command()— builds agetMorecommand document, conditionally including batch size,maxTimeMS, andcomment(gated on wire version ≥ 9)Read preference
_maybe_add_read_preference()— injects$readPreferenceinto a query spec for non-primary modes, skipping plainsecondaryPreferredwhen no tags or maxStalenessSeconds are setTest Plan
Added unit tests using a real
ZlibContextfor compression paths and aMagicMockconnection for_gen_get_more_command. Focuses on the pure-Python code paths without requiring a live MongoDB server.Checklist
Checklist for Author
Checklist for Reviewer