Esempio n. 1
0
 def test_deserialize_int(self):
     value = memcache_lib.decompress_and_unpickle(
         key="key",
         serialized="100",
         flags=memcache_lib.PickleFlags.INTEGER)
     self.assertEqual(value, 100)
     self.assertTrue(isinstance(value, int))
Esempio n. 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_unpickle(key="key",
                                                     serialized="stuff",
                                                     flags=0)
        self.assertEqual(value, "stuff")
        zlib.decompress.assert_not_called()
Esempio n. 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.PickleFlags.ZLIB
        value = memcache_lib.decompress_and_unpickle(key="key",
                                                     serialized="nonsense",
                                                     flags=flags)
        self.assertEqual(value, expected_value)
        zlib.decompress.assert_called_with("nonsense")
Esempio n. 4
0
    def test_deserialize_other(self):
        pickle_patch = mock.patch.object(memcache_lib, "pickle")
        pickle = pickle_patch.start()
        self.addCleanup(pickle_patch.stop)

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

        value = memcache_lib.decompress_and_unpickle(
            key="key",
            serialized="garbage",
            flags=memcache_lib.PickleFlags.PICKLE)

        pickle.loads.assert_called_with("garbage")
        self.assertEqual(value, expected_value)
Esempio n. 5
0
    def test_deserialize_decompress_int(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

        int_patch = mock.patch.object(builtins, "int")
        int_cls = int_patch.start()
        self.addCleanup(int_patch.stop)

        expected_int_value = object()
        int_cls.return_value = expected_int_value
        flags = memcache_lib.PickleFlags.INTEGER | memcache_lib.PickleFlags.ZLIB
        value = memcache_lib.decompress_and_unpickle(key="key",
                                                     serialized="nonsense",
                                                     flags=flags)
        zlib.decompress.assert_called_with("nonsense")
        int_cls.assert_called_with(expected_zlib_value)
        self.assertEqual(value, expected_int_value)
Esempio n. 6
0
 def test_deserialize_str(self):
     value = memcache_lib.decompress_and_unpickle(key="key",
                                                  serialized="val",
                                                  flags=0)
     self.assertEqual(value, "val")