Skip to content

Commit

Permalink
Adding customJSON comparer to RM tests (#2393)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbdias authored Apr 14, 2023
1 parent 55c28a6 commit fa45a49
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
31 changes: 18 additions & 13 deletions server/resourcemanager/testutil/common_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@ func generateRandomString() string {
return generator.TraceID().String()
}

func removeID(input map[string]any) map[string]any {
func RemoveFieldFromJSONResource(field, jsonResource string) string {
resourceAsMap := parseJSON(jsonResource)

clean := removeFieldFromResource(field, resourceAsMap)

out, err := json.Marshal(clean)
if err != nil {
panic(err)
}
return string(out)
}

func removeIDFromJSON(input string) string {
return RemoveFieldFromJSONResource("id", input)
}

func removeFieldFromResource(field string, input map[string]any) map[string]any {
out := map[string]any{}
out["type"] = input["type"]
newSpec := map[string]any{}
for k, v := range input["spec"].(map[string]any) {
if k == "id" {
if k == field {
continue
}
newSpec[k] = v
Expand All @@ -42,17 +58,6 @@ func parseJSON(input string) map[string]any {
return parsed
}

func removeIDFromJSON(input string) string {

clean := removeID(parseJSON(input))

out, err := json.Marshal(clean)
if err != nil {
panic(err)
}
return string(out)
}

func extractID(input string) string {
parsed := parseJSON(input)
id := parsed["spec"].(map[string]any)["id"]
Expand Down
4 changes: 2 additions & 2 deletions server/resourcemanager/testutil/operations_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var createNoIDOperation = buildSingleStepOperation(singleStepOperationTester{
clean := removeIDFromJSON(rt.SampleJSON)
expected := ct.toJSON(clean)

require.JSONEq(t, expected, removeIDFromJSON(jsonBody))
rt.customJSONComparer(t, OperationCreateNoID, expected, removeIDFromJSON(jsonBody))
require.NotEmpty(t, extractID(jsonBody))
},
})
Expand All @@ -67,7 +67,7 @@ var createSuccessOperation = buildSingleStepOperation(singleStepOperationTester{
jsonBody := responseBodyJSON(t, resp, ct)
expected := ct.toJSON(rt.SampleJSON)

require.JSONEq(t, expected, jsonBody)
rt.customJSONComparer(t, OperationCreateSuccess, expected, jsonBody)
},
})

Expand Down
2 changes: 1 addition & 1 deletion server/resourcemanager/testutil/operations_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var getSuccessOperation = buildSingleStepOperation(singleStepOperationTester{

expected := ct.toJSON(rt.SampleJSON)

require.JSONEq(t, expected, jsonBody)
rt.customJSONComparer(t, OperationGetSuccess, expected, jsonBody)
},
})

Expand Down
20 changes: 15 additions & 5 deletions server/resourcemanager/testutil/operations_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,22 @@ var listSuccessOperation = buildSingleStepOperation(singleStepOperationTester{

jsonBody := responseBodyJSON(t, resp, ct)

expected := `{
"count": 1,
"items": [` + ct.toJSON(rt.SampleJSON) + `]
}`
var parsedJsonBody struct {
Count int `json:"count"`
Items []any `json:"items"`
}
json.Unmarshal([]byte(jsonBody), &parsedJsonBody)

require.JSONEq(t, expected, jsonBody)
require.Equal(t, 1, parsedJsonBody.Count)
require.Equal(t, len(parsedJsonBody.Items), 1)

obtainedAsBytes, err := json.Marshal(parsedJsonBody.Items[0])
require.NoError(t, err)

expected := ct.toJSON(rt.SampleJSON)
obtained := string(obtainedAsBytes)

rt.customJSONComparer(t, OperationListSuccess, expected, obtained)
},
})

Expand Down
2 changes: 1 addition & 1 deletion server/resourcemanager/testutil/operations_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var updateSuccessOperation = buildSingleStepOperation(singleStepOperationTester{

expected := ct.toJSON(rt.SampleJSONUpdated)

require.JSONEq(t, expected, jsonBody)
rt.customJSONComparer(t, OperationUpdateSuccess, expected, jsonBody)
},
})

Expand Down
27 changes: 24 additions & 3 deletions server/resourcemanager/testutil/test_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ type ResourceTypeTest struct {
SampleJSON string
SampleJSONUpdated string

// private files
sortFields []string
// private fields
sortFields []string
customJSONComparer func(t require.TestingT, operation Operation, firstValue, secondValue string)
}

type config struct {
operations operationTesters
operations operationTesters
customJSONComparer func(t require.TestingT, operation Operation, firstValue, secondValue string)
}

type testOption func(*config)
Expand All @@ -37,6 +39,16 @@ func ExcludeOperations(ops ...Operation) testOption {
}
}

func JSONComparer(comparer func(t require.TestingT, operation Operation, firstValue, secondValue string)) testOption {
return func(c *config) {
c.customJSONComparer = comparer
}
}

func rawJSONComparer(t require.TestingT, operation Operation, firstValue, secondValue string) {
require.JSONEq(t, firstValue, secondValue)
}

func TestResourceType(t *testing.T, rt ResourceTypeTest, opts ...testOption) {
t.Helper()

Expand All @@ -48,12 +60,21 @@ func TestResourceType(t *testing.T, rt ResourceTypeTest, opts ...testOption) {
opt(&cfg)
}

// consider customJSONComparer option
if cfg.customJSONComparer == nil {
cfg.customJSONComparer = rawJSONComparer
}
rt.customJSONComparer = cfg.customJSONComparer

TestResourceTypeOperations(t, rt, cfg.operations)
}

func TestResourceTypeWithErrorOperations(t *testing.T, rt ResourceTypeTest) {
t.Helper()

// assumes default
rt.customJSONComparer = rawJSONComparer

TestResourceTypeOperations(t, rt, append(defaultOperations, errorOperations...))
}

Expand Down

0 comments on commit fa45a49

Please sign in to comment.