Skip to content

Commit

Permalink
Merge pull request #53 from seipan/feat/array-responcheck
Browse files Browse the repository at this point in the history
配列型のjsonのレスポンスチェックに対応させる
  • Loading branch information
takanakahiko authored Nov 15, 2023
2 parents 15f450f + 8e6172c commit 7ed91be
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
22 changes: 22 additions & 0 deletions example/template/main_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,25 @@
status: 200
params:
firstname: $atgenRegister[people.people[0].firstname]

- name: TestGetAllPerson
routerFunc: ../handlers.GetRouter
apiVersions:
- v1
tests:
- path: /{apiVersion}/people
method: get
res:
status: 200
register: people
- path: /{apiVersion}/people/all
method: get
res:
status: 200
paramsArray:
- id: "1"
firstname: John
lastname: Doe
- id: "2"
firstname: Jane
lastname: Doe
24 changes: 16 additions & 8 deletions example/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,29 @@ func TestTeamplate(t *testing.T) {
}

params := make(map[string]interface{})

arrayparams := make([]map[string]interface{}, 0)
atgenResParams := map[string]interface{}{}
atgenResParamsArray := []map[string]interface{}{}
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
resBody := buf.String()
if resBody != "" {
err := json.Unmarshal([]byte(resBody), &params)
if err != nil {
t.Fatal(err)
// array
if len(atgenResParamsArray) > 0 {
err := json.Unmarshal([]byte(resBody), &arrayparams)
if err != nil {
t.Fatal(err)
}
checkCompareArray(t, arrayparams, atgenResParamsArray)
} else {
err := json.Unmarshal([]byte(resBody), &params)
if err != nil {
t.Fatal(err)
}
checkCompare(t, params, atgenResParams)
}
}

// This is replaced with req.params defined in YAML
atgenResParams := map[string]interface{}{}
checkCompare(t, params, atgenResParams)

atgenRegister["atgenRegisterKey"] = params
}

Expand Down
13 changes: 13 additions & 0 deletions example/template/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ func checkCompare(t *testing.T, actual, expected interface{}) {
}
}
}

func checkCompareArray(t *testing.T, actual, expected interface{}) {
t.Helper()

if actual == nil {
t.Fatalf("Expected response should include %#v, but actually %#v", expected, actual)
}

a := actual.([]map[string]interface{})
for i, e := range expected.([]map[string]interface{}) {
checkCompare(t, a[i], e)
}
}
3 changes: 3 additions & 0 deletions lib/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ func rewriteTestNode(n ast.Node, test Test) (ast.Node, error) {
case "atgenResParams":
p, _ := parser.ParseExpr(fmt.Sprintf("%#v", test.Res.Params))
cr.Replace(p)
case "atgenResParamsArray":
p, _ := parser.ParseExpr(fmt.Sprintf("%#v", test.Res.ParamsArray))
cr.Replace(p)
case "atgenTestVars":
h, _ := parser.ParseExpr(fmt.Sprintf("%#v", test.Vars))
cr.Replace(h)
Expand Down
26 changes: 23 additions & 3 deletions lib/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,16 @@ func convertToRes(r interface{}) (Res, error) {
return Res{}, err
}

paramsArray, err := convertToParamsArray(res["paramsArray"])
if err != nil {
return Res{}, err
}

return Res{
Status: res["status"].(int),
Params: params,
Headers: headers,
Status: res["status"].(int),
Params: params,
Headers: headers,
ParamsArray: paramsArray,
}, nil
}

Expand Down Expand Up @@ -421,6 +427,20 @@ func convertToParams(p interface{}) (map[string]interface{}, error) {
return params, nil
}

func convertToParamsArray(p interface{}) ([]map[string]interface{}, error) {
params := make([]map[string]interface{}, 0)
if p != nil {
for _, v := range p.([]interface{}) {
p, err := convertToParams(v)
if err != nil {
return params, err
}
params = append(params, p)
}
}
return params, nil
}

func convertToHeaders(h interface{}) (map[string]string, error) {
headers := make(map[string]string)
if h != nil {
Expand Down
7 changes: 4 additions & 3 deletions lib/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ const (

// Res is a response status, parameters and headers which a test should get
type Res struct {
Status int
Params map[string]interface{}
Headers map[string]string
Status int
Params map[string]interface{}
ParamsArray []map[string]interface{}
Headers map[string]string
}

// Tester is an interface for Test and Subtest
Expand Down

0 comments on commit 7ed91be

Please sign in to comment.