Repository: fppt/jedis-mock
Pull request #103 (2020-11-12 16:59:37)

Added zscore operation


Patch:
diff --git a/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java b/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java
index 4792732..6dedef6 100644
--- a/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java
+++ b/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java
@@ -89,7 +89,9 @@ public class OperationFactory {
         TRANSACTIONAL_OPERATIONS.put("zrange", RO_zrange::new);
         TRANSACTIONAL_OPERATIONS.put("zrangebylex", RO_zrangebylex::new);
         TRANSACTIONAL_OPERATIONS.put("zrem", RO_zrem::new);
+        TRANSACTIONAL_OPERATIONS.put("client", RO_client::new);
         TRANSACTIONAL_OPERATIONS.put("rename", RO_rename::new);
+        TRANSACTIONAL_OPERATIONS.put("zscore", RO_zscore::new);
     }
 
 
diff --git a/src/main/java/com/github/fppt/jedismock/operations/RO_client.java b/src/main/java/com/github/fppt/jedismock/operations/RO_client.java
new file mode 100644
index 0000000..76dfa2f
--- /dev/null
+++ b/src/main/java/com/github/fppt/jedismock/operations/RO_client.java
@@ -0,0 +1,18 @@
+package com.github.fppt.jedismock.operations;
+
+import com.github.fppt.jedismock.server.Response;
+import com.github.fppt.jedismock.server.Slice;
+import com.github.fppt.jedismock.storage.RedisBase;
+
+import java.util.List;
+
+class RO_client extends AbstractRedisOperation {
+  RO_client(RedisBase base, List<Slice> params) {
+      super(base, params);
+  }
+
+  Slice response() {
+      Slice key = params().get(1);
+      return Response.bulkString(key);
+  }
+}
diff --git a/src/main/java/com/github/fppt/jedismock/operations/RO_rename.java b/src/main/java/com/github/fppt/jedismock/operations/RO_rename.java
index 1de3362..ec0bda9 100644
--- a/src/main/java/com/github/fppt/jedismock/operations/RO_rename.java
+++ b/src/main/java/com/github/fppt/jedismock/operations/RO_rename.java
@@ -19,6 +19,8 @@ class RO_rename extends AbstractRedisOperation {
         if (ttl == null) {
             return false;
         }
+
+        base().deleteValue(newKey);
         for (Map.Entry<Slice, Slice> entry : value.entrySet()) {
             base().putValue(newKey, entry.getKey(), entry.getValue(), ttl);
         }
diff --git a/src/main/java/com/github/fppt/jedismock/operations/RO_zscore.java b/src/main/java/com/github/fppt/jedismock/operations/RO_zscore.java
new file mode 100644
index 0000000..e3ef5e4
--- /dev/null
+++ b/src/main/java/com/github/fppt/jedismock/operations/RO_zscore.java
@@ -0,0 +1,31 @@
+package com.github.fppt.jedismock.operations;
+
+import com.github.fppt.jedismock.server.Response;
+import com.github.fppt.jedismock.server.Slice;
+import com.github.fppt.jedismock.storage.RedisBase;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+class RO_zscore extends AbstractRedisOperation {
+    
+    RO_zscore(RedisBase base, List<Slice> params) {
+        super(base, params);
+    }
+
+    @Override
+    Slice response() {
+        Slice key = params().get(0);
+        Slice val = params().get(1);
+        
+        Map<Slice, Double> map = getDataFromBase(key, new LinkedHashMap<>());
+        
+        if(val == null || val.toString().isEmpty()) {
+            return Response.error("Valid parameter must be provided");
+        }
+        
+        Double score = map.get(Slice.create(val.toString()));
+        
+        return score == null ? Response.NULL : Response.bulkString(Slice.create(score.toString()));
+    }
+}


Test patch:
diff --git a/src/test/java/com/github/fppt/jedismock/TestRedisOperationExecutor.java b/src/test/java/com/github/fppt/jedismock/TestRedisOperationExecutor.java
index 0bde873..ce43c0e 100644
--- a/src/test/java/com/github/fppt/jedismock/TestRedisOperationExecutor.java
+++ b/src/test/java/com/github/fppt/jedismock/TestRedisOperationExecutor.java
@@ -440,4 +440,14 @@ public class TestRedisOperationExecutor {
         assertCommandNull(array("get", "old"));
     }
 
+    @Test
+    public void testRenameHashWithExistingValues() {
+        assertCommandEquals(1, array("hset", "old", "a", "1"));
+        assertCommandEquals(1, array("hset", "new", "z", "2"));
+        assertCommandOK(array("rename", "old", "new"));
+        assertCommandEquals("1", array("hget", "new", "a"));
+        assertCommandNull(array("hget", "new", "z"));
+        assertCommandNull(array("get", "old"));
+    }
+
 }
diff --git a/src/test/java/com/github/fppt/jedismock/TestRedisServer.java b/src/test/java/com/github/fppt/jedismock/TestRedisServer.java
index 1c32421..839a5bf 100644
--- a/src/test/java/com/github/fppt/jedismock/TestRedisServer.java
+++ b/src/test/java/com/github/fppt/jedismock/TestRedisServer.java
@@ -72,6 +72,16 @@ public class TestRedisServer {
         }
     }
 
+    @Test
+    public void testSetClientName() throws IOException {
+      RedisServer server = RedisServer.newRedisServer();
+      server.start();
+
+      Jedis jedis = new Jedis(server.getHost(), server.getBindPort());
+      String clientName = "Test Client Name";
+      assertEquals(clientName, jedis.clientSetname("Test Client Name"));
+    }
+
     @Test
     public void whenRepeatedlyStoppingAndStartingServer_EnsureItResponds() throws IOException {
         for (int i = 0; i < 20; i ++){
diff --git a/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java b/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
index bf90798..0de6a60 100644
--- a/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
+++ b/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
@@ -965,4 +965,24 @@ public class SimpleOperationsTest {
 
         assertNull(result);
     }
+    
+    @TestTemplate
+    public void zscore(Jedis jedis) {
+        jedis.flushDB();
+        
+        String key = "a_key";
+        Map<String, Double> members = new HashMap<>();
+        members.put("aaa", 0d);
+        members.put("bbb", 1d);
+        members.put("ddd", 1d);
+        
+        long result = jedis.zadd(key, members);
+        assertEquals(3L, result); 
+       
+        assertEquals(0d, jedis.zscore(key, "aaa"));
+        assertEquals(1d, jedis.zscore(key, "bbb"));
+        assertEquals(1d, jedis.zscore(key, "ddd"));
+        assertNull(jedis.zscore(key, "ccc"));
+    }
+    
 }