Files
upstream-sqlc/internal/endtoend/json_schema_test.go
upstream-mirror 0f4df23290
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
2026-09-16 02:17:34 +05:00

83 lines
1.6 KiB
Go

package main
import (
"encoding/json"
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/xeipuuv/gojsonschema"
)
type conf struct {
Version string `json:"version"`
}
func loadSchema(t *testing.T, path string) *gojsonschema.Schema {
t.Helper()
schemaBytes, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
loader := gojsonschema.NewStringLoader(string(schemaBytes))
schema, err := gojsonschema.NewSchema(loader)
if err != nil {
t.Fatalf("invalid schema: %s", err)
}
return schema
}
func TestJsonSchema(t *testing.T) {
t.Parallel()
schemaOne := loadSchema(t, filepath.Join("..", "config", "v_one.json"))
schemaTwo := loadSchema(t, filepath.Join("..", "config", "v_two.json"))
srcs := []string{
filepath.Join("..", "..", "examples"),
filepath.Join("testdata"),
}
for _, dir := range srcs {
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if filepath.Base(path) != "sqlc.json" {
return nil
}
t.Run(path, func(t *testing.T) {
t.Parallel()
contents, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var c conf
if err := json.Unmarshal(contents, &c); err != nil {
t.Fatal(err)
}
l := gojsonschema.NewStringLoader(string(contents))
switch c.Version {
case "1":
if _, err := schemaOne.Validate(l); err != nil {
t.Fatal(err)
}
case "2":
if _, err := schemaTwo.Validate(l); err != nil {
t.Fatal(err)
}
default:
t.Fatalf("unknown schema version: %s", c.Version)
}
})
return nil
})
if err != nil {
t.Error(err)
}
}
}