Skip to content

Commit

Permalink
refactor(test): refactor o2k test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Oct 25, 2023
1 parent 1ffaa0a commit 6566253
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions openapi2kong/openapi2kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openapi2kong
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"strings"
"testing"
Expand All @@ -12,30 +13,44 @@ import (

const fixturePath = "./oas3_testfiles/"

func Test_Openapi2kong(t *testing.T) {
// findFilesBySuffix returns a list of files in the fixturePath
// that end with the given suffix.
func findFilesBySuffix(t *testing.T, suffix string) []fs.DirEntry {
files, err := os.ReadDir(fixturePath)
if err != nil {
t.Error("failed reading test data: %w", err)
}

// loop over all files, and remove anything that doesn't end with ".yaml"
for i := 0; i < len(files); i++ {
if !strings.HasSuffix(files[i].Name(), suffix) {
files = append(files[:i], files[i+1:]...)
i--
}
}

return files
}

func Test_Openapi2kong(t *testing.T) {
files := findFilesBySuffix(t, ".yaml")

for _, file := range files {
fileNameIn := file.Name()
if strings.HasSuffix(fileNameIn, ".yaml") {
fileNameExpected := strings.TrimSuffix(fileNameIn, ".yaml") + ".expected.json"
fileNameOut := strings.TrimSuffix(fileNameIn, ".yaml") + ".generated.json"
dataIn, _ := os.ReadFile(fixturePath + fileNameIn)
dataOut, err := Convert(dataIn, O2kOptions{
Tags: []string{"OAS3_import", "OAS3file_" + fileNameIn},
})
if err != nil {
t.Error(fmt.Sprintf("'%s' didn't expect error: %%w", fixturePath+fileNameIn), err)
} else {
JSONOut, _ := json.MarshalIndent(dataOut, "", " ")
os.WriteFile(fixturePath+fileNameOut, JSONOut, 0o600)
JSONExpected, _ := os.ReadFile(fixturePath + fileNameExpected)
assert.JSONEq(t, string(JSONExpected), string(JSONOut),
"'%s': the JSON blobs should be equal", fixturePath+fileNameIn)
}
fileNameExpected := strings.TrimSuffix(fileNameIn, ".yaml") + ".expected.json"
fileNameOut := strings.TrimSuffix(fileNameIn, ".yaml") + ".generated.json"
dataIn, _ := os.ReadFile(fixturePath + fileNameIn)
dataOut, err := Convert(dataIn, O2kOptions{
Tags: []string{"OAS3_import", "OAS3file_" + fileNameIn},
})
if err != nil {
t.Error(fmt.Sprintf("'%s' didn't expect error: %%w", fixturePath+fileNameIn), err)
} else {
JSONOut, _ := json.MarshalIndent(dataOut, "", " ")
os.WriteFile(fixturePath+fileNameOut, JSONOut, 0o600)
JSONExpected, _ := os.ReadFile(fixturePath + fileNameExpected)
assert.JSONEq(t, string(JSONExpected), string(JSONOut),
"'%s': the JSON blobs should be equal", fixturePath+fileNameIn)
}
}
}
Expand Down

0 comments on commit 6566253

Please sign in to comment.