Skip to content

Commit

Permalink
tests: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leynier committed Jan 26, 2025
1 parent 4251bc0 commit 9ce9241
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 67 deletions.
1 change: 1 addition & 0 deletions fastapi_cache_plus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
async def close_caches() -> None:
for cache in caches.all():
await cache.close()
caches.flush()
16 changes: 8 additions & 8 deletions fastapi_cache_plus/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@

class BaseCacheBackend(Generic[KT, VT]):
async def add(self, key: KT, value: VT, **kwargs) -> bool:
raise NotImplementedError
raise NotImplementedError # pragma: no cover

async def get(self, key: KT, default: VT = None, **kwargs) -> VT:
raise NotImplementedError
raise NotImplementedError # pragma: no cover

async def set(self, key: KT, value: VT, **kwargs) -> bool:
raise NotImplementedError
raise NotImplementedError # pragma: no cover

async def expire(self, key: KT, ttl: int) -> bool:
raise NotImplementedError
raise NotImplementedError # pragma: no cover

async def exists(self, *keys: KT) -> bool:
raise NotImplementedError
raise NotImplementedError # pragma: no cover

async def delete(self, key: KT) -> bool:
raise NotImplementedError
raise NotImplementedError # pragma: no cover

async def flush(self) -> None:
raise NotImplementedError
raise NotImplementedError # pragma: no cover

async def close(self) -> None:
raise NotImplementedError
raise NotImplementedError # pragma: no cover
43 changes: 24 additions & 19 deletions tests/memory_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def f_backend() -> InMemoryCacheBackend:
@pytest.mark.asyncio
async def test_should_add_n_get_data(f_backend: InMemoryCacheBackend) -> None:
is_added = await f_backend.add(TEST_KEY, TEST_VALUE)

assert is_added is True
assert await f_backend.get(TEST_KEY) == TEST_VALUE

Expand All @@ -27,7 +26,6 @@ async def test_add_should_return_false_if_key_exists(
) -> None:
await f_backend.add(TEST_KEY, TEST_VALUE)
is_added = await f_backend.add(TEST_KEY, TEST_VALUE)

assert is_added is False


Expand All @@ -37,29 +35,23 @@ async def test_should_return_default_if_key_not_exists(
) -> None:
default = "3.14159"
fetched_value = await f_backend.get("not_exists", default)

assert fetched_value == default


@pytest.mark.asyncio
async def test_set_should_rewrite_value(f_backend: InMemoryCacheBackend) -> None:
eulers_number = "2.71828"

await f_backend.add(TEST_KEY, TEST_VALUE)
await f_backend.set(TEST_KEY, eulers_number)

fetched_value = await f_backend.get(TEST_KEY)

assert fetched_value == eulers_number


@pytest.mark.asyncio
async def test_delete_should_remove_from_cache(f_backend: InMemoryCacheBackend) -> None:
await f_backend.add(TEST_KEY, TEST_VALUE)
await f_backend.delete(TEST_KEY)

fetched_value = await f_backend.get(TEST_KEY)

assert fetched_value is None


Expand All @@ -69,9 +61,7 @@ async def test_flush_should_remove_all_objects_from_cache(
) -> None:
await f_backend.add("pi", "3.14159")
await f_backend.add("golden_ratio", "1.61803")

await f_backend.flush()

assert await f_backend.get("pi") is None
assert await f_backend.get("golden_ratio") is None

Expand All @@ -89,7 +79,6 @@ async def test_should_set_value_with_ttl(
) -> None:
await f_backend.set(key, value, ttl=ttl)
fetched_value = await f_backend.get(key)

assert fetched_value == expected


Expand All @@ -102,11 +91,14 @@ async def test_should_set_value_with_ttl(
],
)
async def test_should_add_value_with_ttl(
key: Hashable, value: Any, ttl: int, expected: Any, f_backend: InMemoryCacheBackend
key: Hashable,
value: Any,
ttl: int,
expected: Any,
f_backend: InMemoryCacheBackend,
) -> None:
await f_backend.add(key, value, ttl=ttl)
fetched_value = await f_backend.get(key)

