Skip to content

Commit

Permalink
add schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Oct 19, 2024
1 parent f3d0f09 commit de33eab
Show file tree
Hide file tree
Showing 11 changed files with 10,566 additions and 2 deletions.
28 changes: 26 additions & 2 deletions ndc-rest-schema/openapi/oas2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"os"
"testing"

"github.com/hasura/ndc-rest/ndc-rest-schema/schema"
rest "github.com/hasura/ndc-rest/ndc-rest-schema/schema"
"github.com/hasura/ndc-sdk-go/schema"
"gotest.tools/v3/assert"
)

Expand All @@ -17,36 +18,45 @@ func TestOpenAPIv2ToRESTSchema(t *testing.T) {
Source string
Options ConvertOptions
Expected string
Schema string
}{
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/jsonplaceholder/swagger.json -o ./ndc-rest-schema/openapi/testdata/jsonplaceholder/expected.json --spec oas2 --trim-prefix /v1
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/jsonplaceholder/swagger.json -o ./ndc-rest-schema/openapi/testdata/jsonplaceholder/schema.json --pure --spec oas2 --trim-prefix /v1
{
Name: "jsonplaceholder",
Source: "testdata/jsonplaceholder/swagger.json",
Expected: "testdata/jsonplaceholder/expected.json",
Schema: "testdata/jsonplaceholder/schema.json",
Options: ConvertOptions{
TrimPrefix: "/v1",
},
},
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/petstore2/swagger.json -o ./ndc-rest-schema/openapi/testdata/petstore2/expected.json --spec oas2
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/petstore2/swagger.json -o ./ndc-rest-schema/openapi/testdata/petstore2/schema.json --pure --spec oas2
{
Name: "petstore2",
Source: "testdata/petstore2/swagger.json",
Expected: "testdata/petstore2/expected.json",
Schema: "testdata/petstore2/schema.json",
},
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/prefix2/source.json -o ./ndc-rest-schema/openapi/testdata/prefix2/expected_single_word.json --spec oas2 --prefix hasura
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/prefix2/source.json -o ./ndc-rest-schema/openapi/testdata/prefix2/expected_single_word.schema.json --pure --spec oas2 --prefix hasura
{
Name: "prefix2_single_word",
Source: "testdata/prefix2/source.json",
Expected: "testdata/prefix2/expected_single_word.json",
Schema: "testdata/prefix2/expected_single_word.schema.json",
Options: ConvertOptions{
Prefix: "hasura",
},
},
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/prefix2/source.json -o ./ndc-rest-schema/openapi/testdata/prefix2/expected_multi_words.json --spec oas2 --prefix hasura_mock_json
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/prefix2/source.json -o ./ndc-rest-schema/openapi/testdata/prefix2/expected_multi_words.schema.json --pure --spec oas2 --prefix hasura_mock_json
{
Name: "prefix2_single_word",
Source: "testdata/prefix2/source.json",
Expected: "testdata/prefix2/expected_multi_words.json",
Schema: "testdata/prefix2/expected_multi_words.schema.json",
Options: ConvertOptions{
Prefix: "hasura_mock_json",
},
Expand All @@ -60,7 +70,7 @@ func TestOpenAPIv2ToRESTSchema(t *testing.T) {

expectedBytes, err := os.ReadFile(tc.Expected)
assert.NilError(t, err)
var expected schema.NDCRestSchema
var expected rest.NDCRestSchema
assert.NilError(t, json.Unmarshal(expectedBytes, &expected))

output, errs := OpenAPIv2ToNDCSchema(sourceBytes, tc.Options)
Expand All @@ -70,6 +80,7 @@ func TestOpenAPIv2ToRESTSchema(t *testing.T) {
}

assertRESTSchemaEqual(t, &expected, output)
assertConnectorSchema(t, tc.Schema, output)
})
}

Expand All @@ -78,3 +89,16 @@ func TestOpenAPIv2ToRESTSchema(t *testing.T) {
assert.ErrorContains(t, errors.Join(err...), "there is nothing in the spec, it's empty")
})
}

