Repository: DiceDB/dice
File: integration_tests/commands/async/hmget_test.go (Go)

Code before:

	"gotest.tools/v3/assert"
)

func TestHMGET(t *testing.T) {
	conn := getLocalConnection()
	var smallHashKeys []string
	var smallHashValues []string
	var bigHashKeys []string
	var bigHashValues []string
	// Setup smallHash
	for i := 0; i < 8; i++ {
		key := fmt.Sprintf("field%d", i)
		value := fmt.Sprintf("value%d", i)
		smallHashKeys = append(smallHashKeys, key)
		smallHashValues = append(smallHashValues, value)
		FireCommand(conn, "HSET smallHash "+key+" "+value)
	}
	// Setup bigHash
	for i := 0; i < 1024; i++ {
		key := fmt.Sprintf("field%d", i)
		value := fmt.Sprintf("value%d", i)
		bigHashKeys = append(bigHashKeys, key)
		bigHashValues = append(bigHashValues, value)
		FireCommand(conn, "HSET bigHash "+key+" "+value)
	}
	defer conn.Close()
	defer FireCommand(conn, "DEL key_hmGet key_hmGet1 smallHash bigHash")
	testCases := []TestCase{
		{
			name:     "hmget existing keys and fields",
			commands: []string{"HSET key_hmGet field value", "HSET key_hmGet field2 value_new", "HMGET key_hmGet field field2"},
			expected: []interface{}{ONE, ONE, []string{"value", "value_new"}},
		},
		{
			name:     "hmget key does not exist",
			commands: []string{"HMGET doesntexist field"},
			expected: []interface{}{[]interface{}{"(nil)"}},
		},
		{
			name:     "hmget field does not exist in smallHash",
			commands: []string{"HMGET smallHash field"},
			expected: []interface{}{[]interface{}{"(nil)"}},
		},
		{
			name:     "hmget field does not exist in bigHash",
			commands: []string{"HMGET bigHash field"},
			expected: []interface{}{[]interface{}{"(nil)"}},
		},
		{
			name:     "hmget bigHash",

Review comment (bug):
Why do we need so many values? This looks more like a benchmark here. A test should ideally be small, easy to understand, and easy to debug.

Code after:

func TestHMGET(t *testing.T) {
	conn := getLocalConnection()
	defer conn.Close()
	defer FireCommand(conn, "DEL key_hmGet key_hmGet1")

	testCases := []TestCase{
		{
			name:     "hmget existing keys and fields",
			commands: []string{"HSET key_hmGet field value", "HSET key_hmGet field2 value_new", "HMGET key_hmGet field field2"},
			expected: []interface{}{ONE, ONE, []interface{}{"value", "value_new"}},
		},
		{
			name:     "hmget key does not exist",
			commands: []string{"HMGET doesntexist field"},
			expected: []interface{}{[]interface{}{"(nil)"}},
		},
		{
			name:     "hmget field does not exist",
			commands: []string{"HMGET key_hmGet field3"},
			expected: []interface{}{[]interface{}{"(nil)"}},
		},
		{
			name:     "hmget some fields do not exist",
			commands: []string{"HMGET key_hmGet field field2 field3 field3"},
			expected: []interface{}{[]interface{}{"value", "value_new", "(nil)", "(nil)"}},
		},
		{
			name:     "hmget with wrongtype",
			commands: []string{"SET key_hmGet1 field", "HMGET key_hmGet1 field"},
			expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
		},
		{
			name:     "wrong number of arguments",
			commands: []string{"HMGET key_hmGet", "HMGET"},
			expected: []interface{}{"ERR wrong number of arguments for 'hmget' command",
				"ERR wrong number of arguments for 'hmget' command"},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			for i, cmd := range tc.commands {
				// Fire the command and get the result
				result := FireCommand(conn, cmd)
				assert.DeepEqual(t, result, tc.expected[i])
			}
		})
	}
}