-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added example of test * Added positive tests * Updated configuration * Added invalid tests * Added test cases
- Loading branch information
Showing
14 changed files
with
205 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="build" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile"> | ||
<makefile filename="$PROJECT_DIR$/makefile" target="build" workingDirectory="" arguments=""> | ||
<envs /> | ||
</makefile> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="format" type="CompoundRunConfigurationType"> | ||
<toRun name="gofmt" type="ShConfigurationType"/> | ||
<toRun name="gofumpt" type="ShConfigurationType"/> | ||
<method v="2"/> | ||
</configuration> | ||
<configuration default="false" name="format" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile"> | ||
<makefile filename="$PROJECT_DIR$/makefile" target="format" workingDirectory="" arguments=""> | ||
<envs /> | ||
</makefile> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package schema | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TransformToJSON(t *testing.T, dir string, file string) string { | ||
t.Helper() | ||
yamlFilePath := filepath.Join(testutils.CurrentDir(t), file) | ||
jsonFilePath := filepath.Join(dir, strings.Replace(filepath.Base(file), ".yaml", ".json", 1)) | ||
|
||
yamlFile, err := os.OpenFile(yamlFilePath, os.O_RDONLY, os.ModePerm) | ||
require.NoError(t, err, "Failed to open file: %v", err) | ||
defer yamlFile.Close() | ||
|
||
jsonFile, err := os.OpenFile(jsonFilePath, os.O_CREATE|os.O_WRONLY, os.ModePerm) | ||
require.NoError(t, err, "Failed to open file: %v", err) | ||
defer yamlFile.Close() | ||
|
||
var data any | ||
err = yaml.NewDecoder(yamlFile).Decode(&data) | ||
require.NoError(t, err, "Failed to decode yaml: %v", err) | ||
|
||
err = json.NewEncoder(jsonFile).Encode(data) | ||
require.NoError(t, err, "Failed to encode json: %v", err) | ||
|
||
return jsonFilePath | ||
} | ||
|
||
func DirPredicate(dir string) func(string) string { | ||
return func(file string) string { | ||
return filepath.Join(dir, file) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mappings: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mappings: | ||
- from: http://localhost | ||
to: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package schema_test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/evg4b/uncors/tests/schema" | ||
|
||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xeipuuv/gojsonschema" | ||
) | ||
|
||
func TestInvalidJsonSchema(t *testing.T) { | ||
testdir := schema.DirPredicate("invalid") | ||
|
||
testTempDir := t.TempDir() | ||
jsonSchemaPath := filepath.Join(testutils.CurrentDir(t), "../../schema.json") | ||
|
||
cases := []struct { | ||
name string | ||
file string | ||
errors []string | ||
}{ | ||
{ | ||
name: "empty mappings", | ||
file: testdir("empty-mappings.yaml"), | ||
errors: []string{ | ||
"mappings: Array must have at least 1 items", | ||
}, | ||
}, | ||
{ | ||
name: "not full mapping", | ||
file: testdir("not-full-mapping.yaml"), | ||
errors: []string{ | ||
"mappings.0: Must validate one and only one schema (oneOf)", | ||
"mappings.0.to: Invalid type. Expected: string, given: null", | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range cases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
targetJSONFile := schema.TransformToJSON(t, testTempDir, testCase.file) | ||
|
||
schemaLoader := gojsonschema.NewReferenceLoader("file://" + jsonSchemaPath) | ||
documentLoader := gojsonschema.NewReferenceLoader("file://" + targetJSONFile) | ||
|
||
result, err := gojsonschema.Validate(schemaLoader, documentLoader) | ||
require.NoError(t, err) | ||
|
||
errors := lo.Map(result.Errors(), func(err gojsonschema.ResultError, _ int) string { | ||
return err.String() | ||
}) | ||
|
||
assert.Equal(t, testCase.errors, errors, "The errors are not as expected") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mappings: | ||
- from: http://localhost | ||
to: https://github.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
http-port: 3000 | ||
mappings: | ||
- http://localhost: https://github.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package schema_test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/evg4b/uncors/tests/schema" | ||
|
||
"github.com/evg4b/uncors/testing/testutils" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/xeipuuv/gojsonschema" | ||
) | ||
|
||
func TestValidJsonSchema(t *testing.T) { | ||
testdir := schema.DirPredicate("valid") | ||
|
||
testTempDir := t.TempDir() | ||
jsonSchemaPath := filepath.Join(testutils.CurrentDir(t), "../../schema.json") | ||
|
||
cases := []struct { | ||
name string | ||
file string | ||
}{ | ||
{ | ||
name: "minimal valid file", | ||
file: testdir("minimal-valid.yaml"), | ||
}, | ||
{ | ||
name: "short mapping", | ||
file: testdir("short-mapping.yaml"), | ||
}, | ||
} | ||
|
||
for _, testCase := range cases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
targetJSONFile := schema.TransformToJSON(t, testTempDir, testCase.file) | ||
|
||
schemaLoader := gojsonschema.NewReferenceLoader("file://" + jsonSchemaPath) | ||
documentLoader := gojsonschema.NewReferenceLoader("file://" + targetJSONFile) | ||
|
||
result, err := gojsonschema.Validate(schemaLoader, documentLoader) | ||
require.NoError(t, err) | ||
|
||
assert.Empty(t, result.Errors(), "The document is not valid") | ||
}) | ||
} | ||
} |