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

Port tests/ to Python 3 #3808

Merged
merged 4 commits into from
Sep 6, 2018
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
1 change: 1 addition & 0 deletions changelog.d/3808.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/ is now ported to Python 3.
1 change: 1 addition & 0 deletions tests/api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ def test_blocking_mau(self):
def test_reserved_threepid(self):
self.hs.config.limit_usage_by_mau = True
self.hs.config.max_mau_value = 1
self.store.get_monthly_active_count = lambda: defer.succeed(2)
threepid = {'medium': 'email', 'address': '[email protected]'}
unknown_threepid = {'medium': 'email', 'address': '[email protected]'}
self.hs.config.mau_limits_reserved_threepids = [threepid]
Expand Down
4 changes: 2 additions & 2 deletions tests/app/test_frontend_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_listen_http_with_presence_enabled(self):
self.assertEqual(len(self.reactor.tcpServers), 1)
site = self.reactor.tcpServers[0][1]
self.resource = (
site.resource.children["_matrix"].children["client"].children["r0"]
site.resource.children[b"_matrix"].children[b"client"].children[b"r0"]
)

request, channel = self.make_request("PUT", "presence/a/status")
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_listen_http_with_presence_disabled(self):
self.assertEqual(len(self.reactor.tcpServers), 1)
site = self.reactor.tcpServers[0][1]
self.resource = (
site.resource.children["_matrix"].children["client"].children["r0"]
site.resource.children[b"_matrix"].children[b"client"].children[b"r0"]
)

request, channel = self.make_request("PUT", "presence/a/status")
Expand Down
4 changes: 1 addition & 3 deletions tests/handlers/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def _expect_edu_transaction(edu_type, content, origin="test"):


def _make_edu_transaction_json(edu_type, content):
return json.dumps(_expect_edu_transaction(edu_type, content)).encode(
'utf8'
)
return json.dumps(_expect_edu_transaction(edu_type, content)).encode('utf8')


class TypingNotificationsTestCase(unittest.TestCase):
Expand Down
561 changes: 257 additions & 304 deletions tests/rest/client/v1/test_rooms.py

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions tests/rest/client/v2_alpha/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ def test_sync_presence_disabled(self):
self.assertEqual(channel.code, 200)
self.assertTrue(
set(
[
"next_batch",
"rooms",
"account_data",
"to_device",
"device_lists",
]
["next_batch", "rooms", "account_data", "to_device", "device_lists"]
).issubset(set(channel.json_body.keys()))
)
2 changes: 1 addition & 1 deletion tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def requestDone(self, _self):
def getPeer(self):
# We give an address so that getClientIP returns a non null entry,
# causing us to record the MAU
return address.IPv4Address(b"TCP", "127.0.0.1", 3423)
return address.IPv4Address("TCP", "127.0.0.1", 3423)

def getHost(self):
return None
Expand Down
30 changes: 12 additions & 18 deletions tests/server_notices/test_resource_limits_server_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ def test_maybe_send_server_notice_to_user_remove_blocked_notice(self):

self._rlsn._auth.check_auth_blocking = Mock()
mock_event = Mock(
type=EventTypes.Message,
content={"msgtype": ServerNoticeMsgType},
type=EventTypes.Message, content={"msgtype": ServerNoticeMsgType}
)
self._rlsn._store.get_events = Mock(
return_value=defer.succeed({"123": mock_event})
)
self._rlsn._store.get_events = Mock(return_value=defer.succeed(
{"123": mock_event}
))

yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
# Would be better to check the content, but once == remove blocking event
Expand All @@ -99,12 +98,11 @@ def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self):
)

mock_event = Mock(
type=EventTypes.Message,
content={"msgtype": ServerNoticeMsgType},
type=EventTypes.Message, content={"msgtype": ServerNoticeMsgType}
)
self._rlsn._store.get_events = Mock(
return_value=defer.succeed({"123": mock_event})
)
self._rlsn._store.get_events = Mock(return_value=defer.succeed(
{"123": mock_event}
))
yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)

