Skip to content

Commit

Permalink
Move create of client in tests to helper function
Browse files Browse the repository at this point in the history
Removed duplicated code in tests using helper function.

Signed-off-by: Wojciech Szarański <wojciech.szaranski@gmail.com>
  • Loading branch information
wszaranski authored and alicebob committed Mar 27, 2024
1 parent 0dd44a9 commit 889458a
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 778 deletions.
10 changes: 2 additions & 8 deletions cmd_client_test.go
Expand Up @@ -9,10 +9,7 @@ import (
// Test CLIENT *.
func TestClient(t *testing.T) {
t.Run("setname and getname", func(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
_, c := runWithClient(t)

// Set the client name
mustDo(t, c,
Expand All @@ -28,10 +25,7 @@ func TestClient(t *testing.T) {
})

t.Run("getname without setname", func(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
_, c := runWithClient(t)

// Get the client name without setting it first
mustDo(t, c,
Expand Down
5 changes: 1 addition & 4 deletions cmd_cluster_test.go
Expand Up @@ -9,10 +9,7 @@ import (

// Test CLUSTER *.
func TestCluster(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("slots", func(t *testing.T) {
port, err := strconv.Atoi(s.Port())
Expand Down
50 changes: 10 additions & 40 deletions cmd_connection_test.go
Expand Up @@ -8,10 +8,7 @@ import (

func TestAuth(t *testing.T) {
t.Run("default user", func(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

mustDo(t, c,
"AUTH", "foo", "bar", "baz",
Expand All @@ -38,10 +35,7 @@ func TestAuth(t *testing.T) {
})

t.Run("another user", func(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

s.RequireUserAuth("hello", "world")
mustDo(t, c,
Expand All @@ -67,10 +61,7 @@ func TestAuth(t *testing.T) {
})

t.Run("error cases", func(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
_, c := runWithClient(t)

mustDo(t, c,
"AUTH",
Expand All @@ -85,10 +76,7 @@ func TestAuth(t *testing.T) {
}

func TestPing(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
_, c := runWithClient(t)

t.Run("no args", func(t *testing.T) {
mustDo(t, c,
Expand All @@ -113,10 +101,7 @@ func TestPing(t *testing.T) {
}

func TestEcho(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
_, c := runWithClient(t)

mustDo(t, c,
"ECHO", "hello\nworld",
Expand All @@ -130,10 +115,7 @@ func TestEcho(t *testing.T) {
}

func TestSelect(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

mustOK(t, c, "SET", "foo", "bar")
mustOK(t, c, "SELECT", "5")
Expand Down Expand Up @@ -161,10 +143,7 @@ func TestSelect(t *testing.T) {
}

func TestSwapdb(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

mustOK(t, c, "SET", "foo", "bar")
mustOK(t, c, "SELECT", "5")
Expand Down Expand Up @@ -224,10 +203,7 @@ func TestSwapdb(t *testing.T) {
}

func TestQuit(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
_, c := runWithClient(t)

mustOK(t, c, "QUIT")

Expand All @@ -237,10 +213,7 @@ func TestQuit(t *testing.T) {
}

func TestSetError(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

mustDo(t, c,
"PING",
Expand All @@ -262,10 +235,7 @@ func TestSetError(t *testing.T) {

func TestHello(t *testing.T) {
t.Run("default user", func(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

payl := proto.Map(
proto.String("server"), proto.String("miniredis"),
Expand Down
90 changes: 18 additions & 72 deletions cmd_generic_test.go
Expand Up @@ -10,10 +10,7 @@ import (

// Test EXPIRE. Keys with an expiration are called volatile in Redis parlance.
func TestTTL(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("parse", func(t *testing.T) {
t.Run("basic", func(t *testing.T) {
Expand Down Expand Up @@ -102,10 +99,7 @@ func TestTTL(t *testing.T) {
}

func TestExpireat(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

// Not volatile yet
{
Expand Down Expand Up @@ -136,10 +130,7 @@ func TestExpireat(t *testing.T) {
}

func TestTouch(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

// Set something
t.Run("basic", func(t *testing.T) {
Expand Down Expand Up @@ -176,10 +167,7 @@ func TestTouch(t *testing.T) {
}

func TestPexpireat(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

// Not volatile yet
{
Expand Down Expand Up @@ -212,10 +200,7 @@ func TestPexpireat(t *testing.T) {
}

func TestPexpire(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("key exists", func(t *testing.T) {
ok(t, s.Set("foo", "bar"))
Expand Down Expand Up @@ -246,10 +231,7 @@ func TestPexpire(t *testing.T) {
}

func TestDel(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("simple", func(t *testing.T) {
s.Set("foo", "bar")
Expand Down Expand Up @@ -281,10 +263,7 @@ func TestDel(t *testing.T) {
}

func TestUnlink(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("simple", func(t *testing.T) {
s.Set("foo", "bar")
Expand All @@ -309,10 +288,7 @@ func TestUnlink(t *testing.T) {
}

func TestType(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

s.Set("foo", "bar!")
t.Run("string", func(t *testing.T) {
Expand Down Expand Up @@ -355,10 +331,7 @@ func TestType(t *testing.T) {
}

func TestExpireTime(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("nosuch", func(t *testing.T) {
mustDo(t, c, "EXPIRETIME", "nosuch", proto.Int(-2))
Expand All @@ -379,10 +352,7 @@ func TestExpireTime(t *testing.T) {
}

func TestPExpireTime(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("nosuch", func(t *testing.T) {
mustDo(t, c, "PEXPIRETIME", "nosuch", proto.Int(-2))
Expand All @@ -403,10 +373,7 @@ func TestPExpireTime(t *testing.T) {
}

func TestExists(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("string", func(t *testing.T) {
s.Set("foo", "bar!")
Expand Down Expand Up @@ -448,10 +415,7 @@ func TestExists(t *testing.T) {
}

func TestMove(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

// No problem.
{
Expand Down Expand Up @@ -501,10 +465,7 @@ func TestMove(t *testing.T) {
}

func TestKeys(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

s.Set("foo", "bar!")
s.Set("foobar", "bar!")
Expand Down Expand Up @@ -547,10 +508,7 @@ func TestKeys(t *testing.T) {
}

func TestRandom(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

// Empty db.
mustNil(t, c, "RANDOMKEY")
Expand All @@ -575,10 +533,7 @@ func TestRandom(t *testing.T) {
}

func TestRename(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

// Non-existing key
mustDo(t, c,
Expand Down Expand Up @@ -652,10 +607,7 @@ func TestRename(t *testing.T) {
}

func TestScan(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("parse", func(t *testing.T) {
t.Run("basic", func(t *testing.T) {
Expand Down Expand Up @@ -816,10 +768,7 @@ func TestScan(t *testing.T) {
}

func TestRenamenx(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

// Non-existing key
mustDo(t, c,
Expand Down Expand Up @@ -871,10 +820,7 @@ func TestRenamenx(t *testing.T) {
}

func TestCopy(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()
s, c := runWithClient(t)

t.Run("parse", func(t *testing.T) {
t.Run("basic", func(t *testing.T) {
Expand Down

0 comments on commit 889458a

Please sign in to comment.