func assertConnectorSchema(t *testing.T, schemaPath string, output *rest.NDCRestSchema) {
t.Helper()
if schemaPath == "" {
return
}
schemaBytes, err := os.ReadFile(schemaPath)
assert.NilError(t, err)
var expectedSchema schema.SchemaResponse
assert.NilError(t, json.Unmarshal(schemaBytes, &expectedSchema))
outputSchema := output.ToSchemaResponse()
assert.DeepEqual(t, expectedSchema, *outputSchema)
}
12 changes: 12 additions & 0 deletions ndc-rest-schema/openapi/oas3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,57 @@ func TestOpenAPIv3ToRESTSchema(t *testing.T) {
Name string
Source string
Expected string
Schema string
Options ConvertOptions
}{
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/petstore3/source.json -o ./ndc-rest-schema/openapi/testdata/petstore3/expected.json --trim-prefix /v1 --spec openapi3 --env-prefix PET_STORE
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/petstore3/source.json -o ./ndc-rest-schema/openapi/testdata/petstore3/schema.json --pure --trim-prefix /v1 --spec openapi3 --env-prefix PET_STORE
{
Name: "petstore3",
Source: "testdata/petstore3/source.json",
Expected: "testdata/petstore3/expected.json",
Schema: "testdata/petstore3/schema.json",
Options: ConvertOptions{
TrimPrefix: "/v1",
EnvPrefix: "PET_STORE",
},
},
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/onesignal/source.json -o ./ndc-rest-schema/openapi/testdata/onesignal/expected.json --spec openapi3
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/onesignal/source.json -o ./ndc-rest-schema/openapi/testdata/onesignal/schema.json --pure --spec openapi3
{
Name: "onesignal",
Source: "testdata/onesignal/source.json",
Expected: "testdata/onesignal/expected.json",
Schema: "testdata/onesignal/schema.json",
Options: ConvertOptions{},
},
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/openai/source.json -o ./ndc-rest-schema/openapi/testdata/openai/expected.json --spec openapi3
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/openai/source.json -o ./ndc-rest-schema/openapi/testdata/openai/schema.json --pure --spec openapi3
{
Name: "openai",
Source: "testdata/openai/source.json",
Expected: "testdata/openai/expected.json",
Schema: "testdata/openai/schema.json",
Options: ConvertOptions{},
},
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/prefix3/source.json -o ./ndc-rest-schema/openapi/testdata/prefix3/expected_single_word.json --spec openapi3 --prefix hasura
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/prefix3/source.json -o ./ndc-rest-schema/openapi/testdata/prefix3/expected_single_word.schema.json --pure --spec openapi3 --prefix hasura
{
Name: "prefix3_single_word",
Source: "testdata/prefix3/source.json",
Expected: "testdata/prefix3/expected_single_word.json",
Schema: "testdata/prefix3/expected_single_word.schema.json",
Options: ConvertOptions{
Prefix: "hasura",
},
},
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/prefix3/source.json -o ./ndc-rest-schema/openapi/testdata/prefix3/expected_multi_words.json --spec openapi3 --prefix hasura_one_signal
// go run ./ndc-rest-schema convert -f ./ndc-rest-schema/openapi/testdata/prefix3/source.json -o ./ndc-rest-schema/openapi/testdata/prefix3/expected_multi_words.schema.json --pure --spec openapi3 --prefix hasura_one_signal
{
Name: "prefix3_multi_words",
Source: "testdata/prefix3/source.json",
Expected: "testdata/prefix3/expected_multi_words.json",
Schema: "testdata/prefix3/expected_multi_words.schema.json",
Options: ConvertOptions{
Prefix: "hasura_one_signal",
},
Expand All @@ -78,6 +89,7 @@ func TestOpenAPIv3ToRESTSchema(t *testing.T) {
}

assertRESTSchemaEqual(t, &expected, output)
assertConnectorSchema(t, tc.Schema, output)
})
}

Expand Down
Loading

0 comments on commit de33eab

Please sign in to comment.