def test_serialize_no_compress(self): zlib_patch = mock.patch.object(memcache_lib, "zlib") zlib = zlib_patch.start() self.addCleanup(zlib_patch.stop) dump_no_compress = memcache_lib.make_dump_and_compress_fn( min_compress_length=0 # disable compression ) value, flags = dump_no_compress(key="key", value="simple string") self.assertEqual(value, b"simple string") self.assertEqual(flags, 0) zlib.compress.assert_not_called()
def test_serialize_other(self): json_patch = mock.patch.object(memcache_lib, "json") json = json_patch.start() self.addCleanup(json_patch.stop) json.dumps.return_value = "expected" dump_no_compress = memcache_lib.make_dump_and_compress_fn() value, flags = dump_no_compress(key="key", value=("stuff", 1, False)) json.dumps.assert_called_with(("stuff", 1, False)) self.assertEqual(value, b"expected") self.assertEqual(flags, memcache_lib.Flags.JSON)
def test_serialize_compress(self): zlib_patch = mock.patch.object(memcache_lib, "zlib") zlib = zlib_patch.start() self.addCleanup(zlib_patch.stop) expected_value = object() zlib.compress.return_value = expected_value json_and_compress = memcache_lib.make_dump_and_compress_fn( min_compress_length=1, compress_level=1) value, flags = json_and_compress(key="key", value="simple string") self.assertEqual(value, expected_value) self.assertEqual(flags, memcache_lib.Flags.ZLIB) zlib.compress.assert_called_with(b"simple string", 1)
def test_serialize_int(self): dump_no_compress = memcache_lib.make_dump_and_compress_fn() value, flags = dump_no_compress(key="key", value=100) self.assertEqual(value, b"100") self.assertEqual(flags, memcache_lib.Flags.INTEGER)
def test_serialize_str(self): dump_no_compress = memcache_lib.make_dump_and_compress_fn() value, flags = dump_no_compress(key="key", value="val") self.assertEqual(value, b"val") self.assertEqual(flags, 0)