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, "simple string") self.assertEqual(flags, 0) zlib.compress.assert_not_called()
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, "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) expected_value = object() json.dumps.return_value = expected_value 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, expected_value) 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("simple string", 1)
def test_serialize_other(self): json_patch = mock.patch.object(memcache_lib, "json") json = json_patch.start() self.addCleanup(json_patch.stop) expected_value = object() json.dumps.return_value = expected_value 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, expected_value) self.assertEqual(flags, memcache_lib.Flags.JSON)
def test_serialize_long(self): try: long except NameError: # python3 value = 100 expected_flags = memcache_lib.Flags.INTEGER else: value = long(100) expected_flags = memcache_lib.Flags.LONG dump_no_compress = memcache_lib.make_dump_and_compress_fn() value, flags = dump_no_compress(key="key", value=value) self.assertEqual(value, "100") self.assertEqual(flags, expected_flags)
def test_serialize_long(self): try: long except NameError: # python3 value = 100 expected_flags = memcache_lib.Flags.INTEGER else: value = long(100) expected_flags = memcache_lib.Flags.LONG dump_no_compress = memcache_lib.make_dump_and_compress_fn() value, flags = dump_no_compress(key="key", value=value) self.assertEqual(value, "100") self.assertEqual(flags, expected_flags)
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("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, "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, "val") self.assertEqual(flags, 0)
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, "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, "val") self.assertEqual(flags, 0)