Skip to content

Commit

Permalink
Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk committed Jan 20, 2024
1 parent 6d35efc commit 63fcf5b
Showing 1 changed file with 53 additions and 25 deletions.
78 changes: 53 additions & 25 deletions tests/gocase/unit/scripting/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,24 @@ type ListFuncResult struct {
}

func decodeListFuncResult(t *testing.T, v interface{}) ListFuncResult {
list, ok := v.([]interface{})
require.True(t, ok)
require.EqualValues(t, 4, len(list))
require.EqualValues(t, "function_name", list[0])
require.EqualValues(t, "from_library", list[2])
return ListFuncResult{
Name: list[1].(string),
Library: list[3].(string),
switch res := v.(type) {
case []interface{}:
require.EqualValues(t, 4, len(res))
require.EqualValues(t, "function_name", res[0])
require.EqualValues(t, "from_library", res[2])
return ListFuncResult{
Name: res[1].(string),
Library: res[3].(string),
}
case map[interface{}]interface{}:
require.EqualValues(t, 2, len(res))
return ListFuncResult{
Name: res["function_name"].(string),
Library: res["from_library"].(string),
}
}
require.Fail(t, "unexpected type")
return ListFuncResult{}
}

type ListLibResult struct {
Expand All @@ -64,22 +73,41 @@ type ListLibResult struct {
}

func decodeListLibResult(t *testing.T, v interface{}) ListLibResult {
list, ok := v.([]interface{})
require.True(t, ok)
require.EqualValues(t, 6, len(list))
require.EqualValues(t, "library_name", list[0])
require.EqualValues(t, "engine", list[2])
require.EqualValues(t, "functions", list[4])

return ListLibResult{
Name: list[1].(string),
Engine: list[3].(string),
Functions: list[5].([]interface{}),
switch res := v.(type) {
case []interface{}:
require.EqualValues(t, 6, len(res))
require.EqualValues(t, "library_name", res[0])
require.EqualValues(t, "engine", res[2])
require.EqualValues(t, "functions", res[4])
return ListLibResult{
Name: res[1].(string),
Engine: res[3].(string),
Functions: res[5].([]interface{}),
}
case map[interface{}]interface{}:
require.EqualValues(t, 3, len(res))
return ListLibResult{
Name: res["library_name"].(string),
Engine: res["engine"].(string),
Functions: res["functions"].([]interface{}),
}
}
require.Fail(t, "unexpected type")
return ListLibResult{}
}

func TestFunction(t *testing.T) {
srv := util.StartServer(t, map[string]string{})
func TestFunctionsWithRESP3(t *testing.T) {
testFunctions(t, "yes")
}

func TestFunctionsWithoutRESP2(t *testing.T) {
testFunctions(t, "no")
}

var testFunctions = func(t *testing.T, enabledRESP3 string) {
srv := util.StartServer(t, map[string]string{
"resp3-enabled": enabledRESP3,
})
defer srv.Close()

ctx := context.Background()
Expand Down Expand Up @@ -238,14 +266,14 @@ func TestFunction(t *testing.T) {
})

t.Run("FUNCTION LISTLIB", func(t *testing.T) {
list := rdb.Do(ctx, "FUNCTION", "LISTLIB", "mylib1").Val().([]interface{})
r := rdb.Do(ctx, "FUNCTION", "LISTLIB", "mylib1").Val()
require.EqualValues(t, ListLibResult{
Name: "mylib1", Engine: "lua", Functions: []interface{}{"hello", "reverse"},
}, decodeListLibResult(t, list))
}, decodeListLibResult(t, r))

list = rdb.Do(ctx, "FUNCTION", "LISTLIB", "mylib3").Val().([]interface{})
r = rdb.Do(ctx, "FUNCTION", "LISTLIB", "mylib3").Val()
require.EqualValues(t, ListLibResult{
Name: "mylib3", Engine: "lua", Functions: []interface{}{"myget", "myset"},
}, decodeListLibResult(t, list))
}, decodeListLibResult(t, r))
})
}

0 comments on commit 63fcf5b

Please sign in to comment.