From a595404b98495bf5c2ad20193257bb664c8f78e2 Mon Sep 17 00:00:00 2001 From: AislingHPE Date: Fri, 30 Aug 2024 12:18:46 +0100 Subject: [PATCH] Update unit tests to use generated files And add a test to the json package to make sure the variable output stays consistent between versions. --- pkg/json/json_test.go | 57 ++++++++++++++++ pkg/reader/type-constraint_test.go | 4 +- pkg/reader/value_test.go | 8 +-- test/expected/complex-types/defaults.json | 54 --------------- .../sample-input/test-input-null.json | 5 ++ .../complex-types/type-constraints.json | 67 ------------------- test/expected/custom-validation/defaults.json | 28 -------- .../sample-input/test-input-null.json | 15 +++++ .../custom-validation/type-constraints.json | 33 --------- test/expected/empty/defaults.json | 1 - test/expected/empty/type-constraints.json | 1 - test/expected/simple-types/defaults.json | 30 --------- .../sample-input/test-input-null.json | 13 ++++ .../simple-types/type-constraints.json | 35 ---------- test/expected/simple/defaults.json | 3 - .../simple/sample-input/test-input-null.json | 5 ++ test/expected/simple/type-constraints.json | 4 -- 17 files changed, 101 insertions(+), 262 deletions(-) create mode 100644 pkg/json/json_test.go delete mode 100644 test/expected/complex-types/defaults.json create mode 100644 test/expected/complex-types/sample-input/test-input-null.json delete mode 100644 test/expected/complex-types/type-constraints.json delete mode 100644 test/expected/custom-validation/defaults.json create mode 100644 test/expected/custom-validation/sample-input/test-input-null.json delete mode 100644 test/expected/custom-validation/type-constraints.json delete mode 100644 test/expected/empty/defaults.json delete mode 100644 test/expected/empty/type-constraints.json delete mode 100644 test/expected/simple-types/defaults.json create mode 100644 test/expected/simple-types/sample-input/test-input-null.json delete mode 100644 test/expected/simple-types/type-constraints.json delete mode 100644 test/expected/simple/defaults.json create mode 100644 test/expected/simple/sample-input/test-input-null.json delete mode 100644 test/expected/simple/type-constraints.json diff --git a/pkg/json/json_test.go b/pkg/json/json_test.go new file mode 100644 index 0000000..1e4afd2 --- /dev/null +++ b/pkg/json/json_test.go @@ -0,0 +1,57 @@ +package json + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/require" +) + +func TestCreateSchema(t *testing.T) { + t.Parallel() + tfPath := "../../test/modules" + schemaPath := "../../test/expected" + testCases := []string{ + "empty", + "simple", + "simple-types", + "complex-types", + "custom-validation", + } + for i := range testCases { + name := testCases[i] + t.Run(name, func(t *testing.T) { + t.Parallel() + expected, err := os.ReadFile(filepath.Join(schemaPath, name, "variables.json")) + require.NoError(t, err) + + result, err := ExportVariables(filepath.Join(tfPath, name), ExportVariablesOptions{ + AllowEmpty: true, + DebugOut: true, + SuppressLogging: false, + EscapeJSON: false, + Indent: "\t", + }) + require.NoError(t, err) + + // marshal and unmarshal to ensure that the map is in the correct format + buf, err := json.Marshal(result) + require.NoError(t, err) + + var gotMap map[string]any + err = json.Unmarshal(buf, &gotMap) + require.NoError(t, err) + + var expectedMap map[string]any + err = json.Unmarshal(expected, &expectedMap) + require.NoError(t, err) + + if d := cmp.Diff(expectedMap, gotMap); d != "" { + t.Errorf("Schema has incorrect value (-want,+got):\n%s", d) + } + }) + } +} diff --git a/pkg/reader/type-constraint_test.go b/pkg/reader/type-constraint_test.go index d604497..e1f34d5 100644 --- a/pkg/reader/type-constraint_test.go +++ b/pkg/reader/type-constraint_test.go @@ -27,7 +27,7 @@ func TestGetTypeConstraint(t *testing.T) { name := testCases[i] t.Run(name, func(t *testing.T) { t.Parallel() - expected, err := os.ReadFile(filepath.Join(expectedPath, name, "type-constraints.json")) + expected, err := os.ReadFile(filepath.Join(expectedPath, name, "variables.json")) require.NoError(t, err) varMap, err := GetVarMap(filepath.Join(tfPath, name), true) @@ -42,7 +42,7 @@ func TestGetTypeConstraint(t *testing.T) { require.Equal(t, len(varMap), len(expectedMap)) for key, val := range varMap { - expectedVal, ok := expectedMap[key] + expectedVal, ok := expectedMap[key].(map[string]any)["type"] if !ok { t.Errorf("Variable %q not found in expected map", key) } diff --git a/pkg/reader/value_test.go b/pkg/reader/value_test.go index a880da6..f4df6fc 100644 --- a/pkg/reader/value_test.go +++ b/pkg/reader/value_test.go @@ -27,7 +27,7 @@ func TestExpressionToJSONObject_Default(t *testing.T) { name := testCases[i] t.Run(name, func(t *testing.T) { t.Parallel() - expected, err := os.ReadFile(filepath.Join(expectedPath, name, "defaults.json")) + expected, err := os.ReadFile(filepath.Join(expectedPath, name, "variables.json")) require.NoError(t, err) var expectedMap map[string]any err = json.Unmarshal(expected, &expectedMap) @@ -49,12 +49,12 @@ func TestExpressionToJSONObject_Default(t *testing.T) { require.NoError(t, err) } - if len(defaults) != len(expectedMap) { - t.Errorf("Expected %d variables with defaults, got %d", len(expectedMap), len(varMap)) + if len(expectedMap) != len(varMap) { + t.Errorf("Expected %d variables, got %d", len(expectedMap), len(varMap)) } for key, val := range defaults { - expectedVal, ok := expectedMap[key] + expectedVal, ok := expectedMap[key].(map[string]any)["default"] if !ok { t.Errorf("Variable %q not found in expected map", key) } diff --git a/test/expected/complex-types/defaults.json b/test/expected/complex-types/defaults.json deleted file mode 100644 index fe6b91d..0000000 --- a/test/expected/complex-types/defaults.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - "a_very_complicated_object": { - "b": [ - [ - "a", - "b", - "c" - ], - true - ], - "c": { - "a": [ - "a" - ], - "b": [ - "b" - ] - }, - "d": { - "a": [ - [ - "a", - "b" - ], - [ - "c", - "d" - ] - ], - "b": 1 - }, - "e": [ - "a", - 1 - ], - "f": [ - [ - "a" - ], - [ - "b" - ], - [ - "a", - "b" - ] - ] - }, - "an_object_with_optional": { - "a": "a", - "b": 1, - "c": true - } -} \ No newline at end of file diff --git a/test/expected/complex-types/sample-input/test-input-null.json b/test/expected/complex-types/sample-input/test-input-null.json new file mode 100644 index 0000000..9c390a1 --- /dev/null +++ b/test/expected/complex-types/sample-input/test-input-null.json @@ -0,0 +1,5 @@ +{ + "$schema": "../schema.json", + "a_very_complicated_object": null, + "an_object_with_optional": null +} \ No newline at end of file diff --git a/test/expected/complex-types/type-constraints.json b/test/expected/complex-types/type-constraints.json deleted file mode 100644 index 32bd753..0000000 --- a/test/expected/complex-types/type-constraints.json +++ /dev/null @@ -1,67 +0,0 @@ -{ - "a_very_complicated_object": [ - "object", - { - "a": "string", - "b": [ - "tuple", - [ - [ - "list", - "string" - ], - "bool" - ] - ], - "c": [ - "map", - [ - "list", - "string" - ] - ], - "d": [ - "object", - { - "a": [ - "list", - [ - "list", - "string" - ] - ], - "b": "number" - } - ], - "e": [ - "tuple", - [ - "string", - "number" - ] - ], - "f": [ - "set", - [ - "list", - "string" - ] - ] - }, - [ - "a" - ] - ], - "an_object_with_optional": [ - "object", - { - "a": "string", - "b": "number", - "c": "bool", - "d": "string" - }, - [ - "d" - ] - ] -} \ No newline at end of file diff --git a/test/expected/custom-validation/defaults.json b/test/expected/custom-validation/defaults.json deleted file mode 100644 index 75d9d1e..0000000 --- a/test/expected/custom-validation/defaults.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "a_list_maximum_minimum_length": [ - "a" - ], - "a_map_maximum_minimum_entries": { - "a": "a" - }, - "a_number_enum_kind_1": 1, - "a_number_enum_kind_2": 1, - "a_number_exclusive_maximum_minimum": 1, - "a_number_maximum_minimum": 0, - "a_set_maximum_minimum_items": [ - "a" - ], - "a_string_enum_escaped_characters_kind_1": "\\", - "a_string_enum_escaped_characters_kind_2": "\"", - "a_string_enum_kind_1": "a", - "a_string_enum_kind_2": "a", - "a_string_length_over_defined": "a", - "a_string_maximum_minimum_length": "a", - "a_string_pattern_1": "1.1.1.1", - "a_string_pattern_2": "#000000", - "a_string_set_length": "abcd", - "an_object_maximum_minimum_items": { - "name": "a", - "other_field": "b" - } -} \ No newline at end of file diff --git a/test/expected/custom-validation/sample-input/test-input-null.json b/test/expected/custom-validation/sample-input/test-input-null.json new file mode 100644 index 0000000..1f8bd4d --- /dev/null +++ b/test/expected/custom-validation/sample-input/test-input-null.json @@ -0,0 +1,15 @@ +{ + "$schema": "../schema.json", + "a_list_maximum_minimum_length": null, + "a_map_maximum_minimum_entries": null, + "a_set_maximum_minimum_items": null, + "a_string_enum_escaped_characters_kind_1": null, + "a_string_enum_kind_1": null, + "a_string_enum_kind_2": null, + "a_string_length_over_defined": null, + "a_string_maximum_minimum_length":null, + "a_string_pattern_1": null, + "a_string_pattern_2": null, + "a_string_set_length":null, + "an_object_maximum_minimum_items": null +} \ No newline at end of file diff --git a/test/expected/custom-validation/type-constraints.json b/test/expected/custom-validation/type-constraints.json deleted file mode 100644 index 9076c22..0000000 --- a/test/expected/custom-validation/type-constraints.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "a_list_maximum_minimum_length": [ - "list", - "string" - ], - "a_map_maximum_minimum_entries": [ - "map", - "string" - ], - "a_number_enum_kind_1": "number", - "a_number_enum_kind_2": "number", - "a_number_exclusive_maximum_minimum": "number", - "a_number_maximum_minimum": "number", - "a_set_maximum_minimum_items": [ - "set", - "string" - ], - "a_string_enum_escaped_characters_kind_1": "string", - "a_string_enum_escaped_characters_kind_2": "string", - "a_string_enum_kind_1": "string", - "a_string_enum_kind_2": "string", - "a_string_length_over_defined": "string", - "a_string_maximum_minimum_length": "string", - "a_string_pattern_1": "string", - "a_string_pattern_2": "string", - "a_string_set_length": "string", - "an_object_maximum_minimum_items": [ - "object", - { - "name": "string" - } - ] -} \ No newline at end of file diff --git a/test/expected/empty/defaults.json b/test/expected/empty/defaults.json deleted file mode 100644 index 9e26dfe..0000000 --- a/test/expected/empty/defaults.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/test/expected/empty/type-constraints.json b/test/expected/empty/type-constraints.json deleted file mode 100644 index 9e26dfe..0000000 --- a/test/expected/empty/type-constraints.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/test/expected/simple-types/defaults.json b/test/expected/simple-types/defaults.json deleted file mode 100644 index f4ca1df..0000000 --- a/test/expected/simple-types/defaults.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "a_bool": false, - "a_list": [ - "a", - "b", - "c" - ], - "a_map_of_strings": { - "a": "a", - "b": "b", - "c": "c" - }, - "a_set": [ - "a", - "b", - "c" - ], - "a_string": "a string", - "a_tuple": [ - "a", - 1, - true - ], - "a_variable_in_another_file": "", - "an_object": { - "a": "a", - "b": 1, - "c": true - } -} \ No newline at end of file diff --git a/test/expected/simple-types/sample-input/test-input-null.json b/test/expected/simple-types/sample-input/test-input-null.json new file mode 100644 index 0000000..9a3e60e --- /dev/null +++ b/test/expected/simple-types/sample-input/test-input-null.json @@ -0,0 +1,13 @@ +{ + "$schema": "../schema.json", + "a_bool": null, + "a_list": null, + "a_map_of_strings": null, + "a_nullable_string": null, + "a_number": null, + "a_set": null, + "a_string": null, + "a_tuple": null, + "a_variable_in_another_file": null, + "an_object": null +} \ No newline at end of file diff --git a/test/expected/simple-types/type-constraints.json b/test/expected/simple-types/type-constraints.json deleted file mode 100644 index 623ae3a..0000000 --- a/test/expected/simple-types/type-constraints.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "a_bool": "bool", - "a_list": [ - "list", - "string" - ], - "a_map_of_strings": [ - "map", - "string" - ], - "a_nullable_string": "string", - "a_number": "number", - "a_set": [ - "set", - "string" - ], - "a_string": "string", - "a_tuple": [ - "tuple", - [ - "string", - "number", - "bool" - ] - ], - "a_variable_in_another_file": "string", - "an_object": [ - "object", - { - "a": "string", - "b": "number", - "c": "bool" - } - ] -} \ No newline at end of file diff --git a/test/expected/simple/defaults.json b/test/expected/simple/defaults.json deleted file mode 100644 index b2721b7..0000000 --- a/test/expected/simple/defaults.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name": "world" -} \ No newline at end of file diff --git a/test/expected/simple/sample-input/test-input-null.json b/test/expected/simple/sample-input/test-input-null.json new file mode 100644 index 0000000..7a781de --- /dev/null +++ b/test/expected/simple/sample-input/test-input-null.json @@ -0,0 +1,5 @@ +{ + "$schema": "../schema.json", + "name": null, + "age": null +} \ No newline at end of file diff --git a/test/expected/simple/type-constraints.json b/test/expected/simple/type-constraints.json deleted file mode 100644 index 7b92d60..0000000 --- a/test/expected/simple/type-constraints.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "age": "number", - "name": "string" -} \ No newline at end of file