-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: added unit tests to the internal packages
This recovers a bit of the loss of test coverage ratio after accounting for all source files. Signed-off-by: Frederic BIDON <[email protected]>
- Loading branch information
Showing
6 changed files
with
245 additions
and
32 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
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,104 @@ | ||
package antest | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLongTestEnabled(t *testing.T) { | ||
t.Run("should be false by default", func(t *testing.T) { | ||
require.False(t, LongTestsEnabled()) | ||
}) | ||
} | ||
|
||
func TestLoadSpecErrorCases(t *testing.T) { | ||
t.Run("should not load invalid path", func(t *testing.T) { | ||
_, err := LoadSpec("nowhere.json") | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("should not load invalid YAML", func(t *testing.T) { | ||
invalidYAMLFile, clean := prepareBadDoc(t, "yaml", true) | ||
t.Cleanup(clean) | ||
|
||
_, err := LoadSpec(invalidYAMLFile) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("should not load invalid JSON", func(t *testing.T) { | ||
invalidJSONFile, clean := prepareBadDoc(t, "json", true) | ||
t.Cleanup(clean) | ||
|
||
_, err := LoadSpec(invalidJSONFile) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("should not load invalid spec", func(t *testing.T) { | ||
invalidJSONFile, clean := prepareBadDoc(t, "json", false) | ||
t.Cleanup(clean) | ||
|
||
_, err := LoadSpec(invalidJSONFile) | ||
require.Error(t, err) | ||
}) | ||
} | ||
|
||
func prepareBadDoc(t testing.TB, kind string, invalidFormat bool) (string, func()) { | ||
t.Helper() | ||
|
||
var ( | ||
file string | ||
data []byte | ||
) | ||
|
||
switch kind { | ||
case "yaml", "yml": | ||
f, err := os.CreateTemp("", "*.yaml") | ||
require.NoError(t, err) | ||
file = f.Name() | ||
|
||
if invalidFormat { | ||
data = []byte(`-- | ||
zig: | ||
zag 3, 4 | ||
`) | ||
} else { | ||
data = []byte(`-- | ||
swagger: 2 | ||
info: | ||
title: true | ||
`) | ||
} | ||
|
||
case "json": | ||
f, err := os.CreateTemp("", "*.json") | ||
require.NoError(t, err) | ||
file = f.Name() | ||
|
||
if invalidFormat { | ||
data = []byte(`{ | ||
"zig": { | ||
"zag" | ||
}`) | ||
} else { | ||
data = []byte(`{ | ||
"swagger": 2 | ||
"info": { | ||
"title": true | ||
} | ||
}`) | ||
} | ||
|
||
default: | ||
panic("supports only yaml or json") | ||
} | ||
|
||
require.NoError(t, | ||
os.WriteFile(file, data, 0600), | ||
) | ||
|
||
return file, func() { | ||
_ = os.RemoveAll(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 |
---|---|---|
@@ -1,5 +1,67 @@ | ||
package operations | ||
|
||
import ( | ||
"testing" | ||
|
||
_ "github.com/go-openapi/analysis/internal/antest" | ||
"github.com/go-openapi/spec" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var _ Provider = mockOperationsProvider{} | ||
|
||
type mockOperationsProvider struct { | ||
give map[string]map[string]*spec.Operation | ||
} | ||
|
||
func (m mockOperationsProvider) Operations() map[string]map[string]*spec.Operation { | ||
return m.give | ||
} | ||
|
||
func TestGatherOperations(t *testing.T) { | ||
t.Run("should handle empty operation IDs", func(_ *testing.T) { | ||
m := mockOperationsProvider{ | ||
give: map[string]map[string]*spec.Operation{ | ||
"get": { | ||
"/pth1": { | ||
OperationProps: spec.OperationProps{ | ||
ID: "", | ||
Description: "ok", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
res := GatherOperations(m, nil) | ||
require.Contains(t, res, "GetPth1") | ||
}) | ||
|
||
t.Run("should handle duplicate operation IDs (when spec validation is skipped)", func(_ *testing.T) { | ||
m := mockOperationsProvider{ | ||
give: map[string]map[string]*spec.Operation{ | ||
"get": { | ||
"/pth1": { | ||
OperationProps: spec.OperationProps{ | ||
ID: "id1", | ||
Description: "ok", | ||
}, | ||
}, | ||
}, | ||
"post": { | ||
"/pth2": { | ||
OperationProps: spec.OperationProps{ | ||
ID: "id1", | ||
Description: "ok", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
res := GatherOperations(m, nil) | ||
require.Contains(t, res, "id1") | ||
require.NotContains(t, res, "GetPth1") | ||
require.Contains(t, res, "PostPth2") | ||
}) | ||
} |
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