Repository: fppt/jedis-mock
Pull request #18 (2019-01-28 11:59:21)

Implement hsetnx


Patch:
diff --git a/src/main/java/com/github/fppt/jedismock/operations/RO_hset.java b/src/main/java/com/github/fppt/jedismock/operations/RO_hset.java
index 5d8fb8e..9fe567c 100644
--- a/src/main/java/com/github/fppt/jedismock/operations/RO_hset.java
+++ b/src/main/java/com/github/fppt/jedismock/operations/RO_hset.java
@@ -11,13 +11,17 @@ class RO_hset extends AbstractRedisOperation {
         super(base, params);
     }
 
+    Slice hsetValue(Slice key1, Slice key2, Slice value){
+        Slice foundValue = base().getValue(key1, key2);
+        base().putValue(key1, key2, value, -1L);
+        return foundValue;
+    }
+
     Slice response() {
         Slice key1 = params().get(0);
         Slice key2 = params().get(1);
         Slice value = params().get(2);
-
-        Slice oldValue = base().getValue(key1, key2);
-        base().putValue(key1, key2, value, -1L);
+        Slice oldValue = hsetValue(key1, key2, value);
 
         if(oldValue == null){
             return Response.integer(1);
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 ce5c546..0d22ba4 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
@@ -1,21 +1,21 @@
 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_hsetnx extends AbstractRedisOperation {
+class RO_hsetnx extends RO_hset {
     RO_hsetnx(RedisBase base, List<Slice> params) {
         super(base, params);
     }
 
-    Slice response(){
-        if (base().getValue(params().get(0)) == null) {
-            base().putValue(params().get(0), params().get(1));
-            return Response.integer(1);
+    Slice hsetValue(Slice key1, Slice key2, Slice value){
+        Slice foundValue = base().getValue(key1, key2);
+        if(foundValue == null){
+            base().putValue(key1, key2, value, -1L);
+            foundValue = value;
         }
-        return Response.integer(0);
+        return foundValue;
     }
 }


Test patch:
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 7598aaa..ba0236d 100644
--- a/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
+++ b/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
@@ -329,4 +329,13 @@ public class SimpleOperationsTest extends ComparisonBase {
         assertEquals(VALUE_1, jedis.hget(HASH, FIELD_1));
         assertEquals(VALUE_1, jedis.hget(HASH, FIELD_2));
     }
+
+    @Theory
+    public void whenUsingHsetnx_EnsureValueIsOnlyPutIfOtherValueDoesNotExist(Jedis jedis){
+        assertNull(jedis.hget(HASH, FIELD_3));
+        jedis.hsetnx(HASH, FIELD_3, VALUE_1);
+        assertEquals(VALUE_1, jedis.hget(HASH, FIELD_3));
+        jedis.hsetnx(HASH, FIELD_3, VALUE_2);
+        assertEquals(VALUE_1, jedis.hget(HASH, FIELD_3));
+    }
 }