Exemplo n.º 1
0
    def test_should_apply_value_transformations_on_serialization(self):
        # given
        cache_entry = CacheEntry(datetime.utcfromtimestamp(1),
                                 datetime.utcfromtimestamp(2),
                                 datetime.utcfromtimestamp(3), "in")
        encode = Mock(return_value="out")
        decode = Mock(return_value="in")
        serde = JsonSerDe(string_encoding='utf-8',
                          value_to_reversible_repr=encode,
                          reversible_repr_to_value=decode)

        # when
        bytes = serde.serialize(cache_entry)

        # then
        parsed = json.loads(
            codecs.decode(bytes)
        )  # verified this way due to json/ujson slight separator differences
        self.assertEqual(
            parsed, {
                "created": cache_entry.created.timestamp(),
                "update_after": cache_entry.update_after.timestamp(),
                "expires_after": cache_entry.expires_after.timestamp(),
                "value": "out"
            })
        encode.assert_called_once_with("in")
Exemplo n.º 2
0
 def deserialize(self, data: bytes) -> CacheEntry:
     as_dict = json.loads(codecs.decode(data, self.__string_encoding))
     return CacheEntry(
         created=datetime.utcfromtimestamp(as_dict['created']),
         update_after=datetime.utcfromtimestamp(as_dict['update_after']),
         expires_after=datetime.utcfromtimestamp(as_dict['expires_after']),
         value=self.__reversible_repr_to_value(as_dict['value']),
     )
Exemplo n.º 3
0
    def test_should_unpickle_using_default_protocol(self):
        # given
        cache_entry = CacheEntry(datetime.now(), datetime.now(),
                                 datetime.now(), "value")
        serde = PickleSerDe(pickle_protocol=DEFAULT_PROTOCOL)
        bytes = pickle.dumps(cache_entry, protocol=DEFAULT_PROTOCOL)

        # when
        data = serde.deserialize(bytes)

        # then
        self.assertEqual(data, cache_entry)
Exemplo n.º 4
0
    def test_should_pickle_using_default_protocol(self):
        # given
        cache_entry = CacheEntry(datetime.now(), datetime.now(),
                                 datetime.now(), "value")
        serde = PickleSerDe(pickle_protocol=DEFAULT_PROTOCOL)

        # when
        bytes = serde.serialize(cache_entry)

        # then
        expected = pickle.dumps(cache_entry, protocol=DEFAULT_PROTOCOL)
        self.assertEqual(bytes, expected)
Exemplo n.º 5
0
    def test_e2e_integration_with_sample_serde(self):
        # given
        cache_entry = CacheEntry(datetime.now(), datetime.now(),
                                 datetime.now(), "value")
        serde = PickleSerDe(pickle_protocol=HIGHEST_PROTOCOL)  # sample serde
        wrapper = EncodingSerDe(serde, binary_encoding="base64")

        # when
        encoded_cache_entry = wrapper.serialize(cache_entry)
        data = wrapper.deserialize(encoded_cache_entry)

        # then
        self.assertEqual(data, cache_entry)
Exemplo n.º 6
0
    def test_should_decode_readable_json(self):
        # given
        serde = JsonSerDe(string_encoding='utf-8')

        # when
        bytes = serde.deserialize(
            b'{"created":1,"update_after":2,"expires_after":3,"value":"value"}'
        )

        # then
        cache_entry = CacheEntry(datetime.utcfromtimestamp(1),
                                 datetime.utcfromtimestamp(2),
                                 datetime.utcfromtimestamp(3), "value")
        self.assertEqual(bytes, cache_entry)
Exemplo n.º 7
0
    def test_should_apply_decoding_on_wrapped_serde_results(self):
        # given
        cache_entry = CacheEntry(datetime.now(), datetime.now(),
                                 datetime.now(), "value")
        encoded_cache_entry = codecs.encode(b'y', 'base64')
        serde = Mock()
        serde.deserialize.return_value = cache_entry
        wrapper = EncodingSerDe(serde, binary_encoding="base64")

        # when
        data = wrapper.deserialize(encoded_cache_entry)

        # then
        self.assertEqual(data, cache_entry)
        serde.deserialize.assert_called_once_with(b'y')
Exemplo n.º 8
0
    def test_should_apply_encoding_on_wrapped_serde_results(self):
        # given
        cache_entry = CacheEntry(datetime.now(), datetime.now(),
                                 datetime.now(), "value")
        serde = Mock()
        serde.serialize.return_value = b"in"
        wrapper = EncodingSerDe(serde, binary_encoding="base64")

        # when
        bytes = wrapper.serialize(cache_entry)

        # then
        expected = codecs.encode(b"in", 'base64')
        self.assertEqual(bytes, expected)
        serde.serialize.assert_called_once_with(cache_entry)
Exemplo n.º 9
0
    def test_should_apply_value_transformations_on_deserialization(self):
        # given
        encode = Mock(return_value="in")
        decode = Mock(return_value="out")
        serde = JsonSerDe(string_encoding='utf-8',
                          value_to_reversible_repr=encode,
                          reversible_repr_to_value=decode)

        # when
        data = serde.deserialize(
            b'{"created":1,"update_after":2,"expires_after":3,"value":"in"}')

        # then
        cache_entry = CacheEntry(datetime.utcfromtimestamp(1),
                                 datetime.utcfromtimestamp(2),
                                 datetime.utcfromtimestamp(3), "out")
        self.assertEqual(data, cache_entry)
        decode.assert_called_once_with("in")
Exemplo n.º 10
0
    def test_should_encode_as_readable_json(self):
        # given
        cache_entry = CacheEntry(datetime.utcfromtimestamp(1),
                                 datetime.utcfromtimestamp(2),
                                 datetime.utcfromtimestamp(3), "in")
        serde = JsonSerDe(string_encoding='utf-8')

        # when
        bytes = serde.serialize(cache_entry)

        # then
        parsed = json.loads(
            codecs.decode(bytes)
        )  # verified this way due to json/ujson slight separator differences
        self.assertEqual(
            parsed, {
                "created": cache_entry.created.timestamp(),
                "update_after": cache_entry.update_after.timestamp(),
                "expires_after": cache_entry.expires_after.timestamp(),
                "value": "in"
            })
Exemplo n.º 11
0
from datetime import datetime
from tornado.testing import AsyncTestCase, gen_test
from memoize.entry import CacheKey, CacheEntry
from memoize.storage import LocalInMemoryCacheStorage

CACHE_SAMPLE_ENTRY = CacheEntry(datetime.now(), datetime.now(), datetime.now(),
                                "value")

CACHE_KEY = CacheKey("key")


class LocalInMemoryCacheStorageTests(AsyncTestCase):
    def setUp(self):
        super().setUp()
        self.storage = LocalInMemoryCacheStorage()

    def tearDown(self):
        super().tearDown()

    @gen_test
    def test_offer_and_get_returns_same_object(self):
        # given
        yield self.storage.offer(CACHE_KEY, CACHE_SAMPLE_ENTRY)

        # when
        returned_value = yield self.storage.get(CACHE_KEY)

        # then
        self.assertEqual(returned_value.value, "value")

    @gen_test
Exemplo n.º 12
0
 def build(self, key: CacheKey, value: CachedValue) -> CacheEntry:
     now = datetime.datetime.utcnow()
     return CacheEntry(created=now,
                       update_after=now + self._update_after,
                       expires_after=now + self._expires_after,
                       value=value)