Skip to content

Commit a55b86b

Browse files
authored
feat: support to print the JSON schema of the TestSuite struct (#31)
1 parent 578e64b commit a55b86b

File tree

6 files changed

+78
-21
lines changed

6 files changed

+78
-21
lines changed

cmd/jsonschema.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/invopop/jsonschema"
7+
"github.com/linuxsuren/api-testing/pkg/testing"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func createJSONSchemaCmd() (c *cobra.Command) {
12+
c = &cobra.Command{
13+
Use: "json",
14+
Short: "Print the JSON schema of the test suites struct",
15+
RunE: func(cmd *cobra.Command, args []string) (err error) {
16+
var data []byte
17+
schema := jsonschema.Reflect(&testing.TestSuite{})
18+
if data, err = json.MarshalIndent(schema, "", " "); err == nil {
19+
cmd.Println(string(data))
20+
}
21+
return
22+
},
23+
}
24+
return
25+
}

cmd/jsonschema_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd_test
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
8+
"github.com/linuxsuren/api-testing/cmd"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestJSONSchemaCmd(t *testing.T) {
13+
c := cmd.NewRootCmd()
14+
15+
buf := new(bytes.Buffer)
16+
c.SetOut(buf)
17+
18+
c.SetArgs([]string{"json"})
19+
err := c.Execute()
20+
assert.Nil(t, err)
21+
assert.True(t, strings.Contains(buf.String(), "schema"))
22+
}

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"os"
5+
46
"github.com/linuxsuren/api-testing/pkg/version"
57
"github.com/spf13/cobra"
68
)
@@ -11,9 +13,10 @@ func NewRootCmd() (c *cobra.Command) {
1113
Use: "atest",
1214
Short: "API testing tool",
1315
}
16+
c.SetOut(os.Stdout)
1417
c.Version = version.GetVersion()
1518
c.AddCommand(createInitCommand(),
1619
createRunCommand(), createSampleCmd(),
17-
createServerCmd())
20+
createServerCmd(), createJSONSchemaCmd())
1821
return
1922
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ require (
2424
github.com/google/uuid v1.3.0 // indirect
2525
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
2626
github.com/huandu/xstrings v1.3.3 // indirect
27+
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
2728
github.com/imdario/mergo v0.3.11 // indirect
2829
github.com/inconshreveable/mousetrap v1.0.1 // indirect
30+
github.com/invopop/jsonschema v0.7.0 // indirect
2931
github.com/mitchellh/copystructure v1.0.0 // indirect
3032
github.com/mitchellh/reflectwalk v1.0.0 // indirect
3133
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,13 +543,17 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
543543
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
544544
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
545545
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
546+
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
547+
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
546548
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
547549
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
548550
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
549551
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
550552
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
551553
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
552554
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
555+
github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So=
556+
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
553557
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
554558
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
555559
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+
598602
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
599603
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
600604
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
605+
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
601606
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
602607
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
603608
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

pkg/testing/case.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ package testing
22

33
// TestSuite represents a set of test cases
44
type TestSuite struct {
5-
Name string `yaml:"name"`
6-
API string `yaml:"api"`
7-
Items []TestCase `yaml:"items"`
5+
Name string `yaml:"name" json:"name"`
6+
API string `yaml:"api,omitempty" json:"api,omitempty"`
7+
Items []TestCase `yaml:"items" json:"items"`
88
}
99

1010
// TestCase represents a test case
1111
type TestCase struct {
12-
Name string
12+
Name string `yaml:"name" json:"name"`
1313
Group string
14-
Prepare Prepare `yaml:"prepare"`
15-
Request Request `yaml:"request"`
16-
Expect Response `yaml:"expect"`
17-
Clean Clean `yaml:"clean"`
14+
Prepare Prepare `yaml:"prepare" json:"-"`
15+
Request Request `yaml:"request" json:"request"`
16+
Expect Response `yaml:"expect" json:"expect"`
17+
Clean Clean `yaml:"clean" json:"-"`
1818
}
1919

2020
// Prepare does the prepare work
@@ -24,22 +24,22 @@ type Prepare struct {
2424

2525
// Request represents a HTTP request
2626
type Request struct {
27-
API string `yaml:"api"`
28-
Method string `yaml:"method"`
29-
Query map[string]string `yaml:"query"`
30-
Header map[string]string `yaml:"header"`
31-
Form map[string]string `yaml:"form"`
32-
Body string `yaml:"body"`
33-
BodyFromFile string `yaml:"bodyFromFile"`
27+
API string `yaml:"api" json:"api"`
28+
Method string `yaml:"method,omitempty" json:"method,omitempty" jsonschema:"enum=GET,enum=POST,enum=PUT,enum=DELETE"`
29+
Query map[string]string `yaml:"query" json:"query,omitempty"`
30+
Header map[string]string `yaml:"header" json:"header,omitempty"`
31+
Form map[string]string `yaml:"form" json:"form,omitempty"`
32+
Body string `yaml:"body" json:"body,omitempty"`
33+
BodyFromFile string `yaml:"bodyFromFile" json:"bodyFromFile,omitempty"`
3434
}
3535

3636
// Response is the expected response
3737
type Response struct {
38-
StatusCode int `yaml:"statusCode"`
39-
Body string `yaml:"body"`
40-
Header map[string]string `yaml:"header"`
41-
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect"`
42-
Verify []string `yaml:"verify"`
38+
StatusCode int `yaml:"statusCode" json:"bodyFromFile,omitempty"`
39+
Body string `yaml:"body" json:"body,omitempty"`
40+
Header map[string]string `yaml:"header" json:"header,omitempty"`
41+
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect" json:"bodyFieldsExpect,omitempty"`
42+
Verify []string `yaml:"verify" json:"verify,omitempty"`
4343
}
4444

4545
// Clean represents the clean work after testing

0 commit comments

Comments
 (0)