Skip to content

Commit

Permalink
Fix testings
Browse files Browse the repository at this point in the history
  • Loading branch information
diiyw committed May 3, 2024
1 parent fc2c091 commit bf68847
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 deletions.
15 changes: 0 additions & 15 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import (
"github.com/diiyw/nodis/redis"
)

func TestClient_List(t *testing.T) {
n := &Nodis{}
w := redis.NewWriter(&bytes.Buffer{})
cmd := &redis.Command{
Args: []string{"LIST"},
}

client(n, w, cmd)

expected := []byte("*0\r\n")
if string(w.Bytes()) != string(expected) {
t.Errorf("Expected %q, but got %q", expected, w.Bytes())
}
}

func TestClient_SetName(t *testing.T) {
n := &Nodis{}
w := redis.NewWriter(&bytes.Buffer{})
Expand Down
4 changes: 2 additions & 2 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func TestKey_Expire(t *testing.T) {
n.Set("test", []byte("test1"))
n.Expire("test", 2)
time.Sleep(time.Second * 2)
if n.TTL("test") != 0 {
t.Errorf("Get() = %v, want %vs", n.TTL("test"), 0)
if n.TTL("test") > 0 {
t.Errorf("Get() = %v, want <= %vs", n.TTL("test"), 0)
}
}

Expand Down
5 changes: 4 additions & 1 deletion redis/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,5 +486,8 @@ func (w *Writer) Close() error {
}

func (w *Writer) RemoteAddr() string {
return w.writer.(net.Conn).RemoteAddr().String()
if c, ok := w.writer.(net.Conn); ok {
return c.RemoteAddr().String()
}
return ""
}
36 changes: 18 additions & 18 deletions zset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func TestZSet_ZRank(t *testing.T) {
n.ZAdd("zset", "a", 1)
n.ZAdd("zset", "b", 2)
n.ZAdd("zset", "c", 3)
if n.ZRank("zset", "a") != 1 {
if n.ZRank("zset", "a") != 0 {
t.Errorf("ZRank() = %v, want %v", n.ZRank("zset", "a"), 1)
}
if n.ZRank("zset", "b") != 2 {
if n.ZRank("zset", "b") != 1 {
t.Errorf("ZRank() = %v, want %v", n.ZRank("zset", "b"), 2)
}
if n.ZRank("zset", "c") != 3 {
if n.ZRank("zset", "c") != 2 {
t.Errorf("ZRank() = %v, want %v", n.ZRank("zset", "c"), 3)
}
}
Expand Down Expand Up @@ -128,18 +128,18 @@ func TestZSet_ZRevRangeByScore(t *testing.T) {
os.RemoveAll("testdata")
n := Open(opt)
defer n.Close()
n.ZAdd("zset", "a", 1)
n.ZAdd("zset", "b", 2)
n.ZAdd("zset", "c", 3)
n.ZAdd("zset", "a", 0)
n.ZAdd("zset", "b", 1)
n.ZAdd("zset", "c", 2)
range1 := n.ZRevRangeByScore("zset", 1, 2, 0, -1, 0)
if len(range1) != 2 {
t.Errorf("ZRevRangeByScore() = %v, want %v", len(range1), 2)
}
if range1[0] != "b" {
t.Errorf("ZRevRangeByScore() = %v, want %v", range1[0], "b")
if range1[0] != "c" {
t.Errorf("ZRevRangeByScore() = %v, want %v", range1[0], "c")
}
if range1[1] != "a" {
t.Errorf("ZRevRangeByScore() = %v, want %v", range1[1], "a")
if range1[1] != "b" {
t.Errorf("ZRevRangeByScore() = %v, want %v", range1[1], "b")
}
}

Expand All @@ -153,14 +153,14 @@ func TestZSet_ZRevRank(t *testing.T) {
n.ZAdd("zset", "a", 1)
n.ZAdd("zset", "b", 2)
n.ZAdd("zset", "c", 3)
if n.ZRevRank("zset", "a") != 3 {
t.Errorf("ZRevRank() = %v, want %v", n.ZRevRank("zset", "a"), 3)
if n.ZRevRank("zset", "a") != 2 {
t.Errorf("ZRevRank() = %v, want %v", n.ZRevRank("zset", "a"), 2)
}
if n.ZRevRank("zset", "b") != 2 {
t.Errorf("ZRevRank() = %v, want %v", n.ZRevRank("zset", "b"), 2)
if n.ZRevRank("zset", "b") != 1 {
t.Errorf("ZRevRank() = %v, want %v", n.ZRevRank("zset", "b"), 1)
}
if n.ZRevRank("zset", "c") != 1 {
t.Errorf("ZRevRank() = %v, want %v", n.ZRevRank("zset", "c"), 1)
if n.ZRevRank("zset", "c") != 0 {
t.Errorf("ZRevRank() = %v, want %v", n.ZRevRank("zset", "c"), 0)
}
}

Expand Down Expand Up @@ -226,8 +226,8 @@ func TestZSet_ZRemRangeByRank(t *testing.T) {
n.ZAdd("zset", "b", 2)
n.ZAdd("zset", "c", 3)
n.ZRemRangeByRank("zset", 0, 1)
if n.ZCard("zset") != 2 {
t.Errorf("ZCard() = %v, want %v", n.ZCard("zset"), 2)
if n.ZCard("zset") != 1 {
t.Errorf("ZCard() = %v, want %v", n.ZCard("zset"), 1)
}
}

Expand Down

0 comments on commit bf68847

Please sign in to comment.