assert fetched_value == expected


Expand All @@ -118,11 +110,11 @@ async def test_should_add_value_with_ttl(
],
)
async def test_key_should_check_for_exists(
keys: Tuple[Hashable], f_backend: InMemoryCacheBackend
keys: Tuple[Hashable],
f_backend: InMemoryCacheBackend,
) -> None:
for key in keys:
await f_backend.set(key, key)

assert await f_backend.exists(*keys) is True


Expand All @@ -135,11 +127,13 @@ async def test_key_should_check_for_exists(
],
)
async def test_key_should_check_for_exists_with_ttl(
keys: Tuple[Hashable], ttl: int, exists: bool, f_backend: InMemoryCacheBackend
keys: Tuple[Hashable],
ttl: int,
exists: bool,
f_backend: InMemoryCacheBackend,
) -> None:
for key in keys:
await f_backend.set(key, key, ttl=ttl)

assert await f_backend.exists(*keys) is exists


Expand All @@ -165,10 +159,21 @@ async def test_should_return_false_if_keys_not_exist(
],
)
async def test_expire_from_cache(
key: Hashable, value: Any, ttl: int, expected: Any, f_backend: InMemoryCacheBackend
key: Hashable,
value: Any,
ttl: int,
expected: Any,
f_backend: InMemoryCacheBackend,
) -> None:
await f_backend.add(key, value)
await f_backend.expire(key, ttl)
fetched_value = await f_backend.get(key)

assert fetched_value == expected


@pytest.mark.asyncio
async def test_close_should_not_raise_exception(
f_backend: InMemoryCacheBackend,
) -> None:
await f_backend.close()
assert True
24 changes: 9 additions & 15 deletions tests/redis_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def f_backend() -> RedisCacheBackend:
@pytest.mark.asyncio
async def test_should_add_n_get_data(f_backend: RedisCacheBackend) -> None:
is_added = await f_backend.add(TEST_KEY, TEST_VALUE)

assert is_added is True
assert await f_backend.get(TEST_KEY) == TEST_VALUE

Expand All @@ -32,7 +31,6 @@ async def test_should_add_n_get_data_no_encoding(f_backend: RedisCacheBackend) -
NO_ENCODING_KEY = "bytes"
NO_ENCODING_VALUE = b"test"
is_added = await f_backend.add(NO_ENCODING_KEY, NO_ENCODING_VALUE)

assert is_added is True
assert await f_backend.get(NO_ENCODING_KEY, encoding=None) == bytes(
NO_ENCODING_VALUE
Expand All @@ -45,7 +43,6 @@ async def test_add_should_return_false_if_key_exists(
) -> None:
await f_backend.add(TEST_KEY, TEST_VALUE)
is_added = await f_backend.add(TEST_KEY, TEST_VALUE)

assert is_added is False


Expand All @@ -55,7 +52,6 @@ async def test_should_return_default_if_key_not_exists(
) -> None:
default = "3.14159"
fetched_value = await f_backend.get("not_exists", default)

assert fetched_value == default


Expand Down Expand Up @@ -101,29 +97,23 @@ async def test_should_check_is_several_keys_exists(
) -> None:
for key, value in preset:
await f_backend.add(key, value)

assert await f_backend.exists(*keys) == exists


@pytest.mark.asyncio
async def test_set_should_rewrite_value(f_backend: RedisCacheBackend) -> None:
eulers_number = "2.71828"

await f_backend.add(TEST_KEY, TEST_VALUE)
await f_backend.set(TEST_KEY, eulers_number)

fetched_value = await f_backend.get(TEST_KEY)

assert fetched_value == eulers_number


@pytest.mark.asyncio
async def test_delete_should_remove_from_cache(f_backend: RedisCacheBackend) -> None:
await f_backend.add(TEST_KEY, TEST_VALUE)
await f_backend.delete(TEST_KEY)

fetched_value = await f_backend.get(TEST_KEY)

assert fetched_value is None


Expand All @@ -136,12 +126,15 @@ async def test_delete_should_remove_from_cache(f_backend: RedisCacheBackend) ->
],
)
async def test_expire_from_cache(
key: RedisKey, value: Any, ttl: int, expected: Any, f_backend: RedisCacheBackend
key: RedisKey,
value: Any,
ttl: int,
expected: Any,
f_backend: RedisCacheBackend,
) -> None:
await f_backend.add(key, value)
await f_backend.expire(key, ttl)
fetched_value = await f_backend.get(key)

