From a4fda1dfbabf54cbd13d576374455db18a103a02 Mon Sep 17 00:00:00 2001 From: rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Thu, 6 Apr 2023 13:32:13 +0800 Subject: [PATCH] feat: support to print the JSON schema of the TestSuite struct --- cmd/jsonschema.go | 25 +++++++++++++++++++++++++ cmd/jsonschema_test.go | 22 ++++++++++++++++++++++ cmd/root.go | 5 ++++- go.mod | 2 ++ go.sum | 5 +++++ pkg/testing/case.go | 40 ++++++++++++++++++++-------------------- 6 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 cmd/jsonschema.go create mode 100644 cmd/jsonschema_test.go diff --git a/cmd/jsonschema.go b/cmd/jsonschema.go new file mode 100644 index 00000000..71604fe7 --- /dev/null +++ b/cmd/jsonschema.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "encoding/json" + + "github.com/invopop/jsonschema" + "github.com/linuxsuren/api-testing/pkg/testing" + "github.com/spf13/cobra" +) + +func createJSONSchemaCmd() (c *cobra.Command) { + c = &cobra.Command{ + Use: "json", + Short: "Print the JSON schema of the test suites struct", + RunE: func(cmd *cobra.Command, args []string) (err error) { + var data []byte + schema := jsonschema.Reflect(&testing.TestSuite{}) + if data, err = json.MarshalIndent(schema, "", " "); err == nil { + cmd.Println(string(data)) + } + return + }, + } + return +} diff --git a/cmd/jsonschema_test.go b/cmd/jsonschema_test.go new file mode 100644 index 00000000..03ff2632 --- /dev/null +++ b/cmd/jsonschema_test.go @@ -0,0 +1,22 @@ +package cmd_test + +import ( + "bytes" + "strings" + "testing" + + "github.com/linuxsuren/api-testing/cmd" + "github.com/stretchr/testify/assert" +) + +func TestJSONSchemaCmd(t *testing.T) { + c := cmd.NewRootCmd() + + buf := new(bytes.Buffer) + c.SetOut(buf) + + c.SetArgs([]string{"json"}) + err := c.Execute() + assert.Nil(t, err) + assert.True(t, strings.Contains(buf.String(), "schema")) +} diff --git a/cmd/root.go b/cmd/root.go index 45d97508..17710a33 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,8 @@ package cmd import ( + "os" + "github.com/linuxsuren/api-testing/pkg/version" "github.com/spf13/cobra" ) @@ -11,9 +13,10 @@ func NewRootCmd() (c *cobra.Command) { Use: "atest", Short: "API testing tool", } + c.SetOut(os.Stdout) c.Version = version.GetVersion() c.AddCommand(createInitCommand(), createRunCommand(), createSampleCmd(), - createServerCmd()) + createServerCmd(), createJSONSchemaCmd()) return } diff --git a/go.mod b/go.mod index 82fc3aed..ef0c8e1f 100644 --- a/go.mod +++ b/go.mod @@ -24,8 +24,10 @@ require ( github.com/google/uuid v1.3.0 // indirect github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect github.com/huandu/xstrings v1.3.3 // indirect + github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect github.com/imdario/mergo v0.3.11 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/invopop/jsonschema v0.7.0 // indirect github.com/mitchellh/copystructure v1.0.0 // indirect github.com/mitchellh/reflectwalk v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index 7be4bf4a..79b98615 100644 --- a/go.sum +++ b/go.sum @@ -543,6 +543,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4= github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk= +github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA= github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= @@ -550,6 +552,8 @@ github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So= +github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -598,6 +602,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/pkg/testing/case.go b/pkg/testing/case.go index c4311db2..7343ad1f 100644 --- a/pkg/testing/case.go +++ b/pkg/testing/case.go @@ -2,19 +2,19 @@ package testing // TestSuite represents a set of test cases type TestSuite struct { - Name string `yaml:"name"` - API string `yaml:"api"` - Items []TestCase `yaml:"items"` + Name string `yaml:"name" json:"name"` + API string `yaml:"api,omitempty" json:"api,omitempty"` + Items []TestCase `yaml:"items" json:"items"` } // TestCase represents a test case type TestCase struct { - Name string + Name string `yaml:"name" json:"name"` Group string - Prepare Prepare `yaml:"prepare"` - Request Request `yaml:"request"` - Expect Response `yaml:"expect"` - Clean Clean `yaml:"clean"` + Prepare Prepare `yaml:"prepare" json:"-"` + Request Request `yaml:"request" json:"request"` + Expect Response `yaml:"expect" json:"expect"` + Clean Clean `yaml:"clean" json:"-"` } // Prepare does the prepare work @@ -24,22 +24,22 @@ type Prepare struct { // Request represents a HTTP request type Request struct { - API string `yaml:"api"` - Method string `yaml:"method"` - Query map[string]string `yaml:"query"` - Header map[string]string `yaml:"header"` - Form map[string]string `yaml:"form"` - Body string `yaml:"body"` - BodyFromFile string `yaml:"bodyFromFile"` + API string `yaml:"api" json:"api"` + Method string `yaml:"method,omitempty" json:"method,omitempty" jsonschema:"enum=GET,enum=POST,enum=PUT,enum=DELETE"` + Query map[string]string `yaml:"query" json:"query,omitempty"` + Header map[string]string `yaml:"header" json:"header,omitempty"` + Form map[string]string `yaml:"form" json:"form,omitempty"` + Body string `yaml:"body" json:"body,omitempty"` + BodyFromFile string `yaml:"bodyFromFile" json:"bodyFromFile,omitempty"` } // Response is the expected response type Response struct { - StatusCode int `yaml:"statusCode"` - Body string `yaml:"body"` - Header map[string]string `yaml:"header"` - BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect"` - Verify []string `yaml:"verify"` + StatusCode int `yaml:"statusCode" json:"bodyFromFile,omitempty"` + Body string `yaml:"body" json:"body,omitempty"` + Header map[string]string `yaml:"header" json:"header,omitempty"` + BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect" json:"bodyFieldsExpect,omitempty"` + Verify []string `yaml:"verify" json:"verify,omitempty"` } // Clean represents the clean work after testing