From 2f179fe41d30e14583de23a41295e8ee033478f7 Mon Sep 17 00:00:00 2001 From: Beppe Catanese <1771700+gcatanese@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:48:30 +0200 Subject: [PATCH] [Go] Verify content of Go server generated files (samples) (#19504) * Add Python tests to verify generates Go files * Run Python tests during CI * Remove Python tests * Add Go tests * Update workflow to run Go tests --- .github/workflows/samples-go.yaml | 24 +++++++++ .../go-api-server/samples_tests/README.md | 3 ++ .../samples_tests/api_pet_test.go | 26 ++++++++++ .../samples_tests/go_mod_test.go | 35 +++++++++++++ .../samples_tests/model_pet_test.go | 22 ++++++++ .../samples_tests/routers_test.go | 38 ++++++++++++++ .../samples_tests/utils/fileutils.go | 51 +++++++++++++++++++ 7 files changed, 199 insertions(+) create mode 100644 samples/server/petstore/go-api-server/samples_tests/README.md create mode 100644 samples/server/petstore/go-api-server/samples_tests/api_pet_test.go create mode 100644 samples/server/petstore/go-api-server/samples_tests/go_mod_test.go create mode 100644 samples/server/petstore/go-api-server/samples_tests/model_pet_test.go create mode 100644 samples/server/petstore/go-api-server/samples_tests/routers_test.go create mode 100644 samples/server/petstore/go-api-server/samples_tests/utils/fileutils.go diff --git a/.github/workflows/samples-go.yaml b/.github/workflows/samples-go.yaml index c1b8973ba06..19858a90bb7 100644 --- a/.github/workflows/samples-go.yaml +++ b/.github/workflows/samples-go.yaml @@ -35,3 +35,27 @@ jobs: - name: Run test working-directory: ${{ matrix.sample }} run: go test -mod=mod -v + + verify: + name: Verify generated Go files with Go tests + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + sample: + - samples/server/petstore/go-api-server/ + go-version: + - "1.18" + steps: + - uses: actions/checkout@v4 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go-version }} + - name: Install Dependencies + working-directory: ${{ matrix.sample }} + run: | + go mod tidy + - name: Run tests + working-directory: ${{ matrix.sample }} + run: go test ./samples_tests -v \ No newline at end of file diff --git a/samples/server/petstore/go-api-server/samples_tests/README.md b/samples/server/petstore/go-api-server/samples_tests/README.md new file mode 100644 index 00000000000..ebddd916582 --- /dev/null +++ b/samples/server/petstore/go-api-server/samples_tests/README.md @@ -0,0 +1,3 @@ +This folder contains the tests to verify the correctness of the generate samples + +Those tests are not generated by the OpenAPI Generator \ No newline at end of file diff --git a/samples/server/petstore/go-api-server/samples_tests/api_pet_test.go b/samples/server/petstore/go-api-server/samples_tests/api_pet_test.go new file mode 100644 index 00000000000..c66ba7531d1 --- /dev/null +++ b/samples/server/petstore/go-api-server/samples_tests/api_pet_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "strings" + "testing" + + "github.com/GIT_USER_ID/GIT_REPO_ID/samples_tests/utils" +) + +func Test_API_Pet(t *testing.T) { + + t.Run("Check DeletePet route exists", func(t *testing.T) { + + filepath := "../go/api_pet.go" + + expected := ("\t\t\"DeletePet\": Route{\n" + + "\t\t\tstrings.ToUpper(\"Delete\"),\n" + + "\t\t\t\"/v2/pet/{petId}\",\n" + + "\t\t\tc.DeletePet,\n" + + "\t\t}") + + if !strings.Contains(utils.ReadContent(filepath), expected) { + t.Errorf("Route was not found in the file") + } + }) +} diff --git a/samples/server/petstore/go-api-server/samples_tests/go_mod_test.go b/samples/server/petstore/go-api-server/samples_tests/go_mod_test.go new file mode 100644 index 00000000000..75c769eb2bc --- /dev/null +++ b/samples/server/petstore/go-api-server/samples_tests/go_mod_test.go @@ -0,0 +1,35 @@ +package main + +import ( + "testing" + + "github.com/GIT_USER_ID/GIT_REPO_ID/samples_tests/utils" +) + +func Test_Go_Mod(t *testing.T) { + + t.Run("Check module", func(t *testing.T) { + + filepath := "../go.mod" + + lines := utils.ReadLines(filepath) + expected := "module github.com/GIT_USER_ID/GIT_REPO_ID" + + if lines[0] != expected { + t.Errorf("Expected '%s', but got '%s'", expected, lines[0]) + } + }) + + t.Run("Check Go version", func(t *testing.T) { + + filepath := "../go.mod" + + lines := utils.ReadLines(filepath) + expected := "go 1.18" + + if lines[2] != expected { + t.Errorf("Expected '%s', but got '%s'", expected, lines[2]) + } + }) + +} diff --git a/samples/server/petstore/go-api-server/samples_tests/model_pet_test.go b/samples/server/petstore/go-api-server/samples_tests/model_pet_test.go new file mode 100644 index 00000000000..5835268b81d --- /dev/null +++ b/samples/server/petstore/go-api-server/samples_tests/model_pet_test.go @@ -0,0 +1,22 @@ +package main + +import ( + "testing" + + "github.com/GIT_USER_ID/GIT_REPO_ID/samples_tests/utils" +) + +func Test_Model_Pet(t *testing.T) { + + t.Run("Check Pet model exists", func(t *testing.T) { + + filepath := "../go/model_pet.go" + + lines := utils.ReadLines(filepath) + expected := "\tId int64 `json:\"id,omitempty\"`" + + if lines[18] != expected { + t.Errorf("Expected '%s', but got '%s'", expected, lines[18]) + } + }) +} diff --git a/samples/server/petstore/go-api-server/samples_tests/routers_test.go b/samples/server/petstore/go-api-server/samples_tests/routers_test.go new file mode 100644 index 00000000000..6c2686fb86d --- /dev/null +++ b/samples/server/petstore/go-api-server/samples_tests/routers_test.go @@ -0,0 +1,38 @@ +package main + +import ( + "testing" + "strings" + + "github.com/GIT_USER_ID/GIT_REPO_ID/samples_tests/utils" +) + +func Test_Routers(t *testing.T) { + + t.Run("Check struct Route exists", func(t *testing.T) { + + filepath := "../go/routers.go" + + expected := ("type Route struct {\n" + + "\tMethod\t string\n" + + "\tPattern\t string\n" + + "\tHandlerFunc http.HandlerFunc\n" + + "}") + + if !strings.Contains(utils.ReadContent(filepath), expected) { + t.Errorf("Type Route was not found in the file") + } + }) + + t.Run("Check map Routes exists", func(t *testing.T) { + + filepath := "../go/routers.go" + + lines := utils.ReadLines(filepath) + expected := "type Routes map[string]Route" + + if lines[34] != expected { + t.Errorf("Expected '%s', but got '%s'", expected, lines[34]) + } + }) +} diff --git a/samples/server/petstore/go-api-server/samples_tests/utils/fileutils.go b/samples/server/petstore/go-api-server/samples_tests/utils/fileutils.go new file mode 100644 index 00000000000..6253ed8da28 --- /dev/null +++ b/samples/server/petstore/go-api-server/samples_tests/utils/fileutils.go @@ -0,0 +1,51 @@ +package utils + +import ( + "bufio" + "os" + "strings" +) + +// Load file content as array of lines +func ReadLines(filePath string) ([]string) { + var lines []string + + file, err := os.Open(filePath) + if err != nil { + panic("Cannot open file " + filePath); + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + + if err := scanner.Err(); err != nil { + panic("Cannot scan file " + filePath); + } + + return lines +} + +// Load file content as string +func ReadContent(filePath string) (string) { + file, err := os.Open(filePath) + if err != nil { + panic("Cannot open file " + filePath); + } + defer file.Close() + + var contentBuilder strings.Builder + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + contentBuilder.WriteString(scanner.Text() + "\n") + } + + if err := scanner.Err(); err != nil { + panic("Cannot scan file " + filePath); + } + + return contentBuilder.String() +} \ No newline at end of file