예제 #1
0
    def test_deserialize_long(self):
        value = memcache_lib.decompress_and_load(key="key",
                                                 serialized="100",
                                                 flags=memcache_lib.Flags.LONG)

        self.assertEqual(value, 100)
        self.assertTrue(isinstance(value, int))
예제 #2
0
    def test_deserialize_no_decompress(self):
        zlib_patch = mock.patch.object(memcache_lib, "zlib")
        zlib = zlib_patch.start()
        self.addCleanup(zlib_patch.stop)

        value = memcache_lib.decompress_and_load(key="key",
                                                 serialized="stuff",
                                                 flags=0)
        self.assertEqual(value, "stuff")
        zlib.decompress.assert_not_called()
예제 #3
0
    def test_deserialize_decompress_str(self):
        zlib_patch = mock.patch.object(memcache_lib, "zlib")
        zlib = zlib_patch.start()
        self.addCleanup(zlib_patch.stop)

        expected_value = object()
        zlib.decompress.return_value = expected_value

        flags = 0 | memcache_lib.Flags.ZLIB
        value = memcache_lib.decompress_and_load(key="key",
                                                 serialized=b"nonsense",
                                                 flags=flags)
        self.assertEqual(value, expected_value)
        zlib.decompress.assert_called_with(b"nonsense")
예제 #4
0
    def test_deserialize_other(self):
        json_patch = mock.patch.object(memcache_lib, "json")
        json = json_patch.start()
        self.addCleanup(json_patch.stop)

        expected_value = object()
        json.loads.return_value = expected_value

        value = memcache_lib.decompress_and_load(key="key",
                                                 serialized=b"garbage",
                                                 flags=memcache_lib.Flags.JSON)

        json.loads.assert_called_with(b"garbage")
        self.assertEqual(value, expected_value)
예제 #5
0
    def test_deserialize_decompress_long(self):
        zlib_patch = mock.patch.object(memcache_lib, "zlib")
        zlib = zlib_patch.start()
        self.addCleanup(zlib_patch.stop)

        expected_zlib_value = object()
        zlib.decompress.return_value = expected_zlib_value

        long_patch = mock.patch.object(memcache_lib, "int")
        long_cls = long_patch.start()
        self.addCleanup(long_patch.stop)

        expected_long_value = object()
        long_cls.return_value = expected_long_value
        flags = memcache_lib.Flags.LONG | memcache_lib.Flags.ZLIB
        value = memcache_lib.decompress_and_load(key="key",
                                                 serialized="nonsense",
                                                 flags=flags)
        zlib.decompress.assert_called_with("nonsense")
        long_cls.assert_called_with(expected_zlib_value)
        self.assertEqual(value, expected_long_value)
예제 #6
0
    def test_deserialize_decompress_unjson(self):
        zlib_patch = mock.patch.object(memcache_lib, "zlib")
        zlib = zlib_patch.start()
        self.addCleanup(zlib_patch.stop)

        expected_zlib_value = object()
        zlib.decompress.return_value = expected_zlib_value

        json_patch = mock.patch.object(memcache_lib, "json")
        json = json_patch.start()
        self.addCleanup(json_patch.stop)

        expected_json_value = object()
        json.loads.return_value = expected_json_value

        flags = memcache_lib.Flags.JSON | memcache_lib.Flags.ZLIB
        value = memcache_lib.decompress_and_load(key="key",
                                                 serialized="nonsense",
                                                 flags=flags)
        zlib.decompress.assert_called_with("nonsense")
        json.loads.assert_called_with(expected_zlib_value)
        self.assertEqual(value, expected_json_value)
예제 #7
0
 def test_deserialize_str(self):
     value = memcache_lib.decompress_and_load(key="key",
                                              serialized="val",
                                              flags=0)
     self.assertEqual(value, "val")