Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
[pyupgrade] tests/ (#10347)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowJonathan authored Jul 13, 2021
1 parent 879d8c1 commit 89cfc3d
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 58 deletions.
1 change: 1 addition & 0 deletions changelog.d/10347.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run `pyupgrade` on the codebase.
4 changes: 2 additions & 2 deletions tests/config/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_load_fails_if_server_name_missing(self):
def test_generates_and_loads_macaroon_secret_key(self):
self.generate_config()

with open(self.file, "r") as f:
with open(self.file) as f:
raw = yaml.safe_load(f)
self.assertIn("macaroon_secret_key", raw)

Expand Down Expand Up @@ -120,7 +120,7 @@ def generate_config(self):
def generate_config_and_remove_lines_containing(self, needle):
self.generate_config()

with open(self.file, "r") as f:
with open(self.file) as f:
contents = f.readlines()
contents = [line for line in contents if needle not in line]
with open(self.file, "w") as f:
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_set_my_name(self):
)

self.assertIsNone(
(self.get_success(self.store.get_profile_displayname(self.frank.localpart)))
self.get_success(self.store.get_profile_displayname(self.frank.localpart))
)

def test_set_my_name_if_disabled(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/http/federation/test_matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def test_get(self):
self.assertEqual(response.code, 200)

# Send the body
request.write('{ "a": 1 }'.encode("ascii"))
request.write(b'{ "a": 1 }')
request.finish()

self.reactor.pump((0.1,))
Expand Down
8 changes: 3 additions & 5 deletions tests/http/test_fedclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def do_request():
self.assertNoResult(test_d)

# Send it the HTTP response
res_json = '{ "a": 1 }'.encode("ascii")
res_json = b'{ "a": 1 }'
protocol.dataReceived(
b"HTTP/1.1 200 OK\r\n"
b"Server: Fake\r\n"
Expand Down Expand Up @@ -339,10 +339,8 @@ def test_timeout_reading_body(self, method_name: str):

# Send it the HTTP response
client.dataReceived(
(
b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
b"Server: Fake\r\n\r\n"
)
b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
b"Server: Fake\r\n\r\n"
)

# Push by enough to time it out
Expand Down
6 changes: 3 additions & 3 deletions tests/replication/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,12 @@ def encode(self, obj):
if obj is None:
return "$-1\r\n"
if isinstance(obj, str):
return "${len}\r\n{str}\r\n".format(len=len(obj), str=obj)
return f"${len(obj)}\r\n{obj}\r\n"
if isinstance(obj, int):
return ":{val}\r\n".format(val=obj)
return f":{obj}\r\n"
if isinstance(obj, (list, tuple)):
items = "".join(self.encode(a) for a in obj)
return "*{len}\r\n{items}".format(len=len(obj), items=items)
return f"*{len(obj)}\r\n{items}"

raise Exception("Unrecognized type for encoding redis: %r: %r", type(obj), obj)

Expand Down
4 changes: 2 additions & 2 deletions tests/replication/test_multi_media_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _get_media_req(
self.reactor,
FakeSite(resource),
"GET",
"/{}/{}".format(target, media_id),
f"/{target}/{media_id}",
shorthand=False,
access_token=self.access_token,
await_result=False,
Expand Down Expand Up @@ -113,7 +113,7 @@ def _get_media_req(
self.assertEqual(request.method, b"GET")
self.assertEqual(
request.path,
"/_matrix/media/r0/download/{}/{}".format(target, media_id).encode("utf-8"),
f"/_matrix/media/r0/download/{target}/{media_id}".encode("utf-8"),
)
self.assertEqual(
request.requestHeaders.getRawHeaders(b"host"), [target.encode("utf-8")]
Expand Down
6 changes: 3 additions & 3 deletions tests/replication/test_sharded_event_persister.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_vector_clock_token(self):
self.reactor,
sync_hs_site,
"GET",
"/sync?since={}".format(next_batch),
f"/sync?since={next_batch}",
access_token=access_token,
)

Expand Down Expand Up @@ -241,7 +241,7 @@ def test_vector_clock_token(self):
self.reactor,
sync_hs_site,
"GET",
"/sync?since={}".format(vector_clock_token),
f"/sync?since={vector_clock_token}",
access_token=access_token,
)

Expand All @@ -266,7 +266,7 @@ def test_vector_clock_token(self):
self.reactor,
sync_hs_site,
"GET",
"/sync?since={}".format(next_batch),
f"/sync?since={next_batch}",
access_token=access_token,
)

Expand Down
6 changes: 2 additions & 4 deletions tests/rest/admin/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_delete_group(self):
# Create a new group
channel = self.make_request(
"POST",
"/create_group".encode("ascii"),
b"/create_group",
access_token=self.admin_user_tok,
content={"localpart": "test"},
)
Expand Down Expand Up @@ -129,9 +129,7 @@ def _check_group(self, group_id, expect_code):

def _get_groups_user_is_in(self, access_token):
"""Returns the list of groups the user is in (given their access token)"""
channel = self.make_request(
"GET", "/joined_groups".encode("ascii"), access_token=access_token
)
channel = self.make_request("GET", b"/joined_groups", access_token=access_token)

self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])

