Skip to content

Commit

Permalink
[Go] Verify content of Go server generated files (samples) (#19504)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gcatanese authored Sep 16, 2024
1 parent 7dcaece commit 2f179fe
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/samples-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions samples/server/petstore/go-api-server/samples_tests/README.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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")
}
})
}
35 changes: 35 additions & 0 deletions samples/server/petstore/go-api-server/samples_tests/go_mod_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
})

}
Original file line number Diff line number Diff line change
@@ -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])
}
})
}
Original file line number Diff line number Diff line change
@@ -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])
}
})
}
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 2f179fe

Please sign in to comment.