Skip to content

Commit

Permalink
Improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
k0t3n committed Aug 2, 2024
1 parent 10cd779 commit 39c3993
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 144 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ django-stubs = "^5.0.2"
[tool.mypy]
plugins = ["mypy_django_plugin.main", ]
strict = true
warn_no_return = false

[tool.django-stubs]
django_settings_module = 'theine'
Expand Down
99 changes: 44 additions & 55 deletions tests/adapters/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ def cache() -> Iterable[BaseCache]:


class TestTheineCache:
def test_settings(self, cache: BaseCache):
def test_settings(self, cache: BaseCache) -> None:
assert cache._max_entries == 1000
assert cache.default_timeout == 60

def test_unicode_keys(self, cache: BaseCache):
def test_unicode_keys(self, cache: BaseCache) -> None:
cache.set("ключ", "value")
res = cache.get("ключ")
assert res == "value"

def test_save_and_integer(self, cache: BaseCache):
def test_save_and_integer(self, cache: BaseCache) -> None:
cache.set("test_key", 2)
res = cache.get("test_key", "Foo")

assert isinstance(res, int)
assert res == 2

def test_save_string(self, cache: BaseCache):
def test_save_string(self, cache: BaseCache) -> None:
cache.set("test_key", "hello" * 1000)
res = cache.get("test_key")

Expand All @@ -45,14 +45,14 @@ def test_save_string(self, cache: BaseCache):
assert isinstance(res, str)
assert res == "2"

def test_save_unicode(self, cache: BaseCache):
def test_save_unicode(self, cache: BaseCache) -> None:
cache.set("test_key", "heló")
res = cache.get("test_key")

assert isinstance(res, str)
assert res == "heló"

def test_save_dict(self, cache: BaseCache):
def test_save_dict(self, cache: BaseCache) -> None:
now_dt = datetime.datetime.now()
test_dict = {"id": 1, "date": now_dt, "name": "Foo"}

Expand All @@ -64,7 +64,7 @@ def test_save_dict(self, cache: BaseCache):
assert res["name"] == "Foo"
assert res["date"] == now_dt

def test_save_float(self, cache: BaseCache):
def test_save_float(self, cache: BaseCache) -> None:
float_val = 1.345620002

cache.set("test_key", float_val)
Expand All @@ -73,19 +73,19 @@ def test_save_float(self, cache: BaseCache):
assert isinstance(res, float)
assert res == float_val

def test_timeout(self, cache: BaseCache):
def test_timeout(self, cache: BaseCache) -> None:
cache.set("test_key", 222, timeout=3)
time.sleep(4)

res = cache.get("test_key")
assert res is None

def test_timeout_0(self, cache: BaseCache):
def test_timeout_0(self, cache: BaseCache) -> None:
cache.set("test_key", 222, timeout=0)
res = cache.get("test_key")
assert res is None

def test_timeout_parameter_as_positional_argument(self, cache: BaseCache):
def test_timeout_parameter_as_positional_argument(self, cache: BaseCache) -> None:
cache.set("test_key", 222, -1)
res = cache.get("test_key")
assert res is None
Expand All @@ -97,7 +97,7 @@ def test_timeout_parameter_as_positional_argument(self, cache: BaseCache):
assert res1 == 222
assert res2 is None

def test_timeout_negative(self, cache: BaseCache):
def test_timeout_negative(self, cache: BaseCache) -> None:
cache.set("test_key", 222, timeout=-1)
res = cache.get("test_key")
assert res is None
Expand All @@ -107,71 +107,60 @@ def test_timeout_negative(self, cache: BaseCache):
res = cache.get("test_key")
assert res is None

def test_timeout_tiny(self, cache: BaseCache):
def test_timeout_tiny(self, cache: BaseCache) -> None:
cache.set("test_key", 222, timeout=0.00001)
res = cache.get("test_key")
assert res in (None, 222)

def test_set_add(self, cache: BaseCache):
def test_set_add(self, cache: BaseCache) -> None:
cache.set("add_key", "Initial value")
res = cache.add("add_key", "New value")
assert res is False
assert cache.add("add_key", "New value") is False

res = cache.get("add_key")
assert res == "Initial value"
res = cache.add("other_key", "New value")
assert res is True
assert cache.get("add_key") == "Initial value"
assert cache.add("other_key", "New value") is True

def test_get_many(self, cache: BaseCache):
def test_get_many(self, cache: BaseCache) -> None:
cache.set("a", 1)
cache.set("b", 2)
cache.set("c", 3)

res = cache.get_many(["a", "b", "c"])
assert res == {"a": 1, "b": 2, "c": 3}

