Beispiel #1
0
    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))
Beispiel #2
0
    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"]
Beispiel #3
0
    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 == {}
Beispiel #4
0
    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"}
Beispiel #5
0
 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")
Beispiel #6
0
 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")))
Beispiel #7
0
    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)
Beispiel #8
0
 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
Beispiel #9
0
 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"}
Beispiel #10
0
 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"}
Beispiel #11
0
    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"}
Beispiel #12
0
    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))
Beispiel #13
0
 def test___setitem__(self):
     mock_map = collections.TimedCacheMap(expiry=datetime.timedelta(
         seconds=100))
     mock_map["blat"] = 42
     assert mock_map == {"blat": 42}
Beispiel #14
0
 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"]
Beispiel #15
0
 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
Beispiel #16
0
 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"}