You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by ti...@apache.org on 2022/09/09 07:30:11 UTC
[incubator-kvrocks] branch unstable updated: Move TCL test unit/type/incr to Go case (#837)
This is an automated email from the ASF dual-hosted git repository.
tison 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 bb6264b Move TCL test unit/type/incr to Go case (#837)
bb6264b is described below
commit bb6264b56410adb1239d3abbc684cbcd34088778
Author: Ruixiang Tan <81...@qq.com>
AuthorDate: Fri Sep 9 15:30:05 2022 +0800
Move TCL test unit/type/incr to Go case (#837)
Co-authored-by: tison <wa...@gmail.com>
---
tests/gocase/unit/type/incr/incr_test.go | 151 ++++++++++++++++++++++++++++++
tests/gocase/util/assertions.go | 31 +++++++
tests/gocase/util/constants.go | 22 +++++
tests/tcl/tests/test_helper.tcl | 1 -
tests/tcl/tests/unit/type/incr.tcl | 155 -------------------------------
5 files changed, 204 insertions(+), 156 deletions(-)
diff --git a/tests/gocase/unit/type/incr/incr_test.go b/tests/gocase/unit/type/incr/incr_test.go
new file mode 100644
index 0000000..d16fff3
--- /dev/null
+++ b/tests/gocase/unit/type/incr/incr_test.go
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package incr
+
+import (
+ "context"
+ "testing"
+
+ "github.com/apache/incubator-kvrocks/tests/gocase/util"
+ "github.com/stretchr/testify/require"
+)
+
+func TestIncr(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()) }()
+
+ t.Run("INCR against non existing key", func(t *testing.T) {
+ require.EqualValues(t, 1, rdb.Incr(ctx, "novar").Val())
+ require.EqualValues(t, "1", rdb.Get(ctx, "novar").Val())
+ })
+
+ t.Run("INCR against key created by incr itself", func(t *testing.T) {
+ require.EqualValues(t, 2, rdb.Incr(ctx, "novar").Val())
+ })
+
+ t.Run("INCR against key originally set with SET", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", 100, 0).Err())
+ require.EqualValues(t, 101, rdb.Incr(ctx, "novar").Val())
+ })
+
+ t.Run("INCR over 32bit value", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", 17179869184, 0).Err())
+ require.EqualValues(t, 17179869185, rdb.Incr(ctx, "novar").Val())
+ })
+
+ t.Run("INCRBY over 32bit value with over 32bit increment", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", 17179869184, 0).Err())
+ require.EqualValues(t, 34359738368, rdb.IncrBy(ctx, "novar", 17179869184).Val())
+ })
+
+ t.Run("INCR fails against key with spaces (left)", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", " 11", 0).Err())
+ util.ErrorRegexp(t, rdb.Incr(ctx, "novar").Err(), "ERR.*")
+ })
+
+ t.Run("INCR fails against key with spaces (right)", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", "11 ", 0).Err())
+ util.ErrorRegexp(t, rdb.Incr(ctx, "novar").Err(), "ERR.*")
+ })
+
+ t.Run("INCR fails against key with spaces (both)", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", " 11 ", 0).Err())
+ util.ErrorRegexp(t, rdb.Incr(ctx, "novar").Err(), "ERR.*")
+ })
+
+ t.Run("INCR fails against a key holding a list", func(t *testing.T) {
+ require.NoError(t, rdb.RPush(ctx, "mylist", 1).Err())
+ require.ErrorContains(t, rdb.Incr(ctx, "mylist").Err(), "WRONGTYPE")
+ require.NoError(t, rdb.RPop(ctx, "mylist").Err())
+ })
+
+ t.Run("DECRBY over 32bit value with over 32bit increment, negative res", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", 17179869184, 0).Err())
+ require.EqualValues(t, -1, rdb.DecrBy(ctx, "novar", 17179869185).Val())
+ })
+
+ t.Run("INCRBYFLOAT against non existing key", func(t *testing.T) {
+ require.NoError(t, rdb.Del(ctx, "novar").Err())
+ require.EqualValues(t, 1, rdb.IncrByFloat(ctx, "novar", 1.0).Val())
+ r, err := rdb.Get(ctx, "novar").Float64()
+ require.NoError(t, err)
+ require.EqualValues(t, 1, r)
+ require.EqualValues(t, 1.25, rdb.IncrByFloat(ctx, "novar", 0.25).Val())
+ r, err = rdb.Get(ctx, "novar").Float64()
+ require.NoError(t, err)
+ require.EqualValues(t, 1.25, r)
+ })
+
+ t.Run("INCRBYFLOAT against key originally set with SET", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", 1.5, 0).Err())
+ require.EqualValues(t, 3, rdb.IncrByFloat(ctx, "novar", 1.5).Val())
+ })
+
+ t.Run("INCRBYFLOAT over 32bit value", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", 17179869184, 0).Err())
+ require.EqualValues(t, 17179869185.5, rdb.IncrByFloat(ctx, "novar", 1.5).Val())
+ })
+
+ t.Run("INCRBYFLOAT over 32bit value with over 32bit increment", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", 17179869184, 0).Err())
+ require.EqualValues(t, 34359738368, rdb.IncrByFloat(ctx, "novar", 17179869184).Val())
+ })
+
+ t.Run("INCRBYFLOAT fails against key with spaces (left)", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", " 11", 0).Err())
+ util.ErrorRegexp(t, rdb.IncrByFloat(ctx, "novar", 1.0).Err(), "ERR.*valid.*")
+ })
+
+ t.Run("INCRBYFLOAT fails against key with spaces (right)", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", "11 ", 0).Err())
+ util.ErrorRegexp(t, rdb.IncrByFloat(ctx, "novar", 1.0).Err(), "ERR.*valid.*")
+ })
+
+ t.Run("INCRBYFLOAT fails against key with spaces (both)", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "novar", " 11 ", 0).Err())
+ util.ErrorRegexp(t, rdb.IncrByFloat(ctx, "novar", 1.0).Err(), "ERR.*valid.*")
+ })
+
+ t.Run("INCRBYFLOAT fails against a key holding a list", func(t *testing.T) {
+ require.NoError(t, rdb.Del(ctx, "mylist").Err())
+ require.NoError(t, rdb.RPush(ctx, "mylist", 1).Err())
+ require.ErrorContains(t, rdb.IncrByFloat(ctx, "mylist", 1.0).Err(), "WRONGTYPE")
+ require.NoError(t, rdb.Del(ctx, "mylist").Err())
+ })
+
+ t.Run("INCRBYFLOAT does not allow NaN or Infinity", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "foo", 0, 0).Err())
+ util.ErrorRegexp(t, rdb.Do(ctx, "INCRBYFLOAT", "foo", "+inf").Err(), "ERR.*would produce.*")
+ })
+
+ t.Run("INCRBYFLOAT decrement", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "foo", 1, 0).Err())
+ require.InDelta(t, -0.1, rdb.IncrByFloat(ctx, "foo", -1.1).Val(), util.DefaultDelta)
+ })
+
+ t.Run("string to double with null terminator", func(t *testing.T) {
+ require.NoError(t, rdb.Set(ctx, "foo", 1, 0).Err())
+ require.NoError(t, rdb.SetRange(ctx, "foo", 2, "2").Err())
+ util.ErrorRegexp(t, rdb.IncrByFloat(ctx, "foo", 1.0).Err(), "ERR.*valid.*")
+ })
+}
diff --git a/tests/gocase/util/assertions.go b/tests/gocase/util/assertions.go
new file mode 100644
index 0000000..3e6f9a1
--- /dev/null
+++ b/tests/gocase/util/assertions.go
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package util
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func ErrorRegexp(t testing.TB, err error, rx interface{}, msgAndArgs ...interface{}) {
+ require.Error(t, err, msgAndArgs)
+ require.Regexp(t, rx, err.Error())
+}
diff --git a/tests/gocase/util/constants.go b/tests/gocase/util/constants.go
new file mode 100644
index 0000000..9c90e7e
--- /dev/null
+++ b/tests/gocase/util/constants.go
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package util
+
+const DefaultDelta = 0.000001
diff --git a/tests/tcl/tests/test_helper.tcl b/tests/tcl/tests/test_helper.tcl
index 76c9c5d..ab97b52 100644
--- a/tests/tcl/tests/test_helper.tcl
+++ b/tests/tcl/tests/test_helper.tcl
@@ -36,7 +36,6 @@ set ::all_tests {
unit/keyspace
unit/scan
unit/type/string
- unit/type/incr
unit/type/list
unit/type/list-3
unit/type/set
diff --git a/tests/tcl/tests/unit/type/incr.tcl b/tests/tcl/tests/unit/type/incr.tcl
deleted file mode 100644
index e551f33..0000000
--- a/tests/tcl/tests/unit/type/incr.tcl
+++ /dev/null
@@ -1,155 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-
-# Copyright (c) 2006-2020, Salvatore Sanfilippo
-# See bundled license file licenses/LICENSE.redis for details.
-
-# This file is copied and modified from the Redis project,
-# which started out as: https://github.com/redis/redis/blob/dbcc0a8/tests/unit/type/incr.tcl
-
-start_server {tags {"incr"}} {
- test {INCR against non existing key} {
- set res {}
- append res [r incr novar]
- append res [r get novar]
- } {11}
-
- test {INCR against key created by incr itself} {
- r incr novar
- } {2}
-
- test {INCR against key originally set with SET} {
- r set novar 100
- r incr novar
- } {101}
-
- test {INCR over 32bit value} {
- r set novar 17179869184
- r incr novar
- } {17179869185}
-
- test {INCRBY over 32bit value with over 32bit increment} {
- r set novar 17179869184
- r incrby novar 17179869184
- } {34359738368}
-
- test {INCR fails against key with spaces (left)} {
- r set novar " 11"
- catch {r incr novar} err
- format $err
- } {ERR*}
-
- test {INCR fails against key with spaces (right)} {
- r set novar "11 "
- catch {r incr novar} err
- format $err
- } {ERR*}
-
- test {INCR fails against key with spaces (both)} {
- r set novar " 11 "
- catch {r incr novar} err
- format $err
- } {ERR*}
-
- test {INCR fails against a key holding a list} {
- r rpush mylist 1
- catch {r incr mylist} err
- r rpop mylist
- format $err
- } {*WRONGTYPE*}
-
- test {DECRBY over 32bit value with over 32bit increment, negative res} {
- r set novar 17179869184
- r decrby novar 17179869185
- } {-1}
-
- test {INCRBYFLOAT against non existing key} {
- r del novar
- list [roundFloat [r incrbyfloat novar 1]] \
- [roundFloat [r get novar]] \
- [roundFloat [r incrbyfloat novar 0.25]] \
- [roundFloat [r get novar]]
- } {1 1 1.25 1.25}
-
- test {INCRBYFLOAT against key originally set with SET} {
- r set novar 1.5
- roundFloat [r incrbyfloat novar 1.5]
- } {3}
-
- test {INCRBYFLOAT over 32bit value} {
- r set novar 17179869184
- r incrbyfloat novar 1.5
- } {17179869185.5}
-
- test {INCRBYFLOAT over 32bit value with over 32bit increment} {
- r set novar 17179869184
- r incrbyfloat novar 17179869184
- } {34359738368}
-
- test {INCRBYFLOAT fails against key with spaces (left)} {
- set err {}
- r set novar " 11"
- catch {r incrbyfloat novar 1.0} err
- format $err
- } {ERR*valid*}
-
- test {INCRBYFLOAT fails against key with spaces (right)} {
- set err {}
- r set novar "11 "
- catch {r incrbyfloat novar 1.0} err
- format $err
- } {ERR*valid*}
-
- test {INCRBYFLOAT fails against key with spaces (both)} {
- set err {}
- r set novar " 11 "
- catch {r incrbyfloat novar 1.0} err
- format $err
- } {ERR*valid*}
-
- test {INCRBYFLOAT fails against a key holding a list} {
- r del mylist
- set err {}
- r rpush mylist 1
- catch {r incrbyfloat mylist 1.0} err
- r del mylist
- format $err
- } {*WRONGTYPE*}
-
- test {INCRBYFLOAT does not allow NaN or Infinity} {
- r set foo 0
- set err {}
- catch {r incrbyfloat foo +inf} err
- set err
- # p.s. no way I can force NaN to test it from the API because
- # there is no way to increment / decrement by infinity nor to
- # perform divisions.
- } {ERR*would produce*}
-
- # TODO: INCRBYFLOAT precision for human friendly
- # test {INCRBYFLOAT decrement} {
- # r set foo 1
- # roundFloat [r incrbyfloat foo -1.1]
- # } {-0.1}
-
- test {string to double with null terminator} {
- r set foo 1
- r setrange foo 2 2
- catch {r incrbyfloat foo 1} err
- format $err
- } {ERR*valid*}
-}