Skip to content

Commit

Permalink
test: added additional tests (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo committed Mar 15, 2024
1 parent 15bf38e commit 246f9ef
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 35 deletions.
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) {
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

0 comments on commit 246f9ef

Please sign in to comment.