|
| 1 | +"""A counter is recognised the same way on every backend (#111). |
| 2 | +
|
| 3 | +The memory backend and the base fallback parsed any entry whose body was a |
| 4 | +number as a counter, so `increment()` on a cached response with body `42` |
| 5 | +returned 43 and overwrote it, while Redis and Memcached raised. And a counter |
| 6 | +written with `set(key, counter_entry(n))` could be incremented only on the |
| 7 | +memory backend, because the network backends stored it as a JSON document. |
| 8 | +""" |
| 9 | + |
| 10 | +from collections.abc import AsyncIterator |
| 11 | +from collections.abc import Callable |
| 12 | + |
| 13 | +import pytest |
| 14 | +import pytest_asyncio |
| 15 | + |
| 16 | +from fastapi_cachex.backends import AsyncRedisCacheBackend |
| 17 | +from fastapi_cachex.backends import MemcachedBackend |
| 18 | +from fastapi_cachex.backends.base import BaseCacheBackend |
| 19 | +from fastapi_cachex.backends.memory import MemoryBackend |
| 20 | +from fastapi_cachex.exceptions import CacheXError |
| 21 | +from fastapi_cachex.types import COUNTER_FINGERPRINT |
| 22 | +from fastapi_cachex.types import CacheEntry |
| 23 | +from fastapi_cachex.types import counter_entry |
| 24 | +from fastapi_cachex.types import counter_value |
| 25 | +from fastapi_cachex.types import parse_counter |
| 26 | +from tests.backends.test_base import DictBackend |
| 27 | +from tests.live_servers import MEMCACHED_SERVER |
| 28 | +from tests.live_servers import REDIS_HOST |
| 29 | +from tests.live_servers import REDIS_PORT |
| 30 | +from tests.live_servers import memcached_skip_reason |
| 31 | +from tests.live_servers import redis_skip_reason |
| 32 | + |
| 33 | + |
| 34 | +def _redis() -> BaseCacheBackend: |
| 35 | + return AsyncRedisCacheBackend( |
| 36 | + host=REDIS_HOST, port=REDIS_PORT, key_prefix="cachex-test-counter:" |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def _memcached() -> BaseCacheBackend: |
| 41 | + return MemcachedBackend(servers=[MEMCACHED_SERVER]) |
| 42 | + |
| 43 | + |
| 44 | +FACTORIES: dict[ |
| 45 | + str, tuple[Callable[[], BaseCacheBackend], Callable[[], str | None]] |
| 46 | +] = { |
| 47 | + "memory": (MemoryBackend, lambda: None), |
| 48 | + "fallback": (DictBackend, lambda: None), |
| 49 | + "redis": (_redis, redis_skip_reason), |
| 50 | + "memcached": (_memcached, memcached_skip_reason), |
| 51 | +} |
| 52 | +KEYS = ("counter-contract-page", "counter-contract-hits") |
| 53 | + |
| 54 | + |
| 55 | +@pytest_asyncio.fixture(params=list(FACTORIES)) |
| 56 | +async def backend(request: pytest.FixtureRequest) -> AsyncIterator[BaseCacheBackend]: |
| 57 | + factory, skip_reason = FACTORIES[request.param] |
| 58 | + reason = skip_reason() |
| 59 | + if reason is not None: |
| 60 | + pytest.skip(reason) |
| 61 | + instance = factory() |
| 62 | + await instance.delete_many(list(KEYS)) |
| 63 | + yield instance |
| 64 | + await instance.delete_many(list(KEYS)) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.asyncio |
| 68 | +async def test_increment_rejects_a_cached_response_with_a_numeric_body( |
| 69 | + backend: BaseCacheBackend, |
| 70 | +) -> None: |
| 71 | + page = CacheEntry(fingerprint="etag", content=b"42") |
| 72 | + await backend.set(KEYS[0], page) |
| 73 | + |
| 74 | + with pytest.raises(CacheXError, match="not a counter"): |
| 75 | + await backend.increment(KEYS[0]) |
| 76 | + |
| 77 | + assert await backend.get(KEYS[0]) == page |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.asyncio |
| 81 | +async def test_increment_continues_a_counter_written_with_set( |
| 82 | + backend: BaseCacheBackend, |
| 83 | +) -> None: |
| 84 | + await backend.set(KEYS[1], counter_entry(5)) |
| 85 | + |
| 86 | + assert await backend.increment(KEYS[1]) == 6 |
| 87 | + assert await backend.increment(KEYS[1], -2) == 4 # Memcached stops at 0 |
| 88 | + assert await backend.get(KEYS[1]) == counter_entry(4) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.asyncio |
| 92 | +async def test_a_counter_that_shrinks_reads_back_as_a_counter( |
| 93 | + backend: BaseCacheBackend, |
| 94 | +) -> None: |
| 95 | + """Memcached pads the value DECR shortens ("10" -> "9 ").""" |
| 96 | + await backend.increment(KEYS[1], 10) |
| 97 | + await backend.increment(KEYS[1], -1) |
| 98 | + |
| 99 | + assert await backend.get(KEYS[1]) == counter_entry(9) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize( |
| 103 | + ("raw", "expected"), |
| 104 | + [ |
| 105 | + ("7", 7), |
| 106 | + (b"-7", -7), |
| 107 | + (b"9 ", 9), |
| 108 | + (b" 7", None), |
| 109 | + (b"7\n", None), |
| 110 | + (b"1_0", None), |
| 111 | + (b"+7", None), |
| 112 | + (chr(0x0667), None), # ARABIC-INDIC DIGIT SEVEN, which int() accepts |
| 113 | + (b"", None), |
| 114 | + (b"-", None), |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_parse_counter_accepts_only_what_the_servers_write( |
| 118 | + raw: str | bytes, expected: int | None |
| 119 | +) -> None: |
| 120 | + assert parse_counter(raw) == expected |
| 121 | + |
| 122 | + |
| 123 | +@pytest.mark.parametrize( |
| 124 | + "entry", |
| 125 | + [ |
| 126 | + CacheEntry(fingerprint="etag", content=b"42"), |
| 127 | + CacheEntry(fingerprint=COUNTER_FINGERPRINT, content=b"1_0"), |
| 128 | + ], |
| 129 | +) |
| 130 | +def test_counter_value_requires_the_counter_fingerprint_and_a_plain_integer( |
| 131 | + entry: CacheEntry, |
| 132 | +) -> None: |
| 133 | + with pytest.raises(CacheXError, match="not a counter"): |
| 134 | + counter_value(entry) |
0 commit comments