Expand Down
20 changes: 10 additions & 10 deletions tests/rest/admin/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def _is_purged(self, room_id):
)
)

self.assertEqual(count, 0, msg="Rows not purged in {}".format(table))
self.assertEqual(count, 0, msg=f"Rows not purged in {table}")

def _assert_peek(self, room_id, expect_code):
"""Assert that the admin user can (or cannot) peek into the room."""
Expand Down Expand Up @@ -599,7 +599,7 @@ def test_purge_room(self):
)
)

self.assertEqual(count, 0, msg="Rows not purged in {}".format(table))
self.assertEqual(count, 0, msg=f"Rows not purged in {table}")


class RoomTestCase(unittest.HomeserverTestCase):
Expand Down Expand Up @@ -1280,7 +1280,7 @@ def prepare(self, reactor, clock, homeserver):
self.public_room_id = self.helper.create_room_as(
self.creator, tok=self.creator_tok, is_public=True
)
self.url = "/_synapse/admin/v1/join/{}".format(self.public_room_id)
self.url = f"/_synapse/admin/v1/join/{self.public_room_id}"

def test_requester_is_no_admin(self):
"""
Expand Down Expand Up @@ -1420,7 +1420,7 @@ def test_join_private_room_if_not_member(self):
private_room_id = self.helper.create_room_as(
self.creator, tok=self.creator_tok, is_public=False
)
url = "/_synapse/admin/v1/join/{}".format(private_room_id)
url = f"/_synapse/admin/v1/join/{private_room_id}"
body = json.dumps({"user_id": self.second_user_id})

channel = self.make_request(
Expand Down Expand Up @@ -1463,7 +1463,7 @@ def test_join_private_room_if_member(self):

# Join user to room.

url = "/_synapse/admin/v1/join/{}".format(private_room_id)
url = f"/_synapse/admin/v1/join/{private_room_id}"
body = json.dumps({"user_id": self.second_user_id})

channel = self.make_request(
Expand Down Expand Up @@ -1493,7 +1493,7 @@ def test_join_private_room_if_owner(self):
private_room_id = self.helper.create_room_as(
self.admin_user, tok=self.admin_user_tok, is_public=False
)
url = "/_synapse/admin/v1/join/{}".format(private_room_id)
url = f"/_synapse/admin/v1/join/{private_room_id}"
body = json.dumps({"user_id": self.second_user_id})

channel = self.make_request(
Expand Down Expand Up @@ -1633,7 +1633,7 @@ def test_public_room(self):

channel = self.make_request(
"POST",
"/_synapse/admin/v1/rooms/{}/make_room_admin".format(room_id),
f"/_synapse/admin/v1/rooms/{room_id}/make_room_admin",
content={},
access_token=self.admin_user_tok,
)
Expand All @@ -1660,7 +1660,7 @@ def test_private_room(self):

channel = self.make_request(
"POST",
"/_synapse/admin/v1/rooms/{}/make_room_admin".format(room_id),
f"/_synapse/admin/v1/rooms/{room_id}/make_room_admin",
content={},
access_token=self.admin_user_tok,
)
Expand All @@ -1686,7 +1686,7 @@ def test_other_user(self):

channel = self.make_request(
"POST",
"/_synapse/admin/v1/rooms/{}/make_room_admin".format(room_id),
f"/_synapse/admin/v1/rooms/{room_id}/make_room_admin",
content={"user_id": self.second_user_id},
access_token=self.admin_user_tok,
)
Expand Down Expand Up @@ -1720,7 +1720,7 @@ def test_not_enough_power(self):

channel = self.make_request(
"POST",
"/_synapse/admin/v1/rooms/{}/make_room_admin".format(room_id),
f"/_synapse/admin/v1/rooms/{room_id}/make_room_admin",
content={},
access_token=self.admin_user_tok,
)
Expand Down
14 changes: 7 additions & 7 deletions tests/rest/client/v1/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ def test_join_reason(self):
reason = "hello"
channel = self.make_request(
"POST",
"/_matrix/client/r0/rooms/{}/join".format(self.room_id),
f"/_matrix/client/r0/rooms/{self.room_id}/join",
content={"reason": reason},
access_token=self.second_tok,
)
Expand All @@ -1220,7 +1220,7 @@ def test_leave_reason(self):
reason = "hello"
channel = self.make_request(
"POST",
"/_matrix/client/r0/rooms/{}/leave".format(self.room_id),
f"/_matrix/client/r0/rooms/{self.room_id}/leave",
content={"reason": reason},
access_token=self.second_tok,
)
Expand All @@ -1234,7 +1234,7 @@ def test_kick_reason(self):
reason = "hello"
channel = self.make_request(
"POST",
"/_matrix/client/r0/rooms/{}/kick".format(self.room_id),
f"/_matrix/client/r0/rooms/{self.room_id}/kick",
content={"reason": reason, "user_id": self.second_user_id},
access_token=self.second_tok,
)
Expand All @@ -1248,7 +1248,7 @@ def test_ban_reason(self):
reason = "hello"
channel = self.make_request(
"POST",
"/_matrix/client/r0/rooms/{}/ban".format(self.room_id),
f"/_matrix/client/r0/rooms/{self.room_id}/ban",
content={"reason": reason, "user_id": self.second_user_id},
access_token=self.creator_tok,
)
Expand All @@ -1260,7 +1260,7 @@ def test_unban_reason(self):
reason = "hello"
channel = self.make_request(
"POST",
"/_matrix/client/r0/rooms/{}/unban".format(self.room_id),
f"/_matrix/client/r0/rooms/{self.room_id}/unban",
content={"reason": reason, "user_id": self.second_user_id},
access_token=self.creator_tok,
)
Expand All @@ -1272,7 +1272,7 @@ def test_invite_reason(self):
reason = "hello"
channel = self.make_request(
"POST",
"/_matrix/client/r0/rooms/{}/invite".format(self.room_id),
f"/_matrix/client/r0/rooms/{self.room_id}/invite",
content={"reason": reason, "user_id": self.second_user_id},
access_token=self.creator_tok,
)
Expand All @@ -1291,7 +1291,7 @@ def test_reject_invite_reason(self):
reason = "hello"
channel = self.make_request(
"POST",
"/_matrix/client/r0/rooms/{}/leave".format(self.room_id),
f"/_matrix/client/r0/rooms/{self.room_id}/leave",
content={"reason": reason},
access_token=self.second_tok,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/client/v2_alpha/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def test_aggregation_pagination_within_group(self):

prev_token = None
found_event_ids = []
encoded_key = urllib.parse.quote_plus("👍".encode("utf-8"))
encoded_key = urllib.parse.quote_plus("👍".encode())
for _ in range(20):
from_token = ""
if prev_token:
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/client/v2_alpha/test_report_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def prepare(self, reactor, clock, hs):
self.helper.join(self.room_id, user=self.admin_user, tok=self.admin_user_tok)
resp = self.helper.send(self.room_id, tok=self.admin_user_tok)
self.event_id = resp["event_id"]
self.report_path = "rooms/{}/report/{}".format(self.room_id, self.event_id)
self.report_path = f"rooms/{self.room_id}/report/{self.event_id}"

def test_reason_str_and_score_int(self):
data = {"reason": "this makes me sad", "score": -100}
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/media/v1/test_media_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def test_disposition_filenamestar_utf8escaped(self):
correctly decode it as the UTF-8 string, and use filename* in the
response.
"""
filename = parse.quote("\u2603".encode("utf8")).encode("ascii")
filename = parse.quote("\u2603".encode()).encode("ascii")
channel = self._req(
b"inline; filename*=utf-8''" + filename + self.test_image.extension
)
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ def test_delete_alias(self):
self.assertEqual(self.room.to_string(), room_id)

