You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by hu...@apache.org on 2023/05/26 10:15:30 UTC

[incubator-kvrocks] branch unstable updated: Refine functions and add parsing test to set and zset (#1475)

This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new e0667812 Refine functions and add parsing test to set and zset  (#1475)
e0667812 is described below

commit e0667812de2ce807cc10167e7c24e5dad4a94365
Author: Leo <zn...@gmail.com>
AuthorDate: Fri May 26 18:15:23 2023 +0800

    Refine functions and add parsing test to set and zset  (#1475)
---
 src/commands/cmd_set.cc                  |  9 +++++++
 src/commands/cmd_zset.cc                 |  6 ++++-
 tests/gocase/unit/type/set/set_test.go   | 33 +++++++++++++++++++++++
 tests/gocase/unit/type/zset/zset_test.go | 46 ++++++++++++++++++++++++++++++++
 4 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/src/commands/cmd_set.cc b/src/commands/cmd_set.cc
index 736994e4..2f409fee 100644
--- a/src/commands/cmd_set.cc
+++ b/src/commands/cmd_set.cc
@@ -142,11 +142,17 @@ class CommandSMIsMember : public Commander {
 class CommandSPop : public Commander {
  public:
   Status Parse(const std::vector<std::string> &args) override {
+    if (args.size() > 3) {
+      return {Status::RedisParseErr, errWrongNumOfArguments};
+    }
     if (args.size() == 3) {
       auto parse_result = ParseInt<int>(args[2], 10);
       if (!parse_result) {
         return {Status::RedisParseErr, errValueNotInteger};
       }
+      if (*parse_result < 0) {
+        return {Status::RedisParseErr, errValueMustBePositive};
+      }
 
       count_ = *parse_result;
       with_count_ = true;
@@ -182,6 +188,9 @@ class CommandSPop : public Commander {
 class CommandSRandMember : public Commander {
  public:
   Status Parse(const std::vector<std::string> &args) override {
+    if (args.size() > 3) {
+      return {Status::RedisParseErr, errWrongNumOfArguments};
+    }
     if (args.size() == 3) {
       auto parse_result = ParseInt<int>(args[2], 10);
       if (!parse_result) {
diff --git a/src/commands/cmd_zset.cc b/src/commands/cmd_zset.cc
index 120b04c7..ca4e1322 100644
--- a/src/commands/cmd_zset.cc
+++ b/src/commands/cmd_zset.cc
@@ -225,7 +225,11 @@ class CommandZPop : public Commander {
   explicit CommandZPop(bool min) : min_(min) {}
 
   Status Parse(const std::vector<std::string> &args) override {
-    if (args.size() > 2) {
+    if (args.size() > 3) {
+      return {Status::RedisParseErr, errWrongNumOfArguments};
+    }
+
+    if (args.size() == 3) {
       auto parse_result = ParseInt<int>(args[2], 10);
       if (!parse_result) {
         return {Status::RedisParseErr, errValueNotInteger};
diff --git a/tests/gocase/unit/type/set/set_test.go b/tests/gocase/unit/type/set/set_test.go
index 30a26bd7..b7fa08d0 100644
--- a/tests/gocase/unit/type/set/set_test.go
+++ b/tests/gocase/unit/type/set/set_test.go
@@ -550,10 +550,43 @@ func TestSet(t *testing.T) {
 		require.EqualValues(t, []string{"1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9"}, array)
 	})
 
+	t.Run("SPOP with <count> not integer", func(t *testing.T) {
+		CreateSet(t, rdb, ctx, "myset", []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
+		require.ErrorContains(t, rdb.Do(ctx, "spop", "myset", 3.5).Err(), "is not an integer")
+		require.ErrorContains(t, rdb.Do(ctx, "spop", "myset", -5.5).Err(), "is not an integer")
+	})
+
+	t.Run("SPOP with <count> not positive", func(t *testing.T) {
+		CreateSet(t, rdb, ctx, "myset", []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
+		require.ErrorContains(t, rdb.Do(ctx, "spop", "myset", -5.5).Err(), "is not an integer")
+		require.ErrorContains(t, rdb.SPopN(ctx, "myset", -2).Err(), "must be positive")
+
+		// 0 is an exception, although it is not a positive number, it will not return error, just like redis
+		cmd := rdb.SPopN(ctx, "myset", 0)
+		require.NoError(t, cmd.Err())
+		require.EqualValues(t, []string{}, cmd.Val())
+	})
+
+	t.Run("SPOP the num of arguments is not 2 or 3", func(t *testing.T) {
+		CreateSet(t, rdb, ctx, "myset", []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
+		require.ErrorContains(t, rdb.Do(ctx, "spop").Err(), "wrong number of arguments")
+		require.NoError(t, rdb.Do(ctx, "spop", "myset").Err())
+		require.NoError(t, rdb.Do(ctx, "spop", "myset", 1).Err())
+		require.ErrorContains(t, rdb.Do(ctx, "spop", "myset", 1, 1).Err(), "wrong number of arguments")
+	})
+
 	t.Run("SRANDMEMBER with <count> against non existing key", func(t *testing.T) {
 		require.EqualValues(t, "", rdb.SRandMember(ctx, "nonexisting_key").Val())
 	})
 
+	t.Run("SRANDMEMBER the num of arguments is not 2 or 3", func(t *testing.T) {
+		CreateSet(t, rdb, ctx, "myset", []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
+		require.ErrorContains(t, rdb.Do(ctx, "srandmember").Err(), "wrong number of arguments")
+		require.NoError(t, rdb.Do(ctx, "srandmember", "myset").Err())
+		require.NoError(t, rdb.Do(ctx, "srandmember", "myset", 1).Err())
+		require.ErrorContains(t, rdb.Do(ctx, "srandmember", "myset", 1, 1).Err(), "wrong number of arguments")
+	})
+
 	SetupMove := func() {
 		require.NoError(t, rdb.Del(ctx, "myset3", "myset4").Err())
 		CreateSet(t, rdb, ctx, "myset1", []interface{}{1, "a", "b"})
diff --git a/tests/gocase/unit/type/zset/zset_test.go b/tests/gocase/unit/type/zset/zset_test.go
index df40b183..cd98d3cd 100644
--- a/tests/gocase/unit/type/zset/zset_test.go
+++ b/tests/gocase/unit/type/zset/zset_test.go
@@ -198,11 +198,57 @@ func basicTests(t *testing.T, rdb *redis.Client, ctx context.Context, encoding s
 		util.ErrorRegexp(t, rdb.Do(ctx, "zadd", "myzset", 10, "a", 20, "b", 30, "c", 40).Err(), ".*syntax.*")
 	})
 
+	t.Run("ZADD - invalid score will raise error", func(t *testing.T) {
+		rdb.Del(ctx, "myzet")
+		require.ErrorContains(t, rdb.Do(ctx, "zadd", "myzet", "one", "one").Err(), "is not a valid float")
+		require.ErrorContains(t, rdb.Do(ctx, "zadd", "myzet", "3.3.3", "one").Err(), "is not a valid float")
+	})
+
 	t.Run("ZINCRBY does not work variadic even if shares ZADD implementation", func(t *testing.T) {
 		rdb.Del(ctx, "myzset")
 		util.ErrorRegexp(t, rdb.Do(ctx, "zincrby", "myzset", 10, "a", 20, "b", 30, "c").Err(), ".*ERR.*wrong.*number.*arg.*")
 	})
 
+	t.Run("ZINCRBY - invalid increment will raise error", func(t *testing.T) {
+		rdb.Del(ctx, "myzet")
+		require.NoError(t, rdb.ZIncrBy(ctx, "myzet", 1, "one").Err())
+		require.ErrorContains(t, rdb.Do(ctx, "zincrby", "myzet", "one", "one").Err(), "is not a valid float")
+		require.ErrorContains(t, rdb.Do(ctx, "zincrby", "myzet", "3.3.3", "one").Err(), "is not a valid float")
+	})
+
+	t.Run("ZLEXCOUNT - invalid range will raise error", func(t *testing.T) {
+		rdb.Del(ctx, "myzet")
+		require.ErrorContains(t, rdb.ZLexCount(ctx, "myzet", "+", "-").Err(), "min > max")
+		require.ErrorContains(t, rdb.ZLexCount(ctx, "myzet", "x", "[y").Err(), "the min is illegal")
+		require.ErrorContains(t, rdb.ZLexCount(ctx, "myzet", "[x", "y").Err(), "the max is illegal")
+	})
+
+	t.Run("ZPOP - the num of arguments is not 2 or 3", func(t *testing.T) {
+		rdb.Del(ctx, "myzet")
+		require.ErrorContains(t, rdb.Do(ctx, "zpopmin", "myzet", "1", "1").Err(), "wrong number of arguments")
+		require.ErrorContains(t, rdb.Do(ctx, "zpopmax", "myzet", "1", "1").Err(), "wrong number of arguments")
+	})
+
+	t.Run("ZUNIONSTORE - invalid numkeys will raise error", func(t *testing.T) {
+		rdb.Del(ctx, "out")
+		rdb.Del(ctx, "myzet")
+		require.ErrorContains(t, rdb.Do(ctx, "zunionstore", "out", "one", "myzet").Err(), "value is not an integer or out of range")
+		require.ErrorContains(t, rdb.Do(ctx, "zunionstore", "out", "3.3", "myzet").Err(), "value is not an integer or out of range")
+	})
+
+	t.Run("ZUNIONSTORE - invalid weights will raise error", func(t *testing.T) {
+		rdb.Del(ctx, "out")
+		rdb.Del(ctx, "myzet")
+		require.ErrorContains(t, rdb.Do(ctx, "zunionstore", "out", "1", "myzet", "weights", "one").Err(), "weight is not a double or out of range")
+		require.ErrorContains(t, rdb.Do(ctx, "zunionstore", "out", "1", "myzet", "weights", "3.3.3").Err(), "weight is not a double or out of range")
+	})
+
+	t.Run("ZUNIONSTORE - invalid aggregate will raise error", func(t *testing.T) {
+		rdb.Del(ctx, "out")
+		rdb.Del(ctx, "myzet")
+		require.ErrorContains(t, rdb.Do(ctx, "zunionstore", "out", "1", "myzet", "aggregate", "xxx").Err(), "aggregate param error")
+	})
+
 	t.Run(fmt.Sprintf("ZCARD basics - %s", encoding), func(t *testing.T) {
 		rdb.Del(ctx, "ztmp")
 		rdb.ZAdd(ctx, "ztmp", redis.Z{Score: 10, Member: "a"}, redis.Z{Score: 20, Member: "b"}, redis.Z{Score: 30, Member: "c"})