Skip to content

Commit

Permalink
refactor: using github.com/stretchr/testify/assert at the place of go…
Browse files Browse the repository at this point in the history
… assert
  • Loading branch information
vpsinghg committed Nov 10, 2024
1 parent f975019 commit 2e7e599
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 76 deletions.
43 changes: 21 additions & 22 deletions integration_tests/commands/http/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"testing"

"github.com/dicedb/dice/testutils"
testifyAssert "github.com/stretchr/testify/assert"
"gotest.tools/v3/assert"
"github.com/stretchr/testify/assert"
)

type IntegrationTestCase struct {
Expand Down Expand Up @@ -59,11 +58,11 @@ func runIntegrationTests(t *testing.T, exec *HTTPCommandExecutor, testCases []In
case "equal":
assert.Equal(t, out, result)
case "perm_equal":
assert.Assert(t, testutils.ArraysArePermutations(testutils.ConvertToArray(out.(string)), testutils.ConvertToArray(result.(string))))
assert.True(t, testutils.ArraysArePermutations(testutils.ConvertToArray(out.(string)), testutils.ConvertToArray(result.(string))))
case "range":
assert.Assert(t, result.(float64) <= out.(float64) && result.(float64) > 0, "Expected %v to be within 0 to %v", result, out)
assert.True(t, result.(float64) <= out.(float64) && result.(float64) > 0, "Expected %v to be within 0 to %v", result, out)
case "json_equal":
testifyAssert.JSONEq(t, out.(string), result.(string))
assert.JSONEq(t, out.(string), result.(string))
}
}
})
Expand Down Expand Up @@ -250,7 +249,7 @@ func TestJSONOperations(t *testing.T) {
result, _ := exec.FireCommand(cmd)

if jsonResult, ok := result.(string); ok && testutils.IsJSONResponse(jsonResult) {
testifyAssert.JSONEq(t, tc.expected[i].(string), jsonResult)
assert.JSONEq(t, tc.expected[i].(string), jsonResult)
} else {
assert.Equal(t, tc.expected[i], result)
}
Expand All @@ -268,7 +267,7 @@ func TestJSONOperations(t *testing.T) {
if jsonResult, ok := result.(string); ok && testutils.IsJSONResponse(jsonResult) {
var jsonPayload []interface{}
json.Unmarshal([]byte(jsonResult), &jsonPayload)
assert.Assert(t, testutils.UnorderedEqual(tc.expected[i], jsonPayload))
assert.True(t, testutils.UnorderedEqual(tc.expected[i], jsonPayload))
} else {
assert.Equal(t, tc.expected[i], result)
}
Expand Down Expand Up @@ -309,7 +308,7 @@ func TestJSONSetWithInvalidCases(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
for i, cmd := range tc.commands {
result, _ := exec.FireCommand(cmd)
assert.Assert(t, strings.HasPrefix(result.(string), tc.expected[i].(string)), fmt.Sprintf("Expected: %s, Got: %s", tc.expected[i], result))
assert.True(t, strings.HasPrefix(result.(string), tc.expected[i].(string)), fmt.Sprintf("Expected: %s, Got: %s", tc.expected[i], result))
}
})
}
Expand Down Expand Up @@ -377,7 +376,7 @@ func TestJSONSetWithNXAndXX(t *testing.T) {
result, _ := exec.FireCommand(cmd)
jsonResult, isString := result.(string)
if isString && testutils.IsJSONResponse(jsonResult) {
testifyAssert.JSONEq(t, tc.expected[i].(string), jsonResult)
assert.JSONEq(t, tc.expected[i].(string), jsonResult)
} else {
assert.Equal(t, tc.expected[i], result)
}
Expand Down Expand Up @@ -869,7 +868,7 @@ func TestJsonStrlen(t *testing.T) {
if stringResult, ok := result.(string); ok {
assert.Equal(t, tc.expected[i], stringResult)
} else {
assert.Assert(t, testutils.UnorderedEqual(tc.expected[i], result.([]interface{})))
assert.True(t, testutils.UnorderedEqual(tc.expected[i], result.([]interface{})))
}
}
})
Expand Down Expand Up @@ -960,7 +959,7 @@ func TestJSONMGET(t *testing.T) {
resultStr, resultIsString := resultVal.(string)

if isString && resultIsString && testutils.IsJSONResponse(expectedStr) {
testifyAssert.JSONEq(t, expectedStr, resultStr)
assert.JSONEq(t, expectedStr, resultStr)
} else {
assert.Equal(t, expectedVal, resultVal)
}
Expand All @@ -976,7 +975,7 @@ func TestJSONMGET(t *testing.T) {
t.Run("MGET with recursive path", func(t *testing.T) {
result, _ := exec.FireCommand(HTTPCommand{Command: "JSON.MGET", Body: map[string]interface{}{"keys": []interface{}{"doc1", "doc2"}, "path": "$..a"}})
results, ok := result.([]interface{})
assert.Assert(t, ok, "Expected result to be a slice of interface{}")
assert.True(t, ok, "Expected result to be a slice of interface{}")
expectedResults := [][]int{{1, 3}, {4, 6}}
assert.Equal(t, len(expectedResults), len(results), "Expected 2 results")

Expand Down Expand Up @@ -1052,9 +1051,9 @@ func TestJsonARRAPPEND(t *testing.T) {

// because the order of keys is not guaranteed, we need to check if the result is an array
if slice, ok := tc.expected[i].([]interface{}); ok {
assert.Assert(t, testutils.UnorderedEqual(slice, result))
assert.True(t, testutils.UnorderedEqual(slice, result))
} else if testutils.IsJSONResponse(tc.expected[i].(string)) {
testifyAssert.JSONEq(t, tc.expected[i].(string), result.(string))
assert.JSONEq(t, tc.expected[i].(string), result.(string))
} else {
assert.Equal(t, tc.expected[i], result)
}
Expand Down Expand Up @@ -1302,7 +1301,7 @@ func TestJsonObjLen(t *testing.T) {
result, _ := exec.FireCommand(cmd)

if slice, ok := tc.expected[i].([]interface{}); ok {
assert.Assert(t, testutils.UnorderedEqual(slice, result))
assert.True(t, testutils.UnorderedEqual(slice, result))
} else {
assert.Equal(t, tc.expected[i], result)
}
Expand Down Expand Up @@ -1493,9 +1492,9 @@ func TestJsonARRINSERT(t *testing.T) {

// because the order of keys is not guaranteed, we need to check if the result is an array
if slice, ok := tc.expected[i].([]interface{}); ok {
assert.Assert(t, testutils.UnorderedEqual(slice, result))
assert.True(t, testutils.UnorderedEqual(slice, result))
} else if testutils.IsJSONResponse(tc.expected[i].(string)) {
testifyAssert.JSONEq(t, tc.expected[i].(string), result.(string))
assert.JSONEq(t, tc.expected[i].(string), result.(string))
} else {
assert.Equal(t, tc.expected[i], result)
}
Expand Down Expand Up @@ -1633,11 +1632,11 @@ func TestJsonObjKeys(t *testing.T) {
if tc.assertType[i] == "equal" {
assert.Equal(t, out, result)
} else if tc.assertType[i] == "perm_equal" {
assert.Assert(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
assert.True(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
} else if tc.assertType[i] == "json_equal" {
testifyAssert.JSONEq(t, out.(string), result.(string))
assert.JSONEq(t, out.(string), result.(string))
} else if tc.assertType[i] == "nested_perm_equal" {
testifyAssert.ElementsMatch(t,
assert.ElementsMatch(t,
sortNestedSlices(out.([]interface{})),
sortNestedSlices(result.([]interface{})),
"Mismatch in JSON object keys",
Expand Down Expand Up @@ -1757,9 +1756,9 @@ func TestJsonARRTRIM(t *testing.T) {
result, _ := exec.FireCommand(cmd)

if slice, ok := tc.expected[i].([]interface{}); ok {
assert.Assert(t, testutils.UnorderedEqual(slice, result))
assert.True(t, testutils.UnorderedEqual(slice, result))
} else if testutils.IsJSONResponse(tc.expected[i].(string)) {
testifyAssert.JSONEq(t, tc.expected[i].(string), result.(string))
assert.JSONEq(t, tc.expected[i].(string), result.(string))
} else {
assert.Equal(t, tc.expected[i], result)
}
Expand Down
8 changes: 4 additions & 4 deletions integration_tests/commands/resp/getwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ func TestGETWATCHWithSDK(t *testing.T) {

for _, channel := range channels {
v := <-channel
assert.Equal(t, "GET", v.Command) // command
assert.Equal(t, "GET", v.Command) // command
assert.Equal(t, "2714318480", v.Fingerprint) // Fingerprint
assert.Equal(t, tc.val, v.Data.(string)) // data
assert.Equal(t, tc.val, v.Data.(string)) // data
}
}
}
Expand Down Expand Up @@ -145,9 +145,9 @@ func TestGETWATCHWithSDK2(t *testing.T) {

for _, channel := range channels {
v := <-channel
assert.Equal(t, "GET", v.Command) // command
assert.Equal(t, "GET", v.Command) // command
assert.Equal(t, "2714318480", v.Fingerprint) // Fingerprint
assert.Equal(t, tc.val, v.Data.(string)) // data
assert.Equal(t, tc.val, v.Data.(string)) // data
}
}
}
33 changes: 16 additions & 17 deletions integration_tests/commands/resp/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"testing"

"github.com/dicedb/dice/testutils"
"gotest.tools/v3/assert"

testifyAssert "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)

type IntegrationTestCase struct {
Expand Down Expand Up @@ -55,11 +54,11 @@ func runIntegrationTests(t *testing.T, conn net.Conn, testCases []IntegrationTes
case "equal":
assert.Equal(t, out, result)
case "perm_equal":
assert.Assert(t, testutils.ArraysArePermutations(testutils.ConvertToArray(out.(string)), testutils.ConvertToArray(result.(string))))
assert.True(t, testutils.ArraysArePermutations(testutils.ConvertToArray(out.(string)), testutils.ConvertToArray(result.(string))))
case "range":
assert.Assert(t, result.(int64) <= out.(int64) && result.(int64) > 0, "Expected %v to be within 0 to %v", result, out)
assert.True(t, result.(int64) <= out.(int64) && result.(int64) > 0, "Expected %v to be within 0 to %v", result, out)
case "json_equal":
testifyAssert.JSONEq(t, out.(string), result.(string))
assert.JSONEq(t, out.(string), result.(string))
}
}
})
Expand Down Expand Up @@ -539,7 +538,7 @@ func TestJsonStrlen(t *testing.T) {
if ok {
assert.Equal(t, tc.expected[i], stringResult)
} else {
assert.Assert(t, testutils.ArraysArePermutations(tc.expected[i].([]interface{}), result.([]interface{})))
assert.True(t, testutils.ArraysArePermutations(tc.expected[i].([]interface{}), result.([]interface{})))
}
}
})
Expand Down Expand Up @@ -743,7 +742,7 @@ func TestJsonObjLen(t *testing.T) {
cmd := tcase.commands[i]
out := tcase.expected[i]
result := FireCommand(conn, cmd)
assert.DeepEqual(t, out, result)
assert.Equal(t, out, result, "Expected out and result to be deeply equal")
}
})
}
Expand Down Expand Up @@ -790,14 +789,14 @@ func TestJSONARRPOP(t *testing.T) {
jsonResult, isString := result.(string)

if isString && testutils.IsJSONResponse(jsonResult) {
testifyAssert.JSONEq(t, out.(string), jsonResult)
assert.JSONEq(t, out.(string), jsonResult)
continue
}

if tcase.assertType[i] == "equal" {
assert.Equal(t, out, result)
} else if tcase.assertType[i] == "deep_equal" {
assert.Assert(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
assert.True(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
}
}
})
Expand Down Expand Up @@ -854,7 +853,7 @@ func TestJsonARRAPPEND(t *testing.T) {
if tcase.assertType[i] == "equal" {
assert.Equal(t, out, result)
} else if tcase.assertType[i] == "deep_equal" {
assert.Assert(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
assert.True(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
}
}
})
Expand Down Expand Up @@ -926,9 +925,9 @@ func TestJsonARRINSERT(t *testing.T) {
if tcase.assertType[i] == "equal" {
assert.Equal(t, out, result)
} else if tcase.assertType[i] == "deep_equal" {
assert.Assert(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
assert.True(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
} else if tcase.assertType[i] == "jsoneq" {
testifyAssert.JSONEq(t, out.(string), result.(string))
assert.JSONEq(t, out.(string), result.(string))
}
}
})
Expand Down Expand Up @@ -1044,11 +1043,11 @@ func TestJsonObjKeys(t *testing.T) {
if tc.assertType[i] == "equal" {
assert.Equal(t, out, result)
} else if tc.assertType[i] == "perm_equal" {
assert.Assert(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
assert.True(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
} else if tc.assertType[i] == "json_equal" {
testifyAssert.JSONEq(t, out.(string), result.(string))
assert.JSONEq(t, out.(string), result.(string))
} else if tc.assertType[i] == "nested_perm_equal" {
testifyAssert.ElementsMatch(t,
assert.ElementsMatch(t,
sortNestedSlices(out.([]interface{})),
sortNestedSlices(result.([]interface{})),
"Mismatch in JSON object keys",
Expand Down Expand Up @@ -1151,9 +1150,9 @@ func TestJsonARRTRIM(t *testing.T) {
if tcase.assertType[i] == "equal" {
assert.Equal(t, out, result)
} else if tcase.assertType[i] == "deep_equal" {
assert.Assert(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
assert.True(t, testutils.ArraysArePermutations(out.([]interface{}), result.([]interface{})))
} else if tcase.assertType[i] == "jsoneq" {
testifyAssert.JSONEq(t, out.(string), result.(string))
assert.JSONEq(t, out.(string), result.(string))
}
}
})
Expand Down
Loading

0 comments on commit 2e7e599

Please sign in to comment.