def test_redis_cache_compat() -> None: redis = Redis(db=6) version = 5 prefix = "test" cache_backend = CacheKVStorage( CommonRedisCache(redis, version=version, prefix=prefix)) redis_backend = KVStorageCodecWrapper( CacheKeyWrapper(RedisKVStorage(redis), version=version, prefix=prefix), JSONCodec() | BytesCodec(), ) key = "key" value = [1, 2, 3] cache_backend.set(key, value) assert cache_backend.get(key) == value assert redis_backend.get(key) == value value = [4, 5, 6] redis_backend.set("key", value) assert cache_backend.get(key) == value assert redis_backend.get(key) == value cache_backend.delete("key") assert cache_backend.get("key") is None assert redis_backend.get("key") is None
def test_encoding_wrapper(store: KVStorage[str, bytes]) -> None: wrapper = KVStorageCodecWrapper(store, JSONCodec() | BytesCodec()) wrapper.set("key", [1, 2, 3]) assert store.get("key") == b"[1,2,3]" assert wrapper.get("key") == [1, 2, 3] assert [*wrapper.get_many(["key", "missing"])] == [("key", [1, 2, 3])]
def BigtableEventProcessingStore(**options) -> EventProcessingStore: """ Creates an instance of the processing store which uses Bigtable as its backend. Keyword argument are forwarded to the ``BigtableKVStorage`` constructor. """ return EventProcessingStore( KVStorageCodecWrapper( BigtableKVStorage(**options), JSONCodec() | BytesCodec(), # maintains functional parity with cache backend ))
def test_codec_chaining() -> None: codec = JSONCodec() | BytesCodec() assert codec.encode([1, 2, 3]) == b"[1,2,3]" assert codec.decode(b"[1,2,3]") == [1, 2, 3]
import pytest from sentry.utils.codecs import BytesCodec, JSONCodec, ZlibCodec, ZstdCodec @pytest.mark.parametrize( "codec, decoded, encoded", [ (JSONCodec(), { "foo": "bar" }, '{"foo":"bar"}'), (BytesCodec("utf8"), "\N{SNOWMAN}", b"\xe2\x98\x83"), (ZlibCodec(), b"hello", b"x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15"), (ZstdCodec(), b"hello", b"(\xb5/\xfd \x05)\x00\x00hello"), ], ) def test_codec(codec, encoded, decoded): assert codec.encode(decoded) == encoded assert codec.decode(encoded) == decoded def test_codec_chaining() -> None: codec = JSONCodec() | BytesCodec() assert codec.encode([1, 2, 3]) == b"[1,2,3]" assert codec.decode(b"[1,2,3]") == [1, 2, 3]