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

Commit

Permalink
Replace deprecated assertDictContainsSubset
Browse files Browse the repository at this point in the history
by assertLessEqual on dict items()

Signed-off-by: Kai A. Hiller <V02460@gmail.com>
  • Loading branch information
V02460 committed Aug 10, 2023
1 parent 5316961 commit 988032f
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 71 deletions.
24 changes: 12 additions & 12 deletions tests/handlers/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,50 +123,50 @@ def test_get_devices_by_user(self) -> None:

self.assertEqual(3, len(res))
device_map = {d["device_id"]: d for d in res}
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": user1,
"device_id": "xyz",
"display_name": "display 0",
"last_seen_ip": None,
"last_seen_ts": None,
},
device_map["xyz"],
}.items(),
device_map["xyz"].items(),
)
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": user1,
"device_id": "fco",
"display_name": "display 1",
"last_seen_ip": "ip1",
"last_seen_ts": 1000000,
},
device_map["fco"],
}.items(),
device_map["fco"].items(),
)
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": user1,
"device_id": "abc",
"display_name": "display 2",
"last_seen_ip": "ip3",
"last_seen_ts": 3000000,
},
device_map["abc"],
}.items(),
device_map["abc"].items(),
)

def test_get_device(self) -> None:
self._record_users()

res = self.get_success(self.handler.get_device(user1, "abc"))
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": user1,
"device_id": "abc",
"display_name": "display 2",
"last_seen_ip": "ip3",
"last_seen_ts": 3000000,
},
res,
}.items(),
res.items(),
)

def test_delete_device(self) -> None:
Expand Down
5 changes: 3 additions & 2 deletions tests/rest/client/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,9 @@ def test_spam_checker_deny(self) -> None:
body,
)
self.assertEqual(channel.code, 403, channel.result)
self.assertDictContainsSubset(
{"errcode": Codes.LIMIT_EXCEEDED, "extra": "value"}, channel.json_body
self.assertLessEqual(
{"errcode": Codes.LIMIT_EXCEEDED, "extra": "value"}.items(),
channel.json_body.items(),
)


Expand Down
8 changes: 4 additions & 4 deletions tests/rest/client/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_POST_appservice_registration_valid(self) -> None:

self.assertEqual(channel.code, 200, msg=channel.result)
det_data = {"user_id": user_id, "home_server": self.hs.hostname}
self.assertDictContainsSubset(det_data, channel.json_body)
self.assertLessEqual(det_data.items(), channel.json_body.items())

def test_POST_appservice_registration_no_type(self) -> None:
as_token = "i_am_an_app_service"
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_POST_user_valid(self) -> None:
"device_id": device_id,
}
self.assertEqual(channel.code, 200, msg=channel.result)
self.assertDictContainsSubset(det_data, channel.json_body)
self.assertLessEqual(det_data.items(), channel.json_body.items())

@override_config({"enable_registration": False})
def test_POST_disabled_registration(self) -> None:
Expand All @@ -157,7 +157,7 @@ def test_POST_guest_registration(self) -> None:

det_data = {"home_server": self.hs.hostname, "device_id": "guest_device"}
self.assertEqual(channel.code, 200, msg=channel.result)
self.assertDictContainsSubset(det_data, channel.json_body)
self.assertLessEqual(det_data.items(), channel.json_body.items())

def test_POST_disabled_guest_registration(self) -> None:
self.hs.config.registration.allow_guest_access = False
Expand Down Expand Up @@ -267,7 +267,7 @@ def test_POST_registration_requires_token(self) -> None:
"device_id": device_id,
}
self.assertEqual(channel.code, 200, msg=channel.result)
self.assertDictContainsSubset(det_data, channel.json_body)
self.assertLessEqual(det_data.items(), channel.json_body.items())

# Check the `completed` counter has been incremented and pending is 0
res = self.get_success(
Expand Down
40 changes: 21 additions & 19 deletions tests/rest/client/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def test_edit_reply(self) -> None:
)
self.assertEqual(200, channel.code, channel.json_body)
event_result = channel.json_body
self.assertDictContainsSubset(original_body, event_result["content"])
self.assertLessEqual(original_body.items(), event_result["content"].items())

# also check /context, which returns the *edited* event
channel = self.make_request(
Expand All @@ -587,14 +587,14 @@ def test_edit_reply(self) -> None:
(context_result, "/context"),
):
# The reference metadata should still be intact.
self.assertDictContainsSubset(
self.assertLessEqual(
{
"m.relates_to": {
"event_id": self.parent_id,
"rel_type": "m.reference",
}
},
result_event_dict["content"],
}.items(),
result_event_dict["content"].items(),
desc,
)