def test_get_many_unicode(self, cache: BaseCache):
def test_get_many_unicode(self, cache: BaseCache) -> None:
cache.set("a", "1")
cache.set("b", "2")
cache.set("c", "3")

res = cache.get_many(["a", "b", "c"])
assert res == {"a": "1", "b": "2", "c": "3"}

def test_set_many(self, cache: BaseCache):
def test_set_many(self, cache: BaseCache) -> None:
cache.set_many({"a": 1, "b": 2, "c": 3})
res = cache.get_many(["a", "b", "c"])
assert res == {"a": 1, "b": 2, "c": 3}

def test_delete(self, cache: BaseCache):
def test_delete(self, cache: BaseCache) -> None:
cache.set_many({"a": 1, "b": 2, "c": 3})
res = cache.delete("a")
assert bool(res) is True

res = cache.get_many(["a", "b", "c"])
assert res == {"b": 2, "c": 3}
assert cache.delete("a") is True
assert cache.get_many(["a", "b", "c"]) == {"b": 2, "c": 3}
assert cache.delete("a") is False

res = cache.delete("a")
assert bool(res) is False

def test_delete_many(self, cache: BaseCache):
def test_delete_many(self, cache: BaseCache) -> None:
cache.set_many({"a": 1, "b": 2, "c": 3})
res = cache.delete_many(["a", "b"])
res = cache.get_many(["a", "b", "c"])
assert res == {"c": 3}
cache.delete_many(["a", "b"])
assert cache.get_many(["a", "b", "c"]) == {"c": 3}

def test_delete_many_generator(self, cache: BaseCache):
def test_delete_many_generator(self, cache: BaseCache) -> None:
cache.set_many({"a": 1, "b": 2, "c": 3})
res = cache.delete_many(key for key in ["a", "b"])
cache.delete_many(key for key in ["a", "b"])
res = cache.get_many(["a", "b", "c"])
assert res == {"c": 3}

def test_delete_many_empty_generator(self, cache: BaseCache):
res = cache.delete_many(key for key in cast(List[str], []))
assert bool(res) is False

def test_incr(self, cache: BaseCache):
def test_delete_many_empty_generator(self, cache: BaseCache) -> None:
cache.delete_many(key for key in cast(List[str], []))

def test_incr(self, cache: BaseCache) -> None:
cache.set("num", 1)
cache.incr("num")
res = cache.get("num")
Expand All @@ -198,7 +187,7 @@ def test_incr(self, cache: BaseCache):
res = cache.get("num")
assert res == 5

def test_incr_no_timeout(self, cache: BaseCache):
def test_incr_no_timeout(self, cache: BaseCache) -> None:
cache.set("num", 1, timeout=None)

cache.incr("num")
Expand Down Expand Up @@ -226,7 +215,7 @@ def test_incr_no_timeout(self, cache: BaseCache):
res = cache.get("num")
assert res == 5

def test_get_set_bool(self, cache: BaseCache):
def test_get_set_bool(self, cache: BaseCache) -> None:
cache.set("bool", True)
res = cache.get("bool")

Expand All @@ -239,15 +228,15 @@ def test_get_set_bool(self, cache: BaseCache):
assert isinstance(res, bool)
assert res is False

def test_version(self, cache: BaseCache):
def test_version(self, cache: BaseCache) -> None:
cache.set("keytest", 2, version=2)
res = cache.get("keytest")
assert res is None

res = cache.get("keytest", version=2)
assert res == 2

def test_incr_version(self, cache: BaseCache):
def test_incr_version(self, cache: BaseCache) -> None:
cache.set("keytest", 2)
cache.incr_version("keytest")

Expand All @@ -257,7 +246,7 @@ def test_incr_version(self, cache: BaseCache):
res = cache.get("keytest", version=2)
assert res == 2

def test_ttl_incr_version_no_timeout(self, cache: BaseCache):
def test_ttl_incr_version_no_timeout(self, cache: BaseCache) -> None:
cache.set("my_key", "hello world!", timeout=None)

cache.incr_version("my_key")
Expand All @@ -266,50 +255,50 @@ def test_ttl_incr_version_no_timeout(self, cache: BaseCache):

assert my_value == "hello world!"

def test_touch_zero_timeout(self, cache: BaseCache):
def test_touch_zero_timeout(self, cache: BaseCache) -> None:
cache.set("test_key", 222, timeout=10)

assert cache.touch("test_key", 0) is True
res = cache.get("test_key")
assert res is None

def test_touch_positive_timeout(self, cache: BaseCache):
def test_touch_positive_timeout(self, cache: BaseCache) -> None:
cache.set("test_key", 222, timeout=10)

assert cache.touch("test_key", 2) is True
assert cache.get("test_key") == 222
time.sleep(3)
assert cache.get("test_key") is None

