Skip to content

PYTHON-5782 Coverage increase for message.py#2772

Open
aclark4life wants to merge 9 commits into
mongodb:masterfrom
aclark4life:PYTHON-5782
Open

PYTHON-5782 Coverage increase for message.py#2772
aclark4life wants to merge 9 commits into
mongodb:masterfrom
aclark4life:PYTHON-5782

Conversation

@aclark4life

@aclark4life aclark4life commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

PYTHON-5782

Changes in this PR

Adds test/test_message.py with unit tests for pymongo/message.py.

Tests cover:

Result / error conversion

  • _convert_exception() — converts an Exception into an errmsg/errtype failure document for event publishing
  • _convert_client_bulk_exception() — same as above but also captures the error code, 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() — raises DocumentTooLarge with a size-aware message for inserts or a generic message for other operations

Wire-format message construction

  • _op_msg() — builds an OP_MSG wire message, injecting $db and $readPreference, and temporarily popping document-sequence fields before encoding

Command builders

  • _gen_find_command() — builds a find command document from a query spec, applying projections, skip/limit, batch size, read concern, collation, and cursor options
  • _gen_get_more_command() — builds a getMore command document, conditionally including batch size, maxTimeMS, and comment (gated on wire version ≥ 9)

Read preference

  • _maybe_add_read_preference() — injects $readPreference into a query spec for non-primary modes, skipping plain secondaryPreferred when no tags or maxStalenessSeconds are set

Test Plan

Added unit tests using a real ZlibContext for compression paths and a MagicMock connection for _gen_get_more_command. Focuses on the pure-Python code paths without requiring a live MongoDB server.

hatch run test:test test/test_message.py -v

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

Copilot AI review requested due to automatic review settings April 23, 2026 21:42
@aclark4life aclark4life requested a review from a team as a code owner April 23, 2026 21:42
@aclark4life aclark4life requested a review from Jibola April 23, 2026 21:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py with 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-commenter

codecov-commenter commented Apr 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Comment thread test/test_message.py
Comment thread test/test_message.py Outdated
Comment thread test/test_message.py Outdated
@aclark4life aclark4life force-pushed the PYTHON-5782 branch 2 times, most recently from a6c8f8e to a8d9f92 Compare May 6, 2026 20:34
@aclark4life aclark4life requested a review from Copilot May 6, 2026 20:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@aclark4life aclark4life force-pushed the PYTHON-5782 branch 3 times, most recently from 4baf2a6 to 7b31987 Compare May 6, 2026 20:53
@aclark4life aclark4life requested a review from Copilot May 7, 2026 02:02
@aclark4life aclark4life changed the title PYTHON-5782 Increase code coverage for message.py PYTHON-5782 Improve code coverage for message.py May 7, 2026
@aclark4life aclark4life changed the title PYTHON-5782 Improve code coverage for message.py PYTHON-5782 Improve coverage for message.py May 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@aclark4life aclark4life changed the title PYTHON-5782 Improve coverage for message.py PYTHON-5782 Improve coverage for message.py May 7, 2026
@aclark4life aclark4life changed the title PYTHON-5782 Improve coverage for message.py PYTHON-5782 Coverage improvements for message.py May 7, 2026
@aclark4life aclark4life changed the title PYTHON-5782 Coverage improvements for message.py PYTHON-5782 Coverage improvement for message.py May 7, 2026
@aclark4life aclark4life requested a review from Copilot May 7, 2026 02:27
@aclark4life aclark4life changed the title PYTHON-5782 Coverage improvement for message.py PYTHON-5782 Coverage increase for message.py May 7, 2026
@aclark4life aclark4life requested a review from Copilot May 7, 2026 03:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread test/test_message.py Outdated
Comment on lines +28 to +44
from pymongo.compression_support import ZlibContext
from pymongo.errors import DocumentTooLarge, OperationFailure
from pymongo.message import (
_compress,
_convert_client_bulk_exception,
_convert_exception,
_convert_write_result,
_gen_find_command,
_gen_get_more_command,
_get_more_compressed,
_get_more_uncompressed,
_maybe_add_read_preference,
_op_msg,
_query_compressed,
_query_uncompressed,
_raise_document_too_large,
)
@aclark4life aclark4life force-pushed the PYTHON-5782 branch 3 times, most recently from 98ecc43 to 3ab7aa5 Compare June 9, 2026 23:53
- 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)
@aclark4life aclark4life requested review from Copilot and removed request for Jibola June 10, 2026 16:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread test/test_message.py
Comment on lines +109 to +114
def test_update_with_upserted_id(self):
cmd = {"updates": [{"q": {}, "u": {"_id": 42}}]}
result = _convert_write_result("update", cmd, {"n": 1, "upserted": 42})
self.assertIn("upserted", result)
self.assertEqual(result["upserted"][0]["_id"], 42)

Comment thread test/test_message.py Outdated
Comment on lines +143 to +147
class TestOpMsg(unittest.TestCase):
def test_max_doc_size_zero_without_docs(self):
max_doc_size = _op_msg(0, {"ping": 1}, "testdb", None, _OPTS)[3]
self.assertEqual(max_doc_size, 0)

Address Copilot feedback: add tests for legacy upsert _id fallback in
_convert_write_result and for the zlib-compressed _op_msg path.
@aclark4life aclark4life requested a review from Copilot June 10, 2026 16:55
@aclark4life aclark4life marked this pull request as ready for review June 10, 2026 16:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread test/test_message.py
Comment on lines +192 to +197
def test_op_msg_compressed_zlib_header(self):
# Verify the compressed path is taken and produces a valid OP_COMPRESSED frame.
# Header layout (little-endian): [msgLen(4), reqId(4), responseTo(4), opCode(4),
# originalOpcode(4), uncompressedSize(4), compressorId(1)]
ctx = ZlibContext(6)
_, msg, _, _ = _op_msg(0, {"ping": 1}, "testdb", None, _OPTS, ctx=ctx)
Guard zlib compression test with _have_zlib() skip condition.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@aclark4life

Copy link
Copy Markdown
Contributor Author

Test failures flaky:

  Error response from daemon: No such image: quay.io/pypa/manylinux_2_28_aarch64:2026.03.20-1
  Error response from daemon: Get "https://quay.io/v2/": context deadline exceeded
(api/gridfs/asynchronous/grid_file: line   10) timeout   http://dochub.mongodb.org/core/gridfsspec - HTTPSConnectionPool(host='dochub.mongodb.org', port=443): Read timed out. (read timeout=60)

Comment thread test/test_message.py
pref = SecondaryPreferred(tag_sets=[{"dc": "east"}])
spec: dict = {"find": "col"}
result = _maybe_add_read_preference(spec, pref)
self.assertIn("$readPreference", result)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should make the same assertions as test_secondary_adds_read_preference for consistency.

Comment thread test/test_message.py
self.assertEqual(result["upserted"][0]["_id"], 42)

def test_update_legacy_upsert_id_from_update_doc(self):
# Pre-2.6 servers omit "upserted"; _id is extracted from the update doc (takes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support servers <4.2, so no need to test them. Any code that still makes a distinction should be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants