92 lines
3.4 KiB
Go
92 lines
3.4 KiB
Go
package apigen
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestBusinessContract(t *testing.T) {
|
|
spec, err := Parse(filepath.Join(frameworkRoot(t), "examples/contracts/business.api"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c, err := Compile(spec)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var doc map[string]any
|
|
if err = json.Unmarshal(c.Document, &doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
paths := doc["paths"].(map[string]any)
|
|
body := paths["/optional"].(map[string]any)["post"].(map[string]any)["requestBody"].(map[string]any)
|
|
if body["required"] != false {
|
|
t.Fatal("optional body documented as required")
|
|
}
|
|
for _, path := range []string{"/empty", "/absent"} {
|
|
if paths[path].(map[string]any)["post"].(map[string]any)["x-omni-request-policy"] == nil {
|
|
t.Fatal("missing policy metadata")
|
|
}
|
|
}
|
|
}
|
|
func TestBusinessGeneratedProject(t *testing.T) {
|
|
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") != "1" {
|
|
t.Skip("set GO_OMNI_SCAFFOLD_INTEGRATION=1")
|
|
}
|
|
dir := filepath.Join(t.TempDir(), "business")
|
|
o := Options{Dir: dir, API: filepath.Join(frameworkRoot(t), "examples/contracts/business.api"), Module: "example.com/business", FrameworkDir: frameworkRoot(t)}
|
|
if err := generateManagedTestProject(t, o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"upload", "nullable", "nothing", "rawNull", "rawArray", "empty", "absent", "optional", "echo", "tolerant", "noContent"} {
|
|
writeFile(t, filepath.Join(dir, "internal/logic/demo/"+name+".go"), "package demo\n")
|
|
}
|
|
writeFile(t, filepath.Join(dir, "internal/logic/demo/list.go"), readFile(t, "testdata/business/operations.go"))
|
|
writeFile(t, filepath.Join(dir, "consumer_test.go"), readFile(t, "testdata/business/consumer_test.go"))
|
|
writeFile(t, filepath.Join(dir, "internal/middleware/accessFactory.go"), readFile(t, "testdata/business/accessFactory.go"))
|
|
writeFile(t, filepath.Join(dir, "internal/svc/client.go"), readFile(t, "testdata/business/client.go"))
|
|
svcFile := filepath.Join(dir, "internal/svc/serviceContext.go")
|
|
source := strings.Replace(readFile(t, svcFile), "type ServiceContext struct {", "type ServiceContext struct {\n Client *accessClient", 1)
|
|
source = strings.Replace(source, "return nil", "return c.initAccess(ctx)", 1)
|
|
writeFile(t, svcFile, source)
|
|
o.API = filepath.Join(dir, "api/business.api")
|
|
o.Check = true
|
|
if err := generateManagedTestProject(t, o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "go", "test", "-race", "-mod=mod", "-count=1", "./...")
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(), "GOWORK=off")
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("%v\n%s", err, out)
|
|
}
|
|
if os.Getenv("GO_OMNI_RELEASE_CLI") != "" {
|
|
cmd = exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(), "GOWORK=off")
|
|
raw, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatal(err, string(raw))
|
|
}
|
|
var module struct {
|
|
Version, Sum string
|
|
Replace any
|
|
}
|
|
if err = json.Unmarshal(raw, &module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if module.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || module.Replace != nil || module.Sum == "" {
|
|
t.Fatal("not a fixed remote consumer")
|
|
}
|
|
t.Logf("remote framework %s checksum %s; no local replace", module.Version, module.Sum)
|
|
}
|
|
}
|