Repository: fppt/jedis-mock
Pull request #49 (2019-06-04 15:00:35)

Add hkeys support


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 4e7142b..64d17e3 100644
--- a/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java
+++ b/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java
@@ -67,6 +67,7 @@ public class OperationFactory {
         TRANSACTIONAL_OPERATIONS.put("hget", RO_hget::new);
         TRANSACTIONAL_OPERATIONS.put("hset", RO_hset::new);
         TRANSACTIONAL_OPERATIONS.put("hdel", RO_hdel::new);
+        TRANSACTIONAL_OPERATIONS.put("hkeys", RO_hkeys::new);
         TRANSACTIONAL_OPERATIONS.put("hgetall", RO_hgetall::new);
         TRANSACTIONAL_OPERATIONS.put("sinter", RO_sinter::new);
         TRANSACTIONAL_OPERATIONS.put("hmget", RO_hmget::new);
diff --git a/src/main/java/com/github/fppt/jedismock/operations/RO_hkeys.java b/src/main/java/com/github/fppt/jedismock/operations/RO_hkeys.java
new file mode 100644
index 0000000..b42574f
--- /dev/null
+++ b/src/main/java/com/github/fppt/jedismock/operations/RO_hkeys.java
@@ -0,0 +1,33 @@
+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.Arrays;
+import java.util.List;
+import java.util.Map;
+
+public class RO_hkeys extends AbstractRedisOperation {
+    public RO_hkeys(RedisBase base, List<Slice> params) {
+        super(base, params);
+    }
+
+    @Override
+    Slice response() {
+        Slice hash = params().get(0);
+
+        Map<Slice, Slice> fieldAndValueMap = base().getFieldsAndValues(hash);
+        int arraySize = fieldAndValueMap.size();
+        Slice [] fkeys = new Slice[arraySize];
+
+        int currentIndex = 0;
+        for (Map.Entry<Slice, Slice> entry: fieldAndValueMap.entrySet()){
+            fkeys[currentIndex] = Response.bulkString(entry.getKey());
+            currentIndex++;
+        }
+
+        List<Slice> values = Arrays.asList(fkeys);
+        return Response.array(values);
+    }
+}


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 f730542..ed088b5 100644
--- a/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
+++ b/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
@@ -343,6 +343,25 @@ public class SimpleOperationsTest extends ComparisonBase {
         assertEquals(0, result.size());
     }
 
+    @Theory
+    public void whenHKeys_EnsureAllKeysReturned(Jedis jedis){
+        jedis.hset(HASH, FIELD_1, VALUE_1);
+        jedis.hset(HASH, FIELD_2, VALUE_2);
+
+        Set<String> toCompare = new HashSet<String>();
+        toCompare.add(FIELD_1);
+        toCompare.add(FIELD_2);
+
+        Set<String> result = jedis.hkeys(HASH);
+        assertTrue(result.equals(toCompare));
+
+        toCompare.add(FIELD_3);
+        jedis.hset(HASH, FIELD_3, VALUE_3);
+
+        result = jedis.hkeys(HASH);
+        assertTrue(result.equals(toCompare));
+    }
+
     @Theory
     public void whenUsingHsinter_EnsureSetIntersectionIsReturned(Jedis jedis) {
         String key1 = "my-set-key-1";