fix remaining assertRaises

This commit is contained in:
Bastien Gérard
2019-08-31 22:40:54 +03:00
parent ac25f4b98b
commit 3e764d068c
10 changed files with 76 additions and 80 deletions

View File

@@ -595,12 +595,12 @@ class TestIndexes(unittest.TestCase):
Blog.drop_collection()
with pytest.raises(OperationFailure) as ctx_err:
with pytest.raises(OperationFailure) as exc_info:
Blog(id="garbage").save()
# One of the errors below should happen. Which one depends on the
# PyMongo version and dict order.
err_msg = str(ctx_err.exception)
err_msg = str(exc_info.value)
assert any(
[
"The field 'unique' is not valid for an _id index specification"

View File

@@ -335,13 +335,13 @@ class TestInheritance(MongoDBTestCase):
name = StringField()
# can't inherit because Animal didn't explicitly allow inheritance
with pytest.raises(ValueError) as cm:
with pytest.raises(
ValueError, match="Document Animal may not be subclassed"
) as exc_info:
class Dog(Animal):
pass
assert "Document Animal may not be subclassed" in str(cm.exception)
# Check that _cls etc aren't present on simple documents
dog = Animal(name="dog").save()
assert dog.to_mongo().keys() == ["_id", "name"]
@@ -358,13 +358,13 @@ class TestInheritance(MongoDBTestCase):
name = StringField()
meta = {"allow_inheritance": True}
with pytest.raises(ValueError) as cm:
with pytest.raises(ValueError) as exc_info:
class Mammal(Animal):
meta = {"allow_inheritance": False}
assert (
str(cm.exception)
str(exc_info.value)
== 'Only direct subclasses of Document may set "allow_inheritance" to False'
)

View File

@@ -350,14 +350,11 @@ class TestInstance(MongoDBTestCase):
name = StringField()
meta = {"allow_inheritance": True}
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Cannot override primary key field") as e:
class EmailUser(User):
email = StringField(primary_key=True)
exc = e.exception
assert str(exc) == "Cannot override primary key field"
def test_custom_id_field_is_required(self):
"""Ensure the custom primary key field is required."""
@@ -365,10 +362,9 @@ class TestInstance(MongoDBTestCase):
username = StringField(primary_key=True)
name = StringField()
with pytest.raises(ValidationError) as e:
with pytest.raises(ValidationError) as exc_info:
User(name="test").save()
exc = e.exception
assert "Field is required: ['username']" in str(exc)
assert "Field is required: ['username']" in str(exc_info.value)
def test_document_not_registered(self):
class Place(Document):
@@ -870,12 +866,12 @@ class TestInstance(MongoDBTestCase):
t = TestDocument(doc=TestEmbeddedDocument(x=10, y=25, z=15))
with pytest.raises(ValidationError) as cm:
with pytest.raises(ValidationError) as exc_info:
t.save()
expected_msg = "Value of z != x + y"
assert expected_msg in cm.exception.message
assert cm.exception.to_dict() == {"doc": {"__all__": expected_msg}}
assert expected_msg in str(exc_info.value)
assert exc_info.value.to_dict() == {"doc": {"__all__": expected_msg}}
t = TestDocument(doc=TestEmbeddedDocument(x=10, y=25)).save()
assert t.doc.z == 35
@@ -3208,43 +3204,47 @@ class TestInstance(MongoDBTestCase):
def test_positional_creation(self):
"""Document cannot be instantiated using positional arguments."""
with pytest.raises(TypeError) as e:
with pytest.raises(TypeError) as exc_info:
person = self.Person("Test User", 42)
expected_msg = (
"Instantiating a document with positional arguments is not "
"supported. Please use `field_name=value` keyword arguments."
)
assert str(e.exception) == expected_msg
assert str(exc_info.value) == expected_msg
def test_mixed_creation(self):
"""Document cannot be instantiated using mixed arguments."""
with pytest.raises(TypeError) as e:
with pytest.raises(TypeError) as exc_info:
person = self.Person("Test User", age=42)
expected_msg = (
"Instantiating a document with positional arguments is not "
"supported. Please use `field_name=value` keyword arguments."
)
assert str(e.exception) == expected_msg
assert str(exc_info.value) == expected_msg
def test_positional_creation_embedded(self):
"""Embedded document cannot be created using positional arguments."""
with pytest.raises(TypeError) as e:
with pytest.raises(TypeError) as exc_info:
job = self.Job("Test Job", 4)
expected_msg = (
"Instantiating a document with positional arguments is not "
"supported. Please use `field_name=value` keyword arguments."
)
assert str(e.exception) == expected_msg
assert str(exc_info.value) == expected_msg
def test_mixed_creation_embedded(self):
"""Embedded document cannot be created using mixed arguments."""
with pytest.raises(TypeError) as e:
with pytest.raises(TypeError) as exc_info:
job = self.Job("Test Job", years=4)
expected_msg = (
"Instantiating a document with positional arguments is not "
"supported. Please use `field_name=value` keyword arguments."
)
assert str(e.exception) == expected_msg
assert str(exc_info.value) == expected_msg
def test_data_contains_id_field(self):
"""Ensure that asking for _data returns 'id'."""