Skip to content

Commit

Permalink
Update unit tests to use generated files
Browse files Browse the repository at this point in the history
And add a test to the json package to make sure the variable output stays consistent between versions.
  • Loading branch information
AislingHPE committed Aug 30, 2024
1 parent c0f5022 commit a595404
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 262 deletions.
57 changes: 57 additions & 0 deletions pkg/json/json_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/reader/type-constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/reader/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
54 changes: 0 additions & 54 deletions test/expected/complex-types/defaults.json

This file was deleted.

5 changes: 5 additions & 0 deletions test/expected/complex-types/sample-input/test-input-null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "../schema.json",
"a_very_complicated_object": null,
"an_object_with_optional": null
}
67 changes: 0 additions & 67 deletions test/expected/complex-types/type-constraints.json

This file was deleted.

28 changes: 0 additions & 28 deletions test/expected/custom-validation/defaults.json

This file was deleted.

15 changes: 15 additions & 0 deletions test/expected/custom-validation/sample-input/test-input-null.json
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 0 additions & 33 deletions test/expected/custom-validation/type-constraints.json

This file was deleted.

1 change: 0 additions & 1 deletion test/expected/empty/defaults.json

This file was deleted.

1 change: 0 additions & 1 deletion test/expected/empty/type-constraints.json

This file was deleted.

30 changes: 0 additions & 30 deletions test/expected/simple-types/defaults.json

This file was deleted.

13 changes: 13 additions & 0 deletions test/expected/simple-types/sample-input/test-input-null.json
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit a595404

Please sign in to comment.