autoformat with pyupgrade

This commit is contained in:
Bastien Gerard
2020-12-08 23:14:33 +01:00
parent c00a378776
commit eabb8f60f5
36 changed files with 207 additions and 212 deletions

View File

@@ -19,7 +19,7 @@ class TestBinaryField(MongoDBTestCase):
content_type = StringField()
blob = BinaryField()
BLOB = "\xe6\x00\xc4\xff\x07".encode("latin-1")
BLOB = b"\xe6\x00\xc4\xff\x07"
MIME_TYPE = "application/octet-stream"
Attachment.drop_collection()
@@ -43,11 +43,11 @@ class TestBinaryField(MongoDBTestCase):
attachment_required = AttachmentRequired()
with pytest.raises(ValidationError):
attachment_required.validate()
attachment_required.blob = Binary("\xe6\x00\xc4\xff\x07".encode("latin-1"))
attachment_required.blob = Binary(b"\xe6\x00\xc4\xff\x07")
attachment_required.validate()
_5_BYTES = "\xe6\x00\xc4\xff\x07".encode("latin-1")
_4_BYTES = "\xe6\x00\xc4\xff".encode("latin-1")
_5_BYTES = b"\xe6\x00\xc4\xff\x07"
_4_BYTES = b"\xe6\x00\xc4\xff"
with pytest.raises(ValidationError):
AttachmentSizeLimit(blob=_5_BYTES).validate()
AttachmentSizeLimit(blob=_4_BYTES).validate()
@@ -58,7 +58,7 @@ class TestBinaryField(MongoDBTestCase):
class Attachment(Document):
blob = BinaryField()
for invalid_data in (2, u"Im_a_unicode", ["some_str"]):
for invalid_data in (2, "Im_a_unicode", ["some_str"]):
with pytest.raises(ValidationError):
Attachment(blob=invalid_data).validate()
@@ -129,7 +129,7 @@ class TestBinaryField(MongoDBTestCase):
MyDocument.drop_collection()
bin_data = "\xe6\x00\xc4\xff\x07".encode("latin-1")
bin_data = b"\xe6\x00\xc4\xff\x07"
doc = MyDocument(bin_field=bin_data).save()
n_updated = MyDocument.objects(bin_field=bin_data).update_one(

View File

@@ -190,9 +190,9 @@ class TestCachedReferenceField(MongoDBTestCase):
assert dict(a2.to_mongo()) == {
"_id": a2.pk,
"name": u"Wilson Junior",
"tp": u"pf",
"father": {"_id": a1.pk, "tp": u"pj"},
"name": "Wilson Junior",
"tp": "pf",
"father": {"_id": a1.pk, "tp": "pj"},
}
assert Person.objects(father=a1)._query == {"father._id": a1.pk}
@@ -204,9 +204,9 @@ class TestCachedReferenceField(MongoDBTestCase):
a2.reload()
assert dict(a2.to_mongo()) == {
"_id": a2.pk,
"name": u"Wilson Junior",
"tp": u"pf",
"father": {"_id": a1.pk, "tp": u"pf"},
"name": "Wilson Junior",
"tp": "pf",
"father": {"_id": a1.pk, "tp": "pf"},
}
def test_cached_reference_fields_on_embedded_documents(self):

View File

@@ -30,11 +30,11 @@ class TestEmailField(MongoDBTestCase):
user.validate()
# unicode domain
user = User(email=u"user@пример.рф")
user = User(email="user@пример.рф")
user.validate()
# invalid unicode domain
user = User(email=u"user@пример")
user = User(email="user@пример")
with pytest.raises(ValidationError):
user.validate()
@@ -48,7 +48,7 @@ class TestEmailField(MongoDBTestCase):
email = EmailField()
# unicode user shouldn't validate by default...
user = User(email=u"Dörte@Sörensen.example.com")
user = User(email="Dörte@Sörensen.example.com")
with pytest.raises(ValidationError):
user.validate()
@@ -56,7 +56,7 @@ class TestEmailField(MongoDBTestCase):
class User(Document):
email = EmailField(allow_utf8_user=True)
user = User(email=u"Dörte@Sörensen.example.com")
user = User(email="Dörte@Sörensen.example.com")
user.validate()
def test_email_field_domain_whitelist(self):

View File

@@ -74,7 +74,7 @@ class TestEmbeddedDocumentField(MongoDBTestCase):
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as exc_info:
Person.objects(settings__notexist="bar").first()
assert str(exc_info.value) == u'Cannot resolve field "notexist"'
assert str(exc_info.value) == 'Cannot resolve field "notexist"'
with pytest.raises(LookUpError):
Person.objects.only("settings.notexist")
@@ -110,7 +110,7 @@ class TestEmbeddedDocumentField(MongoDBTestCase):
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as exc_info:
assert Person.objects(settings__notexist="bar").first().id == p.id
assert str(exc_info.value) == u'Cannot resolve field "notexist"'
assert str(exc_info.value) == 'Cannot resolve field "notexist"'
# Test existing attribute
assert Person.objects(settings__base_foo="basefoo").first().id == p.id
@@ -318,7 +318,7 @@ class TestGenericEmbeddedDocumentField(MongoDBTestCase):
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as exc_info:
Person.objects(settings__notexist="bar").first()
assert str(exc_info.value) == u'Cannot resolve field "notexist"'
assert str(exc_info.value) == 'Cannot resolve field "notexist"'
with pytest.raises(LookUpError):
Person.objects.only("settings.notexist")
@@ -346,7 +346,7 @@ class TestGenericEmbeddedDocumentField(MongoDBTestCase):
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as exc_info:
assert Person.objects(settings__notexist="bar").first().id == p.id
assert str(exc_info.value) == u'Cannot resolve field "notexist"'
assert str(exc_info.value) == 'Cannot resolve field "notexist"'
# Test existing attribute
assert Person.objects(settings__base_foo="basefoo").first().id == p.id

View File

@@ -292,7 +292,7 @@ class TestField(MongoDBTestCase):
HandleNoneFields.drop_collection()
doc = HandleNoneFields()
doc.str_fld = u"spam ham egg"
doc.str_fld = "spam ham egg"
doc.int_fld = 42
doc.flt_fld = 4.2
doc.com_dt_fld = datetime.datetime.utcnow()
@@ -328,7 +328,7 @@ class TestField(MongoDBTestCase):
HandleNoneFields.drop_collection()
doc = HandleNoneFields()
doc.str_fld = u"spam ham egg"
doc.str_fld = "spam ham egg"
doc.int_fld = 42
doc.flt_fld = 4.2
doc.comp_dt_fld = datetime.datetime.utcnow()
@@ -426,9 +426,9 @@ class TestField(MongoDBTestCase):
def test_list_validation(self):
"""Ensure that a list field only accepts lists with valid elements."""
access_level_choices = (
("a", u"Administration"),
("b", u"Manager"),
("c", u"Staff"),
("a", "Administration"),
("b", "Manager"),
("c", "Staff"),
)
class User(Document):
@@ -476,7 +476,7 @@ class TestField(MongoDBTestCase):
post.access_list = ["a", "b"]
post.validate()
assert post.get_access_list_display() == u"Administration, Manager"
assert post.get_access_list_display() == "Administration, Manager"
post.comments = ["a"]
with pytest.raises(ValidationError):
@@ -2028,8 +2028,8 @@ class TestField(MongoDBTestCase):
"""Ensure that error messages are correct."""
SIZES = ("S", "M", "L", "XL", "XXL")
COLORS = (("R", "Red"), ("B", "Blue"))
SIZE_MESSAGE = u"Value must be one of ('S', 'M', 'L', 'XL', 'XXL')"
COLOR_MESSAGE = u"Value must be one of ['R', 'B']"
SIZE_MESSAGE = "Value must be one of ('S', 'M', 'L', 'XL', 'XXL')"
COLOR_MESSAGE = "Value must be one of ['R', 'B']"
class Shirt(Document):
size = StringField(max_length=3, choices=SIZES)
@@ -2092,7 +2092,7 @@ class TestField(MongoDBTestCase):
assert "comments" in error_dict
assert 1 in error_dict["comments"]
assert "content" in error_dict["comments"][1]
assert error_dict["comments"][1]["content"] == u"Field is required"
assert error_dict["comments"][1]["content"] == "Field is required"
post.comments[1].content = "here we go"
post.validate()
@@ -2104,7 +2104,7 @@ class TestField(MongoDBTestCase):
class EnumField(BaseField):
def __init__(self, **kwargs):
super(EnumField, self).__init__(**kwargs)
super().__init__(**kwargs)
def to_mongo(self, value):
return value
@@ -2606,11 +2606,11 @@ class TestEmbeddedDocumentListField(MongoDBTestCase):
"""
post = self.BlogPost(
comments=[
self.Comments(author="user1", message=u"сообщение"),
self.Comments(author="user2", message=u"хабарлама"),
self.Comments(author="user1", message="сообщение"),
self.Comments(author="user2", message="хабарлама"),
]
).save()
assert post.comments.get(message=u"сообщение").author == "user1"
assert post.comments.get(message="сообщение").author == "user1"
def test_save(self):
"""

View File

@@ -55,7 +55,7 @@ class TestFileField(MongoDBTestCase):
PutFile.drop_collection()
text = "Hello, World!".encode("latin-1")
text = b"Hello, World!"
content_type = "text/plain"
putfile = PutFile()
@@ -97,8 +97,8 @@ class TestFileField(MongoDBTestCase):
StreamFile.drop_collection()
text = "Hello, World!".encode("latin-1")
more_text = "Foo Bar".encode("latin-1")
text = b"Hello, World!"
more_text = b"Foo Bar"
content_type = "text/plain"
streamfile = StreamFile()
@@ -133,8 +133,8 @@ class TestFileField(MongoDBTestCase):
StreamFile.drop_collection()
text = "Hello, World!".encode("latin-1")
more_text = "Foo Bar".encode("latin-1")
text = b"Hello, World!"
more_text = b"Foo Bar"
streamfile = StreamFile()
streamfile.save()
@@ -163,8 +163,8 @@ class TestFileField(MongoDBTestCase):
class SetFile(Document):
the_file = FileField()
text = "Hello, World!".encode("latin-1")
more_text = "Foo Bar".encode("latin-1")
text = b"Hello, World!"
more_text = b"Foo Bar"
SetFile.drop_collection()
@@ -192,7 +192,7 @@ class TestFileField(MongoDBTestCase):
GridDocument.drop_collection()
with tempfile.TemporaryFile() as f:
f.write("Hello World!".encode("latin-1"))
f.write(b"Hello World!")
f.flush()
# Test without default
@@ -209,7 +209,7 @@ class TestFileField(MongoDBTestCase):
assert doc_b.the_file.grid_id == doc_c.the_file.grid_id
# Test with default
doc_d = GridDocument(the_file="".encode("latin-1"))
doc_d = GridDocument(the_file=b"")
doc_d.save()
doc_e = GridDocument.objects.with_id(doc_d.id)
@@ -235,7 +235,7 @@ class TestFileField(MongoDBTestCase):
# First instance
test_file = TestFile()
test_file.name = "Hello, World!"
test_file.the_file.put("Hello, World!".encode("latin-1"))
test_file.the_file.put(b"Hello, World!")
test_file.save()
# Second instance
@@ -291,9 +291,7 @@ class TestFileField(MongoDBTestCase):
test_file = TestFile()
assert not bool(test_file.the_file)
test_file.the_file.put(
"Hello, World!".encode("latin-1"), content_type="text/plain"
)
test_file.the_file.put(b"Hello, World!", content_type="text/plain")
test_file.save()
assert bool(test_file.the_file)
@@ -315,7 +313,7 @@ class TestFileField(MongoDBTestCase):
class TestFile(Document):
the_file = FileField()
text = "Hello, World!".encode("latin-1")
text = b"Hello, World!"
content_type = "text/plain"
testfile = TestFile()
@@ -359,7 +357,7 @@ class TestFileField(MongoDBTestCase):
testfile.the_file.put(text, content_type=content_type, filename="hello")
testfile.save()
text = "Bonjour, World!".encode("latin-1")
text = b"Bonjour, World!"
testfile.the_file.replace(text, content_type=content_type, filename="hello")
testfile.save()
@@ -383,7 +381,7 @@ class TestFileField(MongoDBTestCase):
TestImage.drop_collection()
with tempfile.TemporaryFile() as f:
f.write("Hello World!".encode("latin-1"))
f.write(b"Hello World!")
f.flush()
t = TestImage()
@@ -499,21 +497,21 @@ class TestFileField(MongoDBTestCase):
# First instance
test_file = TestFile()
test_file.name = "Hello, World!"
test_file.the_file.put("Hello, World!".encode("latin-1"), name="hello.txt")
test_file.the_file.put(b"Hello, World!", name="hello.txt")
test_file.save()
data = get_db("test_files").macumba.files.find_one()
assert data.get("name") == "hello.txt"
test_file = TestFile.objects.first()
assert test_file.the_file.read() == "Hello, World!".encode("latin-1")
assert test_file.the_file.read() == b"Hello, World!"
test_file = TestFile.objects.first()
test_file.the_file = "Hello, World!".encode("latin-1")
test_file.the_file = b"Hello, World!"
test_file.save()
test_file = TestFile.objects.first()
assert test_file.the_file.read() == "Hello, World!".encode("latin-1")
assert test_file.the_file.read() == b"Hello, World!"
def test_copyable(self):
class PutFile(Document):
@@ -521,7 +519,7 @@ class TestFileField(MongoDBTestCase):
PutFile.drop_collection()
text = "Hello, World!".encode("latin-1")
text = b"Hello, World!"
content_type = "text/plain"
putfile = PutFile()

View File

@@ -8,7 +8,7 @@ class TestGeoField(MongoDBTestCase):
def _test_for_expected_error(self, Cls, loc, expected):
try:
Cls(loc=loc).validate()
self.fail("Should not validate the location {0}".format(loc))
self.fail(f"Should not validate the location {loc}")
except ValidationError as e:
assert expected == e.to_dict()["loc"]

View File

@@ -135,11 +135,11 @@ class TestMapField(MongoDBTestCase):
BlogPost.drop_collection()
tree = BlogPost(info_dict={u"éééé": {"description": u"VALUE: éééé"}})
tree = BlogPost(info_dict={"éééé": {"description": "VALUE: éééé"}})
tree.save()
assert (
BlogPost.objects.get(id=tree.id).info_dict[u"éééé"].description
== u"VALUE: éééé"
BlogPost.objects.get(id=tree.id).info_dict["éééé"].description
== "VALUE: éééé"
)

View File

@@ -87,7 +87,7 @@ class TestReferenceField(MongoDBTestCase):
parent = ReferenceField("self", dbref=False)
p = Person(name="Steve", parent=DBRef("person", "abcdefghijklmnop"))
assert p.to_mongo() == SON([("name", u"Steve"), ("parent", "abcdefghijklmnop")])
assert p.to_mongo() == SON([("name", "Steve"), ("parent", "abcdefghijklmnop")])
def test_objectid_reference_fields(self):
class Person(Document):

View File

@@ -26,7 +26,7 @@ class TestURLField(MongoDBTestCase):
url = URLField()
link = Link()
link.url = u"http://привет.com"
link.url = "http://привет.com"
# TODO fix URL validation - this *IS* a valid URL
# For now we just want to make sure that the error message is correct
@@ -34,7 +34,7 @@ class TestURLField(MongoDBTestCase):
link.validate()
assert (
str(exc_info.value)
== u"ValidationError (Link:None) (Invalid URL: http://\u043f\u0440\u0438\u0432\u0435\u0442.com: ['url'])"
== "ValidationError (Link:None) (Invalid URL: http://\u043f\u0440\u0438\u0432\u0435\u0442.com: ['url'])"
)
def test_url_scheme_validation(self):