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

@@ -270,10 +270,12 @@ class TestDictField(MongoDBTestCase):
embed = Embedded(name="garbage")
doc = DictFieldTest(dictionary=embed)
with pytest.raises(ValidationError) as ctx_err:
with pytest.raises(ValidationError) as exc_info:
doc.validate()
assert "'dictionary'" in str(ctx_err.exception)
assert "Only dictionaries may be used in a DictField" in str(ctx_err.exception)
error_msg = str(exc_info.value)
assert "'dictionary'" in error_msg
assert "Only dictionaries may be used in a DictField" in error_msg
def test_atomic_update_dict_field(self):
"""Ensure that the entire DictField can be atomically updated."""

View File

@@ -88,9 +88,10 @@ class TestEmailField(MongoDBTestCase):
invalid_idn = ".google.com"
user = User(email="me@%s" % invalid_idn)
with pytest.raises(ValidationError) as ctx_err:
with pytest.raises(ValidationError) as exc_info:
user.validate()
assert "domain failed IDN encoding" in str(ctx_err.exception)
assert "domain failed IDN encoding" in str(exc_info.value)
def test_email_field_ip_domain(self):
class User(Document):

View File

@@ -36,11 +36,11 @@ class TestEmbeddedDocumentField(MongoDBTestCase):
name = StringField()
emb = EmbeddedDocumentField("MyDoc")
with pytest.raises(ValidationError) as ctx:
with pytest.raises(ValidationError) as exc_info:
emb.document_type
assert (
"Invalid embedded document class provided to an EmbeddedDocumentField"
in str(ctx.exception)
in str(exc_info.value)
)
def test_embedded_document_field_only_allow_subclasses_of_embedded_document(self):
@@ -72,9 +72,9 @@ class TestEmbeddedDocumentField(MongoDBTestCase):
p = Person(settings=AdminSettings(foo1="bar1", foo2="bar2"), name="John").save()
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as ctx_err:
with pytest.raises(InvalidQueryError) as exc_info:
Person.objects(settings__notexist="bar").first()
assert unicode(ctx_err.exception) == u'Cannot resolve field "notexist"'
assert unicode(exc_info.value) == u'Cannot resolve field "notexist"'
with pytest.raises(LookUpError):
Person.objects.only("settings.notexist")
@@ -108,9 +108,9 @@ class TestEmbeddedDocumentField(MongoDBTestCase):
p.save()
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as ctx_err:
with pytest.raises(InvalidQueryError) as exc_info:
assert Person.objects(settings__notexist="bar").first().id == p.id
assert unicode(ctx_err.exception) == u'Cannot resolve field "notexist"'
assert unicode(exc_info.value) == u'Cannot resolve field "notexist"'
# Test existing attribute
assert Person.objects(settings__base_foo="basefoo").first().id == p.id
@@ -316,9 +316,9 @@ class TestGenericEmbeddedDocumentField(MongoDBTestCase):
p2 = Person(settings=NonAdminSettings(foo2="bar2")).save()
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as ctx_err:
with pytest.raises(InvalidQueryError) as exc_info:
Person.objects(settings__notexist="bar").first()
assert unicode(ctx_err.exception) == u'Cannot resolve field "notexist"'
assert unicode(exc_info.value) == u'Cannot resolve field "notexist"'
with pytest.raises(LookUpError):
Person.objects.only("settings.notexist")
@@ -344,9 +344,9 @@ class TestGenericEmbeddedDocumentField(MongoDBTestCase):
p.save()
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as ctx_err:
with pytest.raises(InvalidQueryError) as exc_info:
assert Person.objects(settings__notexist="bar").first().id == p.id
assert unicode(ctx_err.exception) == u'Cannot resolve field "notexist"'
assert unicode(exc_info.value) == u'Cannot resolve field "notexist"'
# Test existing attribute
assert Person.objects(settings__base_foo="basefoo").first().id == p.id

View File