self._send_notice.assert_not_called()
Expand Down Expand Up @@ -177,13 +175,9 @@ def setUp(self):

@defer.inlineCallbacks
def test_server_notice_only_sent_once(self):
self.store.get_monthly_active_count = Mock(
return_value=1000,
)
self.store.get_monthly_active_count = Mock(return_value=1000)

self.store.user_last_seen_monthly_active = Mock(
return_value=1000,
)
self.store.user_last_seen_monthly_active = Mock(return_value=1000)

# Call the function multiple times to ensure we only send the notice once
yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
Expand All @@ -193,12 +187,12 @@ def test_server_notice_only_sent_once(self):
# Now lets get the last load of messages in the service notice room and
# check that there is only one server notice
room_id = yield self.server_notices_manager.get_notice_room_for_user(
self.user_id,
self.user_id
)

token = yield self.event_source.get_current_token()
events, _ = yield self.store.get_recent_events_for_room(
room_id, limit=100, end_token=token.room_key,
room_id, limit=100, end_token=token.room_key
)

count = 0
Expand Down
102 changes: 38 additions & 64 deletions tests/storage/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ def test_get_state_for_event(self):

# test _get_some_state_from_cache correctly filters out members with types=[]
(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache,
group, [], filtered_types=[EventTypes.Member]
self.store._state_group_cache, group, [], filtered_types=[EventTypes.Member]
)

self.assertEqual(is_all, True)
Expand All @@ -200,19 +199,20 @@ def test_get_state_for_event(self):

(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache,
group, [], filtered_types=[EventTypes.Member]
group,
[],
filtered_types=[EventTypes.Member],
)

self.assertEqual(is_all, True)
self.assertDictEqual(
{},
state_dict,
)
self.assertDictEqual({}, state_dict)

# test _get_some_state_from_cache correctly filters in members with wildcard types
(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache,
group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member]
group,
[(EventTypes.Member, None)],
filtered_types=[EventTypes.Member],
)

self.assertEqual(is_all, True)
Expand All @@ -226,7 +226,9 @@ def test_get_state_for_event(self):

(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache,
group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member]
group,
[(EventTypes.Member, None)],
filtered_types=[EventTypes.Member],
)

self.assertEqual(is_all, True)
Expand Down Expand Up @@ -264,18 +266,15 @@ def test_get_state_for_event(self):
)

self.assertEqual(is_all, True)
self.assertDictEqual(
{
(e5.type, e5.state_key): e5.event_id,
},
state_dict,
)
self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)

# test _get_some_state_from_cache correctly filters in members with specific types
# and no filtered_types
(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache,
group, [(EventTypes.Member, e5.state_key)], filtered_types=None
group,
[(EventTypes.Member, e5.state_key)],
filtered_types=None,
)

self.assertEqual(is_all, True)
Expand Down Expand Up @@ -305,39 +304,24 @@ def test_get_state_for_event(self):
key=group,
value=state_dict_ids,
# list fetched keys so it knows it's partial
fetched_keys=(
(e1.type, e1.state_key),
),
fetched_keys=((e1.type, e1.state_key),),
)

(is_all, known_absent, state_dict_ids) = self.store._state_group_cache.get(
group
)

self.assertEqual(is_all, False)
self.assertEqual(
known_absent,
set(
[
(e1.type, e1.state_key),
]
),
)
self.assertDictEqual(
state_dict_ids,
{
(e1.type, e1.state_key): e1.event_id,
},
)
self.assertEqual(known_absent, set([(e1.type, e1.state_key)]))
self.assertDictEqual(state_dict_ids, {(e1.type, e1.state_key): e1.event_id})

############################################
# test that things work with a partial cache

# test _get_some_state_from_cache correctly filters out members with types=[]
room_id = self.room.to_string()
(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache,
group, [], filtered_types=[EventTypes.Member]
self.store._state_group_cache, group, [], filtered_types=[EventTypes.Member]
)