Expand Down Expand Up @@ -1371,9 +1371,11 @@ def test_thread_edit_latest_event(self) -> None:
latest_event_in_thread = thread_summary["latest_event"]
# The latest event in the thread should have the edit appear under the
# bundled aggregations.
self.assertDictContainsSubset(
{"event_id": edit_event_id, "sender": "@alice:test"},
latest_event_in_thread["unsigned"]["m.relations"][RelationTypes.REPLACE],
self.assertLessEqual(
{"event_id": edit_event_id, "sender": "@alice:test"}.items(),
latest_event_in_thread["unsigned"]["m.relations"][
RelationTypes.REPLACE
].items(),
)

def test_aggregation_get_event_for_annotation(self) -> None:
Expand Down Expand Up @@ -1636,9 +1638,9 @@ def test_redact_relation_thread(self) -> None:
##################################################
self.assertEquals(self._get_related_events(), list(reversed(thread_replies)))
relations = self._get_bundled_aggregations()
self.assertDictContainsSubset(
{"count": 3, "current_user_participated": True},
relations[RelationTypes.THREAD],
self.assertLessEqual(
{"count": 3, "current_user_participated": True}.items(),
relations[RelationTypes.THREAD].items(),
)
# The latest event is the last sent event.
self.assertEqual(
Expand All @@ -1657,9 +1659,9 @@ def test_redact_relation_thread(self) -> None:
# The thread should still exist, but the latest event should be updated.
self.assertEquals(self._get_related_events(), list(reversed(thread_replies)))
relations = self._get_bundled_aggregations()
self.assertDictContainsSubset(
{"count": 2, "current_user_participated": True},
relations[RelationTypes.THREAD],
self.assertLessEqual(
{"count": 2, "current_user_participated": True}.items(),
relations[RelationTypes.THREAD].items(),
)
# And the latest event is the last unredacted event.
self.assertEqual(
Expand All @@ -1676,9 +1678,9 @@ def test_redact_relation_thread(self) -> None:
# Nothing should have changed (except the thread count).
self.assertEquals(self._get_related_events(), thread_replies)
relations = self._get_bundled_aggregations()
self.assertDictContainsSubset(
{"count": 1, "current_user_participated": True},
relations[RelationTypes.THREAD],
self.assertLessEqual(
{"count": 1, "current_user_participated": True}.items(),
relations[RelationTypes.THREAD].items(),
)
# And the latest event is the last unredacted event.
self.assertEqual(
Expand Down Expand Up @@ -1773,12 +1775,12 @@ def test_redact_parent_thread(self) -> None:
event_ids = self._get_related_events()
relations = self._get_bundled_aggregations()
self.assertEquals(len(event_ids), 1)
self.assertDictContainsSubset(
self.assertLessEqual(
{
"count": 1,
"current_user_participated": True,
},
relations[RelationTypes.THREAD],
}.items(),
relations[RelationTypes.THREAD].items(),
)
self.assertEqual(
relations[RelationTypes.THREAD]["latest_event"]["event_id"],
Expand Down
30 changes: 15 additions & 15 deletions tests/storage/test_client_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ def test_insert_new_client_ip(self) -> None:
)

r = result[(user_id, device_id)]
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": user_id,
"device_id": device_id,
"ip": "ip",
"user_agent": "user_agent",
"last_seen": 12345678000,
},
r,
}.items(),
r.items(),
)

def test_insert_new_client_ip_none_device_id(self) -> None:
Expand Down Expand Up @@ -529,15 +529,15 @@ def test_devices_last_seen_bg_update(self) -> None:
)

r = result[(user_id, device_id)]
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": user_id,
"device_id": device_id,
"ip": None,
"user_agent": None,
"last_seen": None,
},
r,
}.items(),
r.items(),
)

# Register the background update to run again.
Expand All @@ -564,15 +564,15 @@ def test_devices_last_seen_bg_update(self) -> None:
)

r = result[(user_id, device_id)]
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": user_id,
"device_id": device_id,
"ip": "ip",
"user_agent": "user_agent",
"last_seen": 0,
},
r,
}.items(),
r.items(),
)

def test_old_user_ips_pruned(self) -> None:
Expand Down Expand Up @@ -643,15 +643,15 @@ def test_old_user_ips_pruned(self) -> None:
)

r = result2[(user_id, device_id)]
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": user_id,
"device_id": device_id,
"ip": "ip",
"user_agent": "user_agent",
"last_seen": 0,
},
r,
}.items(),
r.items(),
)


Expand Down Expand Up @@ -715,13 +715,13 @@ def _runtest(
self.store.get_last_client_ip_by_device(self.user_id, device_id)
)
r = result[(self.user_id, device_id)]
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": self.user_id,
"device_id": device_id,
"ip": expected_ip,
"user_agent": "Mozzila pizza",
"last_seen": 123456100,
},
r,
}.items(),
r.items(),
)
18 changes: 9 additions & 9 deletions tests/storage/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ def test_store_new_device(self) -> None:

res = self.get_success(self.store.get_device("user_id", "device_id"))
assert res is not None
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": "user_id",
"device_id": "device_id",
"display_name": "display_name",
},
res,
}.items(),
res.items(),
)

def test_get_devices_by_user(self) -> None:
Expand All @@ -80,21 +80,21 @@ def test_get_devices_by_user(self) -> None:

res = self.get_success(self.store.get_devices_by_user("user_id"))
self.assertEqual(2, len(res.keys()))
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": "user_id",
"device_id": "device1",
"display_name": "display_name 1",
},
res["device1"],
}.items(),
res["device1"].items(),
)
self.assertDictContainsSubset(
self.assertLessEqual(
{
"user_id": "user_id",
"device_id": "device2",
"display_name": "display_name 2",
},
res["device2"],
}.items(),
res["device2"].items(),
)

def test_count_devices_by_users(self) -> None:
Expand Down
10 changes: 7 additions & 3 deletions tests/storage/test_end_to_end_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_key_without_device_name(self) -> None:
self.assertIn("user", res)
self.assertIn("device", res["user"])
dev = res["user"]["device"]
self.assertDictContainsSubset(json, dev)
self.assertLessEqual(json.items(), dev.items())

def test_reupload_key(self) -> None:
now = 1470174257070
Expand Down Expand Up @@ -71,8 +71,12 @@ def test_get_key_with_device_name(self) -> None:
self.assertIn("user", res)
self.assertIn("device", res["user"])
dev = res["user"]["device"]
self.assertDictContainsSubset(
{"key": "value", "unsigned": {"device_display_name": "display_name"}}, dev
self.assertLessEqual(
{
"key": "value",
"unsigned": {"device_display_name": "display_name"},
}.items(),
dev.items(),
)

def test_multiple_devices(self) -> None:
Expand Down
Loading

0 comments on commit 988032f

Please sign in to comment.