Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding customJSON comparer to RM tests #2393

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -20,12 +20,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 @@ -36,6 +38,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 @@ -47,12 +59,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