self.assertEqual(is_all, False)
Expand All @@ -346,7 +330,9 @@ def test_get_state_for_event(self):
room_id = self.room.to_string()
(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache,
group, [], filtered_types=[EventTypes.Member]
group,
[],
filtered_types=[EventTypes.Member],
)

self.assertEqual(is_all, True)
Expand All @@ -355,20 +341,19 @@ def test_get_state_for_event(self):
# test _get_some_state_from_cache correctly filters in members wildcard types
(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache,
group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member]
group,
[(EventTypes.Member, None)],
filtered_types=[EventTypes.Member],
)

self.assertEqual(is_all, False)
self.assertDictEqual(
{
(e1.type, e1.state_key): e1.event_id,
},
state_dict,
)
self.assertDictEqual({(e1.type, e1.state_key): e1.event_id}, state_dict)

(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache,
group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member]
group,
[(EventTypes.Member, None)],
filtered_types=[EventTypes.Member],
)

self.assertEqual(is_all, True)
Expand All @@ -389,12 +374,7 @@ def test_get_state_for_event(self):
)

self.assertEqual(is_all, False)
self.assertDictEqual(
{
(e1.type, e1.state_key): e1.event_id,
},
state_dict,
)
self.assertDictEqual({(e1.type, e1.state_key): e1.event_id}, state_dict)

(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache,
Expand All @@ -404,32 +384,26 @@ def test_get_state_for_event(self):
)

self.assertEqual(is_all, True)
self.assertDictEqual(
{
(e5.type, e5.state_key): e5.event_id,
},
state_dict,
)
self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)

# test _get_some_state_from_cache correctly filters in members with specific types
# and no filtered_types
(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache,
group, [(EventTypes.Member, e5.state_key)], filtered_types=None
group,
[(EventTypes.Member, e5.state_key)],
filtered_types=None,
)

self.assertEqual(is_all, False)
self.assertDictEqual({}, state_dict)

(state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache,
group, [(EventTypes.Member, e5.state_key)], filtered_types=None
group,
[(EventTypes.Member, e5.state_key)],
filtered_types=None,
)

self.assertEqual(is_all, True)
self.assertDictEqual(
{
(e5.type, e5.state_key): e5.event_id,
},
state_dict,
)
self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
30 changes: 15 additions & 15 deletions tests/test_mau.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,33 +185,33 @@ def test_trial_users_cant_come_back(self):
self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)

def create_user(self, localpart):
request_data = json.dumps({
"username": localpart,
"password": "monkey",
"auth": {"type": LoginType.DUMMY},
})
request_data = json.dumps(
{
"username": localpart,
"password": "monkey",
"auth": {"type": LoginType.DUMMY},
}
)

request, channel = make_request(b"POST", b"/register", request_data)
request, channel = make_request("POST", "/register", request_data)
render(request, self.resource, self.reactor)

if channel.result["code"] != b"200":
if channel.code != 200:
raise HttpResponseException(
int(channel.result["code"]),
channel.result["reason"],
channel.result["body"],
channel.code, channel.result["reason"], channel.result["body"]
).to_synapse_error()

access_token = channel.json_body["access_token"]

return access_token

def do_sync_for_user(self, token):
request, channel = make_request(b"GET", b"/sync", access_token=token)
request, channel = make_request(
"GET", "/sync", access_token=token.encode('ascii')
)
render(request, self.resource, self.reactor)

if channel.result["code"] != b"200":
if channel.code != 200:
raise HttpResponseException(
int(channel.result["code"]),
channel.result["reason"],
channel.result["body"],
channel.code, channel.result["reason"], channel.result["body"]
).to_synapse_error()
2 changes: 1 addition & 1 deletion tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_branch_no_conflict(self):
graph = Graph(
nodes={
"START": DictObj(
type=EventTypes.Create, state_key="", content={}, depth=1,
type=EventTypes.Create, state_key="", content={}, depth=1
),
"A": DictObj(type=EventTypes.Message, depth=2),
"B": DictObj(type=EventTypes.Message, depth=3),
Expand Down
Loading