From d3558fa4dae15f66da59f346e052db7bd5e787dd Mon Sep 17 00:00:00 2001 From: Bastien Gerard Date: Thu, 30 Jul 2026 23:06:20 +0200 Subject: [PATCH] Improve test cov --- tests/test_context_managers.py | 39 ++++++++++++++++++++++++++++++++++ tests/test_datastructures.py | 9 ++++++++ 2 files changed, 48 insertions(+) diff --git a/tests/test_context_managers.py b/tests/test_context_managers.py index 1333f5574..fd20aa25c 100644 --- a/tests/test_context_managers.py +++ b/tests/test_context_managers.py @@ -11,6 +11,7 @@ from mongoengine import * from mongoengine.connection import _get_session, get_db from mongoengine.context_managers import ( + _commit_with_retry, no_dereference, no_sub_classes, query_counter, @@ -58,6 +59,44 @@ def join(self, timeout=None): class TestContextManagers(MongoDBTestCase): + def test_commit_with_retry__unknown_commit_result__retries_until_success(self): + class Session: + attempts = 0 + + def commit_transaction(self): + self.attempts += 1 + if self.attempts == 1: + raise pymongo.errors.OperationFailure( + "commit failed", + details={ + "errorLabels": ["UnknownTransactionCommitResult"], + }, + ) + + session = Session() + + _commit_with_retry(session) + + assert session.attempts == 2 + + def test_commit_with_retry__other_commit_failure__raises(self): + error = pymongo.errors.OperationFailure("commit failed") + + class Session: + attempts = 0 + + def commit_transaction(self): + self.attempts += 1 + raise error + + session = Session() + + with pytest.raises(pymongo.errors.OperationFailure) as exc_info: + _commit_with_retry(session) + + assert exc_info.value is error + assert session.attempts == 1 + def test_set_write_concern(self): class User(Document): name = StringField() diff --git a/tests/test_datastructures.py b/tests/test_datastructures.py index 209926103..25f1cfe7a 100644 --- a/tests/test_datastructures.py +++ b/tests/test_datastructures.py @@ -466,6 +466,15 @@ def test_mappings_protocol(self): assert dict(d) == {"a": 1, "b": 2} assert dict(**d) == {"a": 1, "b": 2} + def test_mapping_protocol_methods(self): + d = self.dtype(a=1) + + d["b"] = 2 + + assert d.pop("missing", "default") == "default" + assert list(d.iteritems()) == [("a", 1), ("b", 2)] + assert list(d.iterkeys()) == ["a", "b"] + if __name__ == "__main__": unittest.main()