Skip to content

Commit 428b582

Browse files
authored
Merge pull request #2933 from bagerard/improve_cov
Improve test cov
2 parents 1b3f6f5 + d3558fa commit 428b582

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

tests/test_context_managers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from mongoengine import *
1212
from mongoengine.connection import _get_session, get_db
1313
from mongoengine.context_managers import (
14+
_commit_with_retry,
1415
no_dereference,
1516
no_sub_classes,
1617
query_counter,
@@ -58,6 +59,44 @@ def join(self, timeout=None):
5859

5960

6061
class TestContextManagers(MongoDBTestCase):
62+
def test_commit_with_retry__unknown_commit_result__retries_until_success(self):
63+
class Session:
64+
attempts = 0
65+
66+
def commit_transaction(self):
67+
self.attempts += 1
68+
if self.attempts == 1:
69+
raise pymongo.errors.OperationFailure(
70+
"commit failed",
71+
details={
72+
"errorLabels": ["UnknownTransactionCommitResult"],
73+
},
74+
)
75+
76+
session = Session()
77+
78+
_commit_with_retry(session)
79+
80+
assert session.attempts == 2
81+
82+
def test_commit_with_retry__other_commit_failure__raises(self):
83+
error = pymongo.errors.OperationFailure("commit failed")
84+
85+
class Session:
86+
attempts = 0
87+
88+
def commit_transaction(self):
89+
self.attempts += 1
90+
raise error
91+
92+
session = Session()
93+
94+
with pytest.raises(pymongo.errors.OperationFailure) as exc_info:
95+
_commit_with_retry(session)
96+
97+
assert exc_info.value is error
98+
assert session.attempts == 1
99+
61100
def test_set_write_concern(self):
62101
class User(Document):
63102
name = StringField()

tests/test_datastructures.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,15 @@ def test_mappings_protocol(self):
466466
assert dict(d) == {"a": 1, "b": 2}
467467
assert dict(**d) == {"a": 1, "b": 2}
468468

469+
def test_mapping_protocol_methods(self):
470+
d = self.dtype(a=1)
471+
472+
d["b"] = 2
473+
474+
assert d.pop("missing", "default") == "default"
475+
assert list(d.iteritems()) == [("a", 1), ("b", 2)]
476+
assert list(d.iterkeys()) == ["a", "b"]
477+
469478

470479
if __name__ == "__main__":
471480
unittest.main()

0 commit comments

Comments
 (0)