assert fetched_value == expected


Expand All @@ -151,9 +144,7 @@ async def test_flush_should_remove_all_objects_from_cache(
) -> None:
await f_backend.add("pi", "3.14159")
await f_backend.add("golden_ratio", "1.61803")

await f_backend.flush()

assert await f_backend.get("pi") is None
assert await f_backend.get("golden_ratio") is None

Expand All @@ -179,7 +170,10 @@ async def test_close_should_close_connection(f_backend: RedisCacheBackend) -> No
],
)
async def test_scalar_types(
key: RedisKey, value: Any, expected: Any, f_backend: RedisCacheBackend
key: RedisKey,
value: Any,
expected: Any,
f_backend: RedisCacheBackend,
) -> None:
await f_backend.set(key, value)
assert await f_backend.get(key) == expected
20 changes: 12 additions & 8 deletions tests/registry_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from fastapi_cache_plus import close_caches
from fastapi_cache_plus.backends.memory import CACHE_KEY, BaseCacheBackend
from fastapi_cache_plus.registry import CacheRegistry

Expand All @@ -24,18 +25,15 @@ def test_get_from_registry_should_return_cache_instance(
) -> None:
cache = BaseCacheBackend()
cache_registry.set(CACHE_KEY, cache)

assert cache_registry.get(CACHE_KEY) == cache


def test_retrieve_all_registered_caches_from_registry(
cache_registry: CacheRegistry,
) -> None:
cache = BaseCacheBackend()

cache_registry.set(CACHE_KEY, cache)
cache_registry.set("OTHER_CACHE_KEY", cache)

assert cache_registry.all() == (cache, cache)


Expand All @@ -44,7 +42,6 @@ def test_registry_should_raise_error_on_dublicate_cache_key(
) -> None:
cache = BaseCacheBackend()
cache_registry.set(CACHE_KEY, cache)

with pytest.raises(NameError, match="Cache with the same name already registered"):
cache_registry.set(CACHE_KEY, cache)

Expand All @@ -53,7 +50,6 @@ def test_remove_cache_from_registry(cache_registry: CacheRegistry) -> None:
cache = BaseCacheBackend()
cache_registry.set(CACHE_KEY, cache)
cache_registry.remove(CACHE_KEY)

assert cache_registry.get(CACHE_KEY) is None


Expand All @@ -68,16 +64,24 @@ def test_flush_should_remove_all_registered_cashes(
cache_registry: CacheRegistry,
) -> None:
cache = BaseCacheBackend()

cache_registry.set(CACHE_KEY, cache)
cache_registry.set("OTHER_CACHE_KEY", cache)

cache_registry.flush()

assert cache_registry.get(CACHE_KEY) is None
assert cache_registry.get("OTHER_CACHE_KEY") is None


@pytest.mark.asyncio
async def test_close_caches_should_not_raise_exception(
cache_registry: CacheRegistry,
) -> None:
cache = BaseCacheBackend()
cache_registry.set(CACHE_KEY, cache)
cache_registry.set("OTHER_CACHE_KEY", cache)
await close_caches()
assert cache_registry.get(CACHE_KEY) is None


@pytest.mark.backwards
def test_registry_can_be_imported_by_older_path() -> None:
import importlib
Expand Down
Loading

0 comments on commit 9ce9241

Please sign in to comment.