Repository: fppt/jedis-mock (java)

Issue:
Unsupported operation 'hvals'
Add support for 'hvals'.

thinks!

Pull request:
Fix #45: implement hvals
Fix #45: implement `hvals` 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 8bc5514..4792732 100644
--- a/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java
+++ b/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java
@@ -65,11 +65,13 @@ public class OperationFactory {
         TRANSACTIONAL_OPERATIONS.put("spop", RO_spop::new);
         TRANSACTIONAL_OPERATIONS.put("srem", RO_srem::new);
         TRANSACTIONAL_OPERATIONS.put("scard", RO_scard::new);
+        TRANSACTIONAL_OPERATIONS.put("sismember", RO_sismember::new);
         TRANSACTIONAL_OPERATIONS.put("hexists", RO_hexists::new);
         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("hvals", RO_hvals::new);
         TRANSACTIONAL_OPERATIONS.put("hlen", RO_hlen::new);
         TRANSACTIONAL_OPERATIONS.put("hgetall", RO_hgetall::new);
         TRANSACTIONAL_OPERATIONS.put("hincrby", RO_hincrby::new);
diff --git a/src/main/java/com/github/fppt/jedismock/operations/RO_hvals.java b/src/main/java/com/github/fppt/jedismock/operations/RO_hvals.java
new file mode 100644
index 0000000..7bb9d71
--- /dev/null
+++ b/src/main/java/com/github/fppt/jedismock/operations/RO_hvals.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_hvals extends AbstractRedisOperation {
+    public RO_hvals(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 [] fvals = new Slice[arraySize];
+
+        int currentIndex = 0;
+        for (Map.Entry<Slice, Slice> entry: fieldAndValueMap.entrySet()){
+            fvals[currentIndex] = Response.bulkString(entry.getValue());
+            currentIndex++;
+        }
+
+        List<Slice> values = Arrays.asList(fvals);
+        return Response.array(values);
+    }
+}
diff --git a/src/main/java/com/github/fppt/jedismock/operations/RO_sismember.java b/src/main/java/com/github/fppt/jedismock/operations/RO_sismember.java
new file mode 100644
index 0000000..23b6d69
--- /dev/null
+++ b/src/main/java/com/github/fppt/jedismock/operations/RO_sismember.java
@@ -0,0 +1,24 @@
+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;
+import java.util.Set;
+
+public class RO_sismember extends AbstractRedisOperation {
+
+    RO_sismember(RedisBase base, List<Slice> params) {
+        super(base, params);
+    }
+
+    @Override
+    Slice response() {
+        Slice key = params().get(0);
+        Slice member = params().get(1);
+        Set<Slice> set = getDataFromBase(key, null);
+        if (set == null || set.isEmpty()) return Response.integer(0);
+        return Response.integer(set.contains(member) ? 1 : 0);
+    }
+}


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 3b4d4c2..98baaff 100644
--- a/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
+++ b/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java
@@ -313,6 +313,15 @@ public class SimpleOperationsTest extends ComparisonBase {
         } while (poppedValue != null);
     }
 
+    @Theory
+    public void ensureSismemberReturnsCorrectValues(Jedis jedis){
+        String key = "my-set-key-sismember";
+        jedis.sadd(key, "A", "B");
+        assertTrue(jedis.sismember(key, "A"));
+        assertFalse(jedis.sismember(key, "C"));
+        assertFalse(jedis.sismember(key + "-nonexistent", "A"));
+    }
+
     @Theory
     public void whenHSettingOnTheSameKeys_EnsureReturnTypeIs1WhenKeysAreNew(Jedis jedis) {
         assertEquals(new Long(1L), jedis.hset(HASH, FIELD_1, VALUE_1));
@@ -384,7 +393,7 @@ public class SimpleOperationsTest extends ComparisonBase {
         jedis.hset(HASH, FIELD_1, VALUE_1);
         jedis.hset(HASH, FIELD_2, VALUE_2);
 
-        Set<String> toCompare = new HashSet<String>();
+        Set<String> toCompare = new HashSet<>();
         toCompare.add(FIELD_1);
         toCompare.add(FIELD_2);
 
@@ -398,6 +407,25 @@ public class SimpleOperationsTest extends ComparisonBase {
         assertEquals(result, toCompare);
     }
 
+    @Theory
+    public void whenHVals_EnsureAllValuesReturned(Jedis jedis) {
+        String key = "my-hvals-key";
+        jedis.hset(key, FIELD_1, VALUE_1);
+        jedis.hset(key, FIELD_2, VALUE_2);
+
+        Set<String> toCompare = new HashSet<>();
+        toCompare.add(VALUE_1);
+        toCompare.add(VALUE_2);
+        Set<String> result = new HashSet<>(jedis.hvals(key));
+        assertEquals(result, toCompare);
+
+        toCompare.add(VALUE_3);
+        jedis.hset(key, FIELD_3, VALUE_3);
+
+        result = new HashSet<>(jedis.hvals(key));
+        assertEquals(result, toCompare);
+    }
+
     @Theory
     public void whenHLen_EnsureCorrectLengthReturned(Jedis jedis) {
         jedis.flushDB();