-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
karthick
committed
Aug 11, 2024
1 parent
c25c000
commit 65ddeb8
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestCommandRandomKey(t *testing.T) { | ||
var randomKeyTestCases = []struct { | ||
name string | ||
inCmd []string | ||
expected []interface{} | ||
}{ | ||
{ | ||
name: "Try to get RandomKey with 0 keys in map", | ||
inCmd: []string{"RANDOMKEY"}, | ||
expected: []interface{}{"(nil)"}, | ||
}, | ||
{ | ||
name: "Set a key and get a RandomKey", | ||
inCmd: []string{"set Key hello", "RANDOMKEY"}, | ||
expected: []interface{}{"OK", "Key"}, | ||
}, | ||
{ | ||
name: "Set another two keys and check the RandomKey", | ||
inCmd: []string{"set Key2 hello2", "set Key3 hello3", "RANDOMKEY"}, | ||
expected: []interface{}{"OK", "OK"}, | ||
}, | ||
} | ||
|
||
conn := getLocalConnection() | ||
defer conn.Close() | ||
|
||
for _, tc := range randomKeyTestCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var actual []interface{} | ||
for _, cmd := range tc.inCmd { | ||
result := fireCommand(conn, cmd) | ||
actual = append(actual, result) | ||
} | ||
|
||
if tc.name == "Set another two keys and check the RandomKey" { | ||
// Manually check if the last result is one of the expected keys | ||
lastResult := actual[len(actual)-1] | ||
expectedKeys := []string{"Key", "Key2", "Key3"} | ||
found := false | ||
for _, key := range expectedKeys { | ||
if lastResult == key { | ||
found = true | ||
break | ||
} | ||
} | ||
assert.Assert(t, found, "Expected one of %v, but got %v", expectedKeys, lastResult) | ||
} else { | ||
assert.DeepEqual(t, tc.expected, actual) | ||
} | ||
}) | ||
} | ||
} |