Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/test_context_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions tests/test_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()