def test_touch_negative_timeout(self, cache: BaseCache):
def test_touch_negative_timeout(self, cache: BaseCache) -> None:
cache.set("test_key", 222, timeout=10)

assert cache.touch("test_key", -1) is True
res = cache.get("test_key")
assert res is None

def test_touch_missed_key(self, cache: BaseCache):
def test_touch_missed_key(self, cache: BaseCache) -> None:
assert cache.touch("test_key_does_not_exist", 1) is False

def test_touch_forever(self, cache: Theine):
def test_touch_forever(self, cache: Theine) -> None:
cache.set("test_key", "foo", timeout=1)
result = cache.touch("test_key", None)
assert result is True
time.sleep(2)
assert cache.get("test_key") == "foo"

def test_touch_forever_nonexistent(self, cache: BaseCache):
def test_touch_forever_nonexistent(self, cache: BaseCache) -> None:
result = cache.touch("test_key_does_not_exist", None)
assert result is False

def test_touch_default_timeout(self, cache: BaseCache):
def test_touch_default_timeout(self, cache: BaseCache) -> None:
cache.set("test_key", "foo", timeout=1)
result = cache.touch("test_key")
assert result is True
time.sleep(2)
assert cache.get("test_key") == "foo"

def test_clear(self, cache: BaseCache):
def test_clear(self, cache: BaseCache) -> None:
cache.set("foo", "bar")
value_from_cache = cache.get("foo")
assert value_from_cache == "bar"
Expand Down
28 changes: 15 additions & 13 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from datetime import timedelta
from random import randint
from time import sleep
from bounded_zipf import Zipf
from typing import cast

import pytest
from bounded_zipf import Zipf # type: ignore[import]
from pytest_asyncio.plugin import SubRequest

from theine.theine import Cache, sentinel


@pytest.fixture(params=["lru", "tlfu", "clockpro"])
def policy(request):
return request.param
def policy(request: SubRequest) -> str:
return cast(str, request.param)


def test_set(policy):
def test_set(policy: str) -> None:
cache = Cache(policy, 100)
for i in range(20):
key = f"key:{i}"
Expand All @@ -36,15 +38,15 @@ def test_set(policy):
assert len(cache) == 100


def test_set_cache_size(policy):
def test_set_cache_size(policy: str) -> None:
cache = Cache(policy, 500)
for _ in range(100000):
i = randint(0, 100000)
cache.set(f"key:{i}", i)
assert len([i for i in cache._cache if i is not sentinel]) == 500


def test_set_with_ttl(policy):
def test_set_with_ttl(policy: str) -> None:
cache = Cache(policy, 500)
for i in range(30):
key = f"key:{i}"
Expand All @@ -67,7 +69,7 @@ def test_set_with_ttl(policy):
assert f"key:{i}:2" in data


def test_delete(policy):
def test_delete(policy: str) -> None:
cache = Cache(policy, 100)
for i in range(20):
key = f"key:{i}"
Expand All @@ -79,11 +81,11 @@ def test_delete(policy):


class Foo:
def __init__(self, id):
def __init__(self, id: int):
self.id = id


def test_hashable_key(policy):
def test_hashable_key(policy: str) -> None:
cache = Cache(policy, 100)
foos = [Foo(i) for i in range(20)]
for foo in foos:
Expand All @@ -98,7 +100,7 @@ def test_hashable_key(policy):
assert cache.key_gen.len() == 19


def test_set_with_ttl_hashable(policy):
def test_set_with_ttl_hashable(policy: str) -> None:
cache = Cache(policy, 500)
foos = [Foo(i) for i in range(30)]
for i in range(30):
Expand All @@ -117,7 +119,7 @@ def test_set_with_ttl_hashable(policy):
assert cache.key_gen.len() == 0


def test_ttl_high_workload(policy):
def test_ttl_high_workload(policy: str) -> None:
cache = Cache(policy, 500000)
for i in range(500000):
cache.set((i, 2), i, timedelta(seconds=randint(5, 10)))
Expand All @@ -132,15 +134,15 @@ def test_ttl_high_workload(policy):
assert len(cache.key_gen.hk) == 0


def test_close_cache(policy):
def test_close_cache(policy: str) -> None:
for _ in range(10):
cache = Cache(policy, 500)
cache.set("foo", "bar", timedelta(seconds=60))
cache.close()
assert cache._maintainer.is_alive() is False


def test_cache_stats(policy):
def test_cache_stats(policy: str) -> None:
cache = Cache(policy, 5000)
assert cache.max_size == 5000
assert len(cache) == 0
Expand Down
Loading

0 comments on commit 39c3993

Please sign in to comment.