def test___init___raises_value_error_on_invalid_expiry(self): with pytest.raises( ValueError, match="expiry time must be greater than 0 microseconds."): collections.TimedCacheMap(expiry=datetime.timedelta(seconds=0)) with pytest.raises( ValueError, match="expiry time must be greater than 0 microseconds."): collections.TimedCacheMap(expiry=datetime.timedelta(seconds=-50))
def test___getitem___for_unknown_entry(self): mock_map = collections.TimedCacheMap(expiry=datetime.timedelta( seconds=100)) mock_map["blam"] = 8 with pytest.raises(KeyError): mock_map["OK"]
def test_clear(self): raw_map = { "floom": (999999999999999999999999, "buebue"), "bash": (999999999999999999999999, "bunny_time"), } mock_map = collections.TimedCacheMap(raw_map, expiry=datetime.timedelta(seconds=4523412)) mock_map.clear() assert mock_map._data == {}
def test___init___with_source(self): raw_map = { "not_in": (time.perf_counter() - 50, "goodbye"), "ok": (time.perf_counter() + 30, "no"), "blam": (time.perf_counter() + 20, "bye"), } mock_map = collections.TimedCacheMap(raw_map, expiry=datetime.timedelta(seconds=42)) assert mock_map == {"blam": "bye", "ok": "no"}
def test___setitem___removes_old_entry_instead_of_replacing(self): mock_map = collections.TimedCacheMap( { "ok": (time.perf_counter() + 50, "no"), "bar": (time.perf_counter() + 60, "bat"), "foo": (time.perf_counter() + 70, "blam"), }, expiry=datetime.timedelta(seconds=100), ) mock_map["ok"] = "foo" assert list(mock_map.items())[2] == ("ok", "foo")
async def test___delitem___garbage_collection_on_expire_set(self): expire_call = mock.Mock() mock_map = collections.TimedCacheMap( expiry=datetime.timedelta(seconds=hikari_test_helpers.REASONABLE_QUICK_RESPONSE_TIME * 3), on_expire=expire_call, ) mock_map.update({"nyaa": "see", "awwo": "awoo2"}) await asyncio.sleep(hikari_test_helpers.REASONABLE_QUICK_RESPONSE_TIME * 6) expire_call.assert_not_called() mock_map.update({"ayanami": "shinji", "rei": "aww"}) expire_call.assert_has_calls((mock.call("see"), mock.call("awoo2")))
def test_freeze(self): raw_map = { "bash": (999999999999999999999999, "gtuutueu"), "blam": (999999999999999999999999, "poke"), "owowo": (999999999999999999999999, "no you"), } mock_map = collections.TimedCacheMap(raw_map, expiry=datetime.timedelta(seconds=6523423)) result = mock_map.freeze() assert result == {"bash": "gtuutueu", "blam": "poke", "owowo": "no you"} assert isinstance(result, collections._FrozenDict)
def test___len__(self): mock_map = collections.TimedCacheMap(expiry=datetime.timedelta( seconds=100)) mock_map.update({ "o": "k", "boop": "bop", "k": "o", "awoo": "blam", "rei": "cute", "hikari": "rei" }) assert len(mock_map) == 6
async def test___setitem___garbage_collection(self): mock_map = collections.TimedCacheMap( expiry=datetime.timedelta(seconds=hikari_test_helpers.REASONABLE_QUICK_RESPONSE_TIME * 3) ) mock_map["OK"] = "no" await asyncio.sleep(hikari_test_helpers.REASONABLE_QUICK_RESPONSE_TIME * 2) assert mock_map == {"OK": "no"} mock_map["ayanami"] = "rei" assert mock_map == {"OK": "no", "ayanami": "rei"} await asyncio.sleep(hikari_test_helpers.REASONABLE_QUICK_RESPONSE_TIME * 2) mock_map["nyaa"] = "qt" assert mock_map == {"ayanami": "rei", "nyaa": "qt"}
async def test___delitem___garbage_collection(self): mock_map = collections.TimedCacheMap( expiry=datetime.timedelta(seconds=hikari_test_helpers.REASONABLE_QUICK_RESPONSE_TIME * 3) ) mock_map.update({"nyaa": "see", "awwo": "awoo2"}) await asyncio.sleep(hikari_test_helpers.REASONABLE_QUICK_RESPONSE_TIME * 2) assert mock_map == {"nyaa": "see", "awwo": "awoo2"} mock_map.update({"ayanami": "shinji", "rei": "aww"}) assert mock_map == {"nyaa": "see", "awwo": "awoo2", "ayanami": "shinji", "rei": "aww"} await asyncio.sleep(hikari_test_helpers.REASONABLE_QUICK_RESPONSE_TIME * 2) del mock_map["ayanami"] assert mock_map == {"rei": "aww"}
def test_copy(self): raw_map = { "floom": (999999999999999999999999, "buebue"), "bash": (999999999999999999999999, "bunny_time"), } mock_map = collections.TimedCacheMap(raw_map, expiry=datetime.timedelta(seconds=4523412)) result = mock_map.copy() assert result is not mock_map assert isinstance(result, collections.TimedCacheMap) assert result == {"floom": "buebue", "bash": "bunny_time"}
def test_copy(self): raw_map = { "floom": (999999999999999999999999, "buebue"), "bash": (999999999999999999999999, "bunny_time"), } mock_map = collections.TimedCacheMap( raw_map, expiry=datetime.timedelta(seconds=4523412)) result = mock_map.copy() assert result is not mock_map assert isinstance(result, collections.TimedCacheMap) # 158 mediation? # TODO: fix this properly so the cast isn't needed, if we can find out what went wrong? assert dict(result) == { "floom": "buebue", "bash": "bunny_time" }, "got: " + repr(dict(result))
def test___setitem__(self): mock_map = collections.TimedCacheMap(expiry=datetime.timedelta( seconds=100)) mock_map["blat"] = 42 assert mock_map == {"blat": 42}
def test___iter__(self): mock_map = collections.TimedCacheMap(expiry=datetime.timedelta( seconds=100)) mock_map.update({"o": "k", "k": "o", "awoo": "blam", "hikari": "rei"}) assert list(mock_map) == ["o", "k", "awoo", "hikari"]
def test___getitem___for_valid_entry(self): mock_map = collections.TimedCacheMap(expiry=datetime.timedelta( seconds=100)) mock_map["OK"] = 42 mock_map["blam"] = 8 assert mock_map["OK"] == 42
def test___delitem__(self): mock_map = collections.TimedCacheMap(expiry=datetime.timedelta( seconds=100)) mock_map.update({"ok": "no", "ayanami": "rei qt"}) del mock_map["ok"] assert mock_map == {"ayanami": "rei qt"}