@@ -96,13 +96,13 @@ class TestField(MongoDBTestCase):
"it should raise a ValidationError if validation fails"
)
with pytest.raises(DeprecatedError) as ctx_err:
with pytest.raises(DeprecatedError) as exc_info:
Person(name="").validate()
assert str(ctx_err.exception) == error
assert str(exc_info.value) == error
with pytest.raises(DeprecatedError) as ctx_err:
with pytest.raises(DeprecatedError) as exc_info:
Person(name="").save()
assert str(ctx_err.exception) == error
assert str(exc_info.value) == error
def test_custom_field_validation_raise_validation_error(self):
def _not_empty(z):
@@ -114,16 +114,10 @@ class TestField(MongoDBTestCase):
Person.drop_collection()
with pytest.raises(ValidationError) as ctx_err:
with pytest.raises(ValidationError) as exc_info:
Person(name="").validate()
assert "ValidationError (Person:None) (cantbeempty: ['name'])" == str(
ctx_err.exception
)
with pytest.raises(ValidationError):
Person(name="").save()
assert "ValidationError (Person:None) (cantbeempty: ['name'])" == str(
ctx_err.exception
exc_info.value
)
Person(name="garbage").validate()
@@ -1029,9 +1023,9 @@ class TestField(MongoDBTestCase):
if i < 6:
foo.save()
else:
with pytest.raises(ValidationError) as cm:
with pytest.raises(ValidationError) as exc_info:
foo.save()
assert "List is too long" in str(cm.exception)
assert "List is too long" in str(exc_info.value)
def test_list_field_max_length_set_operator(self):
"""Ensure ListField's max_length is respected for a "set" operator."""
@@ -1040,9 +1034,9 @@ class TestField(MongoDBTestCase):
items = ListField(IntField(), max_length=3)
foo = Foo.objects.create(items=[1, 2, 3])
with pytest.raises(ValidationError) as cm:
with pytest.raises(ValidationError) as exc_info:
foo.modify(set__items=[1, 2, 3, 4])
assert "List is too long" in str(cm.exception)
assert "List is too long" in str(exc_info.value)
def test_list_field_rejects_strings(self):
"""Strings aren't valid list field data types."""
@@ -2325,21 +2319,21 @@ class TestEmbeddedDocumentListField(MongoDBTestCase):
# Test with an embeddedDocument instead of a list(embeddedDocument)
# It's an edge case but it used to fail with a vague error, making it difficult to troubleshoot it
post = self.BlogPost(comments=comment)
with pytest.raises(ValidationError) as ctx_err:
with pytest.raises(ValidationError) as exc_info:
post.validate()
assert "'comments'" in str(ctx_err.exception)
assert "Only lists and tuples may be used in a list field" in str(
ctx_err.exception
)
error_msg = str(exc_info.value)
assert "'comments'" in error_msg
assert "Only lists and tuples may be used in a list field" in error_msg
# Test with a Document
post = self.BlogPost(comments=Title(content="garbage"))
with pytest.raises(ValidationError):
with pytest.raises(ValidationError) as exc_info:
post.validate()
assert "'comments'" in str(ctx_err.exception)
assert "Only lists and tuples may be used in a list field" in str(
ctx_err.exception
)
error_msg = str(exc_info.value)
assert "'comments'" in error_msg
assert "Only lists and tuples may be used in a list field" in error_msg
def test_no_keyword_filter(self):
"""

View File

@@ -31,10 +31,10 @@ class TestURLField(MongoDBTestCase):
# TODO fix URL validation - this *IS* a valid URL
# For now we just want to make sure that the error message is correct
with pytest.raises(ValidationError) as ctx_err:
with pytest.raises(ValidationError) as exc_info:
link.validate()
assert (
unicode(ctx_err.exception)
unicode(exc_info.value)
== u"ValidationError (Link:None) (Invalid URL: http://\u043f\u0440\u0438\u0432\u0435\u0442.com: ['url'])"
)