[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
This commit is contained in:
Beppe Catanese 2024-09-16 10:48:30 +02:00 committed by GitHub
parent 7dcaececf8
commit 2f179fe41d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 199 additions and 0 deletions

View File

@ -35,3 +35,27 @@ jobs:
- name: Run test - name: Run test
working-directory: ${{ matrix.sample }} working-directory: ${{ matrix.sample }}
run: go test -mod=mod -v 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

View File

@ -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

View File

@ -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")
}
})
}

View File

@ -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])
}
})
}

View File

@ -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])
}
})
}

View File

@ -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])
}
})
}

View File

@ -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()
}