command_docs_test
File Name: command_docs_test.go
1. Overview
This file contains test cases and benchmarks for the COMMAND DOCS
functionality, which retrieves documentation for specific commands. It tests various scenarios, including valid and invalid commands, and combinations thereof.
2. Detailed Documentation
var getDocsTestCases
var getDocsTestCases = []struct {
name string
inCmd string
expected interface{}
skipExpectedMatch bool
}{
{"Without any commands", "", []any{}, true},
{"Set command", "SET", []interface{}{[]interface{}{
"set",
[]interface{}{
"summary",
string("SET puts a new <key, value> pair in db as in the args\n\t\targs must contain key and value.\n\t\targs can also contain multiple options -\n\t\tEX or ex which will set the expiry time(in secs) for the key\n\t\tReturns encoded error response if at least a <key, value> pair is not part of args\n\t\tReturns encoded error response if expiry tme value in not integer\n\t\tReturns encoded OK RESP once new entry is added\n\t\tIf the key already exists then the value will be overwritten and expiry will be discarded"),
"arity", int64(-3),
"beginIndex", int64(1),
"lastIndex", int64(0),
"step", int64(0),
},
}}, false},
{"Get command", "GET", []interface{}{[]interface{}{
"get",
[]interface{}{
"summary",
string("GET returns the value for the queried key in args\n\t\tThe key should be the only param in args\n\t\tThe RESP value of the key is encoded and then returned\n\t\tGET returns RespNIL if key is expired or it does not exist"),
"arity", int64(2),
"beginIndex", int64(1),
"lastIndex", int64(0),
"step", int64(0),
},
}}, false},
{"Ping command", "PING", []interface{}{[]interface{}{
"ping",
[]interface{}{
"summary",
string(`PING returns with an encoded "PONG" If any message is added with the ping command,the message will be returned.`),
"arity", int64(-1),
"beginIndex", int64(0),
"lastIndex", int64(0),
"step", int64(0),
},
}}, false},
{"Invalid command", "INVALID_CMD",
[]any{},
false,
},
{"Combination of valid and Invalid command", "SET INVALID_CMD", []interface{}{[]interface{}{
"set",
[]interface{}{
"summary",
string("SET puts a new <key, value> pair in db as in the args\n\t\targs must contain key and value.\n\t\targs can also contain multiple options -\n\t\tEX or ex which will set the expiry time(in secs) for the key\n\t\tReturns encoded error response if at least a <key, value> pair is not part of args\n\t\tReturns encoded error response if expiry tme value in not integer\n\t\tReturns encoded OK RESP once new entry is added\n\t\tIf the key already exists then the value will be overwritten and expiry will be discarded"),
"arity", int64(-3),
"beginIndex", int64(1),
"lastIndex", int64(0),
"step", int64(0),
}}}, false},
{"Combination of multiple valid commands", "SET GET", []interface{}{[]interface{}{
"set",
[]interface{}{
"summary",
string("SET puts a new <key, value> pair in db as in the args\n\t\targs must contain key and value.\n\t\targs can also contain multiple options -\n\t\tEX or ex which will set the expiry time(in secs) for the key\n\t\tReturns encoded error response if at least a <key, value> pair is not part of args\n\t\tReturns encoded error response if expiry tme value in not integer\n\t\tReturns encoded OK RESP once new entry is added\n\t\tIf the key already exists then the value will be overwritten and expiry will be discarded"),
"arity", int64(-3),
"beginIndex", int64(1),
"lastIndex", int64(0),
"step", int64(0),
}},
[]interface{}{"get",
[]interface{}{
"summary",
string("GET returns the value for the queried key in args\n\t\tThe key should be the only param in args\n\t\tThe RESP value of the key is encoded and then returned\n\t\tGET returns RespNIL if key is expired or it does not exist"),
"arity", int64(2),
"beginIndex", int64(1),
"lastIndex", int64(0),
"step", int64(0),
},
}}, false},
}
- Purpose: Defines a set of test cases for the
COMMAND DOCS
functionality. Each test case includes a name, the input command string, the expected result, and a flag to indicate whether the expected result should be skipped from direct matching.name
: The name of the test case.inCmd
: The input command string to test.expected
: The expected result of theCOMMAND DOCS
command.skipExpectedMatch
: A boolean indicating whether to skip direct matching of the expected result.
func TestCommandDocs(t *testing.T)
func TestCommandDocs(t *testing.T) {
client := getLocalConnection()
defer client.Close()
for _, tc := range getDocsTestCases {
t.Run(tc.name, func(t *testing.T) {
result := client.FireString("COMMAND DOCS " + tc.inCmd)
if !tc.skipExpectedMatch {
assert.Equal(t, tc.expected, result)
} else {
assert.NotNil(t, result)
ok := result.GetVStr() != ""
assert.True(t, ok)
assert.True(t, len(result.GetVStr()) > 0)
}
})
}
}
- Purpose: Tests the
COMMAND DOCS
functionality by iterating through thegetDocsTestCases
.- Parameters:
t
: A testing object.
- Returns: None.
- Parameters:
func BenchmarkCommandDocs(b *testing.B)
func BenchmarkCommandDocs(b *testing.B) {
client := getLocalConnection()
defer client.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, tc := range getDocsTestCases {
client.FireString("COMMAND DOCS " + tc.inCmd)
}
}
}
- Purpose: Benchmarks the performance of the
COMMAND DOCS
functionality.- Parameters:
b
: A benchmark object.
- Returns: None.
- Parameters:
3. Code Examples
None.