-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
7 changed files
with
199 additions
and
0 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
3 changes: 3 additions & 0 deletions
3
samples/server/petstore/go-api-server/samples_tests/README.md
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,3 @@ | ||
This folder contains the tests to verify the correctness of the generate samples | ||
|
||
Those tests are not generated by the OpenAPI Generator |
26 changes: 26 additions & 0 deletions
26
samples/server/petstore/go-api-server/samples_tests/api_pet_test.go
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,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
35
samples/server/petstore/go-api-server/samples_tests/go_mod_test.go
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,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]) | ||
} | ||
}) | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
samples/server/petstore/go-api-server/samples_tests/model_pet_test.go
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,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]) | ||
} | ||
}) | ||
} |
38 changes: 38 additions & 0 deletions
38
samples/server/petstore/go-api-server/samples_tests/routers_test.go
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,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]) | ||
} | ||
}) | ||
} |
51 changes: 51 additions & 0 deletions
51
samples/server/petstore/go-api-server/samples_tests/utils/fileutils.go
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,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() | ||
} |