You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by GitBox <gi...@apache.org> on 2022/09/24 15:54:35 UTC

[GitHub] [incubator-kvrocks] git-hulk commented on a diff in pull request #903: Move TCL test unit/type/hash to Go case

git-hulk commented on code in PR #903:
URL: https://github.com/apache/incubator-kvrocks/pull/903#discussion_r979265148


##########
tests/gocase/unit/type/hash/hash_test.go:
##########
@@ -21,67 +21,682 @@ package hash
 
 import (
 	"context"
+	"errors"
+	"fmt"
+	"regexp"
+	"sort"
+	"strconv"
+	"strings"
 	"testing"
+	"time"
 
 	"github.com/apache/incubator-kvrocks/tests/gocase/util"
 	"github.com/stretchr/testify/require"
 )
 
+func getKeys(hash map[string]string) []string {
+	r := make([]string, 0)
+	for key := range hash {
+		r = append(r, key)
+	}
+	return r
+}
+
+func getVals(hash map[string]string) []string {
+	r := make([]string, 0)
+	for _, val := range hash {
+		r = append(r, val)
+	}
+	return r
+}
+
+func getAllSorted(hash map[string]string) map[string]string {

Review Comment:
   I think it won't sort anything since the Go's map is unordered, so what this function do is copy the map without any sort.



##########
tests/gocase/unit/type/hash/hash_test.go:
##########
@@ -21,67 +21,682 @@ package hash
 
 import (
 	"context"
+	"errors"
+	"fmt"
+	"regexp"
+	"sort"
+	"strconv"
+	"strings"
 	"testing"
+	"time"
 
 	"github.com/apache/incubator-kvrocks/tests/gocase/util"
 	"github.com/stretchr/testify/require"
 )
 
+func getKeys(hash map[string]string) []string {
+	r := make([]string, 0)
+	for key := range hash {
+		r = append(r, key)
+	}
+	return r
+}
+
+func getVals(hash map[string]string) []string {
+	r := make([]string, 0)
+	for _, val := range hash {
+		r = append(r, val)
+	}
+	return r
+}
+
+func getAllSorted(hash map[string]string) map[string]string {
+	keys := getKeys(hash)
+	sort.Strings(keys)
+	r := make(map[string]string)
+	for _, key := range keys {
+		r[key] = hash[key]
+	}
+	return r
+}
+
 func TestHash(t *testing.T) {
 	srv := util.StartServer(t, map[string]string{})
 	defer srv.Close()
 	ctx := context.Background()
 	rdb := srv.NewClient()
 	defer func() { require.NoError(t, rdb.Close()) }()
-	kvArray := []string{"a", "a", "b", "b", "c", "c", "d", "d", "e", "e", "key1", "value1", "key2", "value2", "key3", "value3", "key10", "value10", "z", "z", "x", "x"}
-	t.Run("HRange normal situation ", func(t *testing.T) {
-		require.NoError(t, rdb.Del(ctx, "hashkey").Err())
-		require.NoError(t, rdb.HMSet(ctx, "hashkey", kvArray).Err())
-		require.EqualValues(t, []interface{}{"key1", "value1", "key10", "value10"}, rdb.Do(ctx, "HRange", "hashkey", "key1", "key2", "limit", 100).Val())
-		require.EqualValues(t, []interface{}{"key1", "value1", "key10", "value10", "key2", "value2"}, rdb.Do(ctx, "HRange", "hashkey", "key1", "key3", "limit", 100).Val())
+	smallhash := make(map[string]string)
+	bighash := make(map[string]string)
+
+	t.Run("HSET/HLEN - Small hash creation", func(t *testing.T) {
+		for i := 0; i < 8; i++ {
+			key := "__avoid_collisions__" + util.RandString(0, 8, util.Alpha)
+			val := "__avoid_collisions__" + util.RandString(0, 8, util.Alpha)
+			if _, ok := smallhash[key]; ok {
+				i--
+			}
+			rdb.HSet(ctx, "smallhash", key, val)
+			smallhash[key] = val
+		}
+		require.Equal(t, int64(8), rdb.HLen(ctx, "smallhash").Val())
+	})
+
+	t.Run("HSET/HLEN - Big hash creation", func(t *testing.T) {
+		for i := 0; i < 1024; i++ {
+			key := "__avoid_collisions__" + util.RandString(0, 8, util.Alpha)
+			val := "__avoid_collisions__" + util.RandString(0, 8, util.Alpha)
+			if _, ok := bighash[key]; ok {
+				i--
+			}
+			rdb.HSet(ctx, "bighash", key, val)
+			bighash[key] = val
+		}
+		require.Equal(t, int64(1024), rdb.HLen(ctx, "bighash").Val())
+	})
+
+	t.Run("HSET wrong number of args", func(t *testing.T) {
+		pattern := ".*wrong number.*"
+		res, err := regexp.MatchString(pattern, rdb.HSet(ctx, "hmsetmulti", "key1", "val1", "key2").Err().Error())

Review Comment:
   Can use `util.ErrorRegexp`, as well as other places.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org