|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 |
|
| 3 | +from functools import reduce |
3 | 4 | import sys
|
4 | 5 | import os
|
5 | 6 | import redis
|
@@ -1053,6 +1054,64 @@ def testInfoEverything(env):
|
1053 | 1054 | res = r.execute_command('INFO', 'EVERYTHING')
|
1054 | 1055 | r.assertFalse(res['modules'] is None)
|
1055 | 1056 |
|
| 1057 | +def testCopyCommand(env): |
| 1058 | + """Test COPY command and make sure behavior of json keys is similar to hash keys""" |
| 1059 | + |
| 1060 | + env.skipOnCluster() |
| 1061 | + r = env |
| 1062 | + |
| 1063 | + values = {"foo": "bar", "fu": "wunderbar"} |
| 1064 | + |
| 1065 | + ### Copy json to a new key (from json1 to json2) |
| 1066 | + r.assertOk(r.execute_command('JSON.SET', 'json1', '$', json.dumps(values))) |
| 1067 | + r.assertTrue(r.execute_command('COPY', 'json1', 'json2')) |
| 1068 | + # Check new values |
| 1069 | + res = r.execute_command('JSON.GET', 'json1', '$') |
| 1070 | + r.assertEqual(json.loads(res), [values]) |
| 1071 | + res = r.execute_command('JSON.GET', 'json2', '$') |
| 1072 | + r.assertEqual(json.loads(res), [values]) |
| 1073 | + |
| 1074 | + ### Copy hash to a new key (from hash1 to hash2) |
| 1075 | + hash_values = list(reduce(lambda acc, v: acc + v, values.items())) |
| 1076 | + r.assertEqual(r.execute_command('HSET', 'hash1', *hash_values), int(len(hash_values) / 2)) |
| 1077 | + r.assertTrue(r.execute_command('COPY', 'hash1', 'hash2')) |
| 1078 | + # Check new values |
| 1079 | + r.assertEqual(r.execute_command('HGETALL', 'hash1'), values) |
| 1080 | + r.assertEqual(r.execute_command('HGETALL', 'hash2'), values) |
| 1081 | + |
| 1082 | + new_values = {"ganz": "neue"} |
| 1083 | + |
| 1084 | + ### Copy hash to an existing key |
| 1085 | + hash_values = list(reduce(lambda acc, v: acc + v, new_values.items())) |
| 1086 | + r.assertEqual(r.execute_command('HSET', 'hash3', *hash_values), int(len(hash_values) / 2)) |
| 1087 | + # Do not overwrite without REPLACE (from hash to hash) |
| 1088 | + r.assertFalse(r.execute_command('COPY', 'hash3', 'hash2')) |
| 1089 | + # Do not overwrite without REPLACE (from hash to json) |
| 1090 | + r.assertFalse(r.execute_command('COPY', 'hash3', 'json2')) |
| 1091 | + # Overwrite with REPLACE (from hash to hash) |
| 1092 | + r.assertTrue(r.execute_command('COPY', 'hash3', 'hash2', 'REPLACE')) |
| 1093 | + # Overwrite with REPLACE (from hash to json) |
| 1094 | + r.assertTrue(r.execute_command('COPY', 'hash3', 'json2', 'REPLACE')) |
| 1095 | + # Check new values |
| 1096 | + r.assertEqual(r.execute_command('HGETALL', 'hash2'), new_values) |
| 1097 | + r.assertEqual(r.execute_command('HGETALL', 'json2'), new_values) |
| 1098 | + |
| 1099 | + ### Copy json to an existing key |
| 1100 | + r.assertOk(r.execute_command('JSON.SET', 'json3', '$', json.dumps(new_values))) |
| 1101 | + # Do not overwrite without REPLACE (from json to json) |
| 1102 | + r.assertFalse(r.execute_command('COPY', 'json3', 'json2')) |
| 1103 | + # Do not overwrite without REPLACE (from json to hash) |
| 1104 | + r.assertFalse(r.execute_command('COPY', 'json3', 'hash2')) |
| 1105 | + # Overwrite with REPLACE (from json to json) |
| 1106 | + r.assertTrue(r.execute_command('COPY', 'json3', 'json2', 'REPLACE')) |
| 1107 | + # Overwrite with REPLACE (from json to hash) |
| 1108 | + r.assertTrue(r.execute_command('COPY', 'json3', 'hash2', 'REPLACE')) |
| 1109 | + # Check new values |
| 1110 | + res = r.execute_command('JSON.GET', 'json2', '$') |
| 1111 | + r.assertEqual(json.loads(res), [new_values]) |
| 1112 | + res = r.execute_command('JSON.GET', 'hash2', '$') |
| 1113 | + r.assertEqual(json.loads(res), [new_values]) |
| 1114 | + |
1056 | 1115 |
|
1057 | 1116 | # class CacheTestCase(BaseReJSONTest):
|
1058 | 1117 | # @property
|
|
0 commit comments