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

Code before:
		t.Run(tc.name, func(t *testing.T) {
			FireCommand(conn, tc.setCommand)
			expected := tc.expected
			out := FireCommand(conn, tc.testCommand)

			_, isString := out.(string)
			if isString {
				outInterface := []interface{}{out}
				assert.DeepEqual(t, outInterface, expected)
			} else {
				assert.DeepEqual(t, out.([]interface{}), expected,
					cmpopts.SortSlices(func(a, b interface{}) bool {
						return fmt.Sprintf("%v", a) < fmt.Sprintf("%v", b)
					}))
			}
		})
	}

}
func TestJsonARRTRIM(t *testing.T) {
	conn := getLocalConnection()
	defer conn.Close()
	a := `[0,1,2]`
	b := `{"connection":{"wireless":true,"names":[0,1,2,3,4]},"names":[0,1,2,3,4]}`

	testCases := []struct {
		name       string
		commands   []string
		expected   []interface{}
		assertType []string
	}{
		{
			name:       "JSON.ARRTRIM not array",
			commands:   []string{"JSON.SET b $ " + b, `JSON.ARRTRIM b $ 0 10`, "JSON.GET b"},
			expected:   []interface{}{"OK", []interface{}{"(nil)"}, b},
			assertType: []string{"equal", "deep_equal", "jsoneq"},
		},
		{
			name:       "JSON.ARRTRIM stop index out of bounds",
			commands:   []string{"JSON.SET a $ " + a, `JSON.ARRTRIM a $ -10 10`, "JSON.GET a"},
			expected:   []interface{}{"OK", []interface{}{int64(3)}, "[0,1,2]"},
			assertType: []string{"equal", "deep_equal", "equal"},
		},
		{
			name:       "JSON.ARRTRIM start&stop are postive",
			commands:   []string{"JSON.SET a $ " + a, `JSON.ARRTRIM a $ 1 2`, "JSON.GET a"},
			expected:   []interface{}{"OK", []interface{}{int64(2)}, "[1,2]"},
			assertType: []string{"equal", "deep_equal", "equal"},
		},
		{
			name:       "JSON.ARRTRIM start&stop are negative",

Review comment (suggestion):
Let's add some cleanup code here which will ensure other tests don't interfere with this one, and this test doesn't interfere with them.

```suggestion
	defer conn.Close()
	a := `[0,1,2]`
	b := `{"connection":{"wireless":true,"names":[0,1,2,3,4]},"names":[0,1,2,3,4]}`

        FireCommand(conn, "DEL a b")
        defer FireCommand(conn, "DEL a b")

	testCases := []struct {
```

Code after:
		t.Run(tc.name, func(t *testing.T) {
			FireCommand(conn, tc.setCommand)
			expected := tc.expected
			out := FireCommand(conn, tc.testCommand)

			_, isString := out.(string)
			if isString {
				outInterface := []interface{}{out}
				assert.DeepEqual(t, outInterface, expected)
			} else {
				assert.DeepEqual(t, out.([]interface{}), expected,
					cmpopts.SortSlices(func(a, b interface{}) bool {
						return fmt.Sprintf("%v", a) < fmt.Sprintf("%v", b)
					}))
			}
		})
	}

}
func TestJsonARRTRIM(t *testing.T) {
	conn := getLocalConnection()
	defer conn.Close()
	a := `[0,1,2]`
	b := `{"connection":{"wireless":true,"names":[0,1,2,3,4]},"names":[0,1,2,3,4]}`

	FireCommand(conn, "DEL a b")
	defer FireCommand(conn, "DEL a b")

	testCases := []struct {
		name       string
		commands   []string
		expected   []interface{}
		assertType []string
	}{
		{
			name:       "JSON.ARRTRIM not array",
			commands:   []string{"JSON.SET b $ " + b, `JSON.ARRTRIM b $ 0 10`, "JSON.GET b"},
			expected:   []interface{}{"OK", []interface{}{"(nil)"}, b},
			assertType: []string{"equal", "deep_equal", "jsoneq"},
		},
		{
			name:       "JSON.ARRTRIM stop index out of bounds",
			commands:   []string{"JSON.SET a $ " + a, `JSON.ARRTRIM a $ -10 10`, "JSON.GET a"},
			expected:   []interface{}{"OK", []interface{}{int64(3)}, "[0,1,2]"},
			assertType: []string{"equal", "deep_equal", "equal"},
		},
		{
			name:       "JSON.ARRTRIM start&stop are postive",
			commands:   []string{"JSON.SET a $ " + a, `JSON.ARRTRIM a $ 1 2`, "JSON.GET a"},
			expected:   []interface{}{"OK", []interface{}{int64(2)}, "[1,2]"},
			assertType: []string{"equal", "deep_equal", "equal"},