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

Code before:
			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 {
				result := FireCommand(conn, cmd)
				expectedResults, ok := tc.expected[i].([]string)
				results, ok2 := result.([]interface{})
				if ok && ok2 && len(results) == len(expectedResults) {
					expectedResultsMap := make(map[string]string)
					resultsMap := make(map[string]string)
					for i := 0; i < len(results); i += 2 {
						expectedResultsMap[expectedResults[i]] = expectedResults[i+1]
						resultsMap[results[i].(string)] = results[i+1].(string)
					}
					if !reflect.DeepEqual(resultsMap, expectedResultsMap) {
						t.Fatalf("Assertion failed: expected true, got false")
					}
				} else {
					assert.DeepEqual(t, tc.expected[i], result)
				}
			}
		})
	}
}


Review comment (refactor):
We can format this in a much nicer and readable way instead of using such if conditions.

```go
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)
			
			// Extract expected results and actual results
			expected, isExpectedSlice := tc.expected[i].([]string)
			actual, isActualSlice := result.([]interface{})
			
			// Check if both expected and actual results are slices of the same length
			if isExpectedSlice && isActualSlice && len(expected) == len(actual) {
				expectedMap := mapResults(expected)
				actualMap := mapResults(convertToStringSlice(actual))
				
				// Use assert.DeepEqual to compare the maps
				assert.DeepEqual(t, actualMap, expectedMap)
			} else {
				// Perform a deep equality assertion if the results aren't map-friendly
				assert.DeepEqual(t, result, tc.expected[i])
			}
		}
	})
}

// Helper function to convert a slice of alternating key-value pairs into a map
func mapResults(pairs []string) map[string]string {
	resultMap := make(map[string]string)
	for i := 0; i < len(pairs); i += 2 {
		resultMap[pairs[i]] = pairs[i+1]
	}
	return resultMap
}

// Helper function to convert a slice of interface{} to a slice of strings
func convertToStringSlice(interfaces []interface{}) []string {
	strings := make([]string, len(interfaces))
	for i, v := range interfaces {
		strings[i] = v.(string)
	}
	return strings
}
```

Code after:
package async

import (
	"testing"

	"gotest.tools/v3/assert"
)

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])
			}
		})
	}
}