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

test: added additional tests #12

Merged
merged 6 commits into from Mar 15, 2024
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
2 changes: 1 addition & 1 deletion tools/cli/Makefile
Expand Up @@ -63,7 +63,7 @@ list: ## List all make targets
.PHONY: unit-test
unit-test: ## Run unit-tests
@echo "==> Running unit tests..."
$(TEST_CMD) -race ./...
$(TEST_CMD) -race -cover ./...

.PHONY: gen-mocks
gen-mocks: ## Generate mocks
Expand Down
1 change: 0 additions & 1 deletion tools/cli/go.mod
Expand Up @@ -9,7 +9,6 @@ require (
github.com/stretchr/testify v1.9.0
github.com/tufin/oasdiff v1.10.11
go.uber.org/mock v0.4.0
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
)

require (
Expand Down
64 changes: 64 additions & 0 deletions tools/cli/internal/cli/merge/merge_test.go
Expand Up @@ -15,13 +15,15 @@
package merge

import (
"fmt"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/mongodb/openapi/tools/cli/internal/cli/flag"
"github.com/mongodb/openapi/tools/cli/internal/cli/validator"
"github.com/mongodb/openapi/tools/cli/internal/openapi"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/load"
"go.uber.org/mock/gomock"
)
Expand Down Expand Up @@ -56,6 +58,31 @@ func TestSuccessfulMerge_Run(t *testing.T) {
}
}

func TestNoBaseSpecMerge_PreRun(t *testing.T) {
externalPaths := []string{"external.json"}
opts := &Opts{
outputPath: "foas.json",
externalPaths: externalPaths,
}

err := opts.PreRunE(nil)
require.Error(t, err)
require.EqualError(t, err, fmt.Sprintf("no base OAS detected. "+
"Please, use the flag %s to include the base OAS", flag.Base))
}

func TestNoExternalSpecMerge_PreRun(t *testing.T) {
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
opts := &Opts{
outputPath: "foas.json",
basePath: "base.json",
}

err := opts.PreRunE(nil)
require.Error(t, err)
require.EqualError(t, err, fmt.Sprintf("no external OAS detected. "+
"Please, use the flag %s to include at least one OAS", flag.External))
}

func TestCreateBuilder(t *testing.T) {
validator.ValidateSubCommandsAndFlags(
t,
Expand All @@ -64,3 +91,40 @@ func TestCreateBuilder(t *testing.T) {
[]string{flag.Base, flag.External, flag.Output},
)
}

func TestOpts_PreRunE(t *testing.T) {
testCases := []struct {
wantErr require.ErrorAssertionFunc
basePath string
name string
externalPaths []string
}{
{
wantErr: require.Error,
basePath: "test",
name: "NoExternalPaths",
externalPaths: nil,
},
{
wantErr: require.Error,
name: "NoBasePath",
externalPaths: []string{"test"},
},
{
wantErr: require.NoError,
basePath: "../../../test/data/base.json",
name: "Successful",
externalPaths: []string{"test"},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
o := &Opts{
basePath: tt.basePath,
externalPaths: tt.externalPaths,
}
tt.wantErr(t, o.PreRunE(nil))
})
}
}
8 changes: 8 additions & 0 deletions tools/cli/internal/openapi/oasdiff.go
Expand Up @@ -200,7 +200,15 @@ func (o OasDiff) mergeResponses() error {

func (o OasDiff) mergeSchemas() error {
extSchemas := o.external.Spec.Components.Schemas
if len(extSchemas) == 0 {
return nil
}

baseSchemas := o.base.Spec.Components.Schemas
if len(baseSchemas) == 0 {
o.base.Spec.Components.Schemas = extSchemas
return nil
}

for k, schemaToMerge := range extSchemas {
if _, ok := baseSchemas[k]; !ok {
Expand Down