self.assertIsNone(
(self.get_success(self.store.get_association_from_room_alias(self.alias)))
self.get_success(self.store.get_association_from_room_alias(self.alias))
)
12 changes: 2 additions & 10 deletions tests/storage/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ def test_displayname(self):
)

self.assertIsNone(
(
self.get_success(
self.store.get_profile_displayname(self.u_frank.localpart)
)
)
self.get_success(self.store.get_profile_displayname(self.u_frank.localpart))
)

def test_avatar_url(self):
Expand All @@ -76,9 +72,5 @@ def test_avatar_url(self):
)

self.assertIsNone(
(
self.get_success(
self.store.get_profile_avatar_url(self.u_frank.localpart)
)
)
self.get_success(self.store.get_profile_avatar_url(self.u_frank.localpart))
)
2 changes: 1 addition & 1 deletion tests/storage/test_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_purge_history_wont_delete_extrems(self):
token = self.get_success(
self.store.get_topological_token_for_event(last["event_id"])
)
event = "t{}-{}".format(token.topological + 1, token.stream + 1)
event = f"t{token.topological + 1}-{token.stream + 1}"

# Purge everything before this topological token
f = self.get_failure(
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_get_room(self):
)

def test_get_room_unknown_room(self):
self.assertIsNone((self.get_success(self.store.get_room("!uknown:test"))))
self.assertIsNone(self.get_success(self.store.get_room("!uknown:test")))

def test_get_room_with_stats(self):
self.assertDictContainsSubset(
Expand Down
4 changes: 1 addition & 3 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,4 @@ def testLeadingUnderscore(self):
def testNonAscii(self):
# this should work with either a unicode or a bytes
self.assertEqual(map_username_to_mxid_localpart("têst"), "t=c3=aast")
self.assertEqual(
map_username_to_mxid_localpart("têst".encode("utf-8")), "t=c3=aast"
)
self.assertEqual(map_username_to_mxid_localpart("têst".encode()), "t=c3=aast")
2 changes: 1 addition & 1 deletion tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def assertObjectHasAttributes(self, attrs, obj):
try:
self.assertEquals(attrs[key], getattr(obj, key))
except AssertionError as e:
raise (type(e))("Assert error for '.{}':".format(key)) from e
raise (type(e))(f"Assert error for '.{key}':") from e

def assert_dict(self, required, actual):
"""Does a partial assert of a dict.
Expand Down

0 comments on commit 89cfc3d

Please sign in to comment.