Repository: fppt/jedis-mock
Pull request #94 (2020-06-02 10:32:09)

Fix hsetnx not returning 0 if field already exists.
According to https://redis.io/commands/hsetnx HSETNX should return 0 if value to be set already exists (and thus was not set), but jedis-mock incorrectly returns 1 in this case.

This patch fixes it and extends the test to verify hsetnx returns a correct value.


Patch:
diff --git a/README.md b/README.md
index 82e92df..6d0c4be 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,7 @@ Add it as a dependency in Maven as:
 <dependency>
   <groupId>com.github.fppt</groupId>
   <artifactId>jedis-mock</artifactId>
-  <version>0.1.16</version>
+  <version>0.1.17</version>
 </dependency>
 ```
 
diff --git a/pom.xml b/pom.xml
index 92629c7..3d0f6cd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
 
     <groupId>com.github.fppt</groupId>
     <artifactId>jedis-mock</artifactId>
-    <version>0.1.17-SNAPSHOT</version>
+    <version>0.1.18-SNAPSHOT</version>
     <packaging>jar</packaging>
     <name>jedis-mock</name>
     <description>In memory Redis mock for testing</description>
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_hsetnx.java b/src/main/java/com/github/fppt/jedismock/operations/RO_hsetnx.java
index 0d22ba4..5cc9a31 100644
--- a/src/main/java/com/github/fppt/jedismock/operations/RO_hsetnx.java
+++ b/src/main/java/com/github/fppt/jedismock/operations/RO_hsetnx.java
@@ -14,7 +14,6 @@ class RO_hsetnx extends RO_hset {
         Slice foundValue = base().getValue(key1, key2);
         if(foundValue == null){
             base().putValue(key1, key2, value, -1L);
-            foundValue = value;
         }
         return foundValue;
     }
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..bd9ef79 100644
--- a/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
+++ b/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
@@ -494,9 +494,9 @@ public class SimpleOperationsTest {
     @TestTemplate
     public void whenUsingHsetnx_EnsureValueIsOnlyPutIfOtherValueDoesNotExist(Jedis jedis) {
         assertNull(jedis.hget(HASH, FIELD_3));
-        jedis.hsetnx(HASH, FIELD_3, VALUE_1);
+        assertEquals(1, jedis.hsetnx(HASH, FIELD_3, VALUE_1));
         assertEquals(VALUE_1, jedis.hget(HASH, FIELD_3));
-        jedis.hsetnx(HASH, FIELD_3, VALUE_2);
+        assertEquals(0, jedis.hsetnx(HASH, FIELD_3, VALUE_2));
         assertEquals(VALUE_1, jedis.hget(HASH, FIELD_3));
     }
 
@@ -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"));
+    }
+    
 }