Skip to content

Commit

Permalink
add more features to API testing, checking is a WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed Oct 16, 2020
1 parent 6d00945 commit 2a4b237
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 6 deletions.
92 changes: 92 additions & 0 deletions lib/test/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package test

import (
"fmt"
"io/ioutil"
"strings"
"time"

"cuelang.org/go/cue"
"cuelang.org/go/encoding/json"
"github.com/parnurzeal/gorequest"

"github.com/hofstadter-io/hof/lib/cuetils"
Expand Down Expand Up @@ -115,6 +118,64 @@ func buildRequest(T *Tester, verbose int, val cue.Value) (R *gorequest.SuperAgen
}
}

timeout := req.Lookup("timeout")
if timeout.Exists() {
to, err := timeout.String()
if err != nil {
return R, err
}
d, err := time.ParseDuration(to)
if err != nil {
return R, err
}
R.Timeout(d)
}

retry := req.Lookup("retry")
if retry.Exists() {
C := 3
count := retry.Lookup("count")
if count.Exists() {
c, err := count.Int64()
if err != nil {
return R, err
}
C = int(c)
}

D := time.Second * 6
timer := retry.Lookup("timer")
if timer.Exists() {
t, err := timer.String()
if err != nil {
return R, err
}
d, err := time.ParseDuration(t)
if err != nil {
return R, err
}
D = d
}

CS := []int{}
codes := retry.Lookup("codes")
if codes.Exists() {
L, err := codes.List()
if err != nil {
return R, err
}
for L.Next() {
v, err := L.Value().Int64()
if err != nil {
return R, err
}
CS = append(CS, int(v))
}
}

R.Retry(C, D, CS...)
}

return
}

Expand Down Expand Up @@ -158,6 +219,8 @@ func checkResponse(T *Tester, verbose int, actual gorequest.Response, expect cue
label := iter.Label()
value := iter.Value()

fmt.Println("checking:", label)

switch label {
case "status":
status, err := value.Int64()
Expand All @@ -167,6 +230,35 @@ func checkResponse(T *Tester, verbose int, actual gorequest.Response, expect cue
if int64(actual.StatusCode) != status {
return fmt.Errorf("status code mismatch %v != %v", actual.StatusCode, status)
}

case "body":
body, err := ioutil.ReadAll(actual.Body)
if err != nil {
return err
}

inst, err := json.Decode(T.CRT, "", body)
if err != nil {
return err
}

V := inst.Value()
result := value.Unify(V)
if result.Err() != nil {
return result.Err()
}
fmt.Println("result: ", result)
err = result.Validate()
if err != nil {
fmt.Println(value)
fmt.Println(inst.Value())

return err
}


default:
return fmt.Errorf("Unknown field in expected response:", label)
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/test/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func RunTestFromArgsFlags(args []string, cmdflags flags.TestFlagpole) (error) {
}

// Get test suites from top level
suites, err := getValueTestSuites(crt.CueValue, cmdflags.Suite)
suites, err := getValueTestSuites(crt.CueRuntime, crt.CueValue, cmdflags.Suite)
if err != nil {
return err
}

// find tests in suites
for s, suite := range suites {
ts, err := getValueTestSuiteTesters(suite.Value, cmdflags.Tester)
ts, err := getValueTestSuiteTesters(crt.CueRuntime, suite.Value, cmdflags.Tester)
if err != nil {
return err
}
Expand Down
14 changes: 10 additions & 4 deletions lib/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Suite struct {
// Name of the Suite (Cue field)
Name string

// Runtime the Cue values were built from
CRT *cue.Runtime

// Cue Value for the Suite
Value cue.Value

Expand All @@ -38,11 +41,11 @@ type Suite struct {
Errors []error
}

func getValueTestSuites(val cue.Value, labels []string) ([]Suite, error) {
func getValueTestSuites(crt *cue.Runtime, val cue.Value, labels []string) ([]Suite, error) {
vals, err := cuetils.GetByAttrKeys(val, "test", append(labels, "suite"), nil)
suites := []Suite{}
for _, v := range vals {
suites = append(suites, Suite{Name: v.Key, Value: v.Val})
suites = append(suites, Suite{Name: v.Key, CRT: crt, Value: v.Val})
}
return suites, err
}
Expand All @@ -55,6 +58,9 @@ type Tester struct {
// Type of the Tester (@test(key[0]))
Type string

// Runtime the Cue values were built from
CRT *cue.Runtime

// Cue Value for the Tester
Value cue.Value

Expand All @@ -68,7 +74,7 @@ type Tester struct {
Errors []error
}

func getValueTestSuiteTesters(val cue.Value, labels []string) ([]Tester, error) {
func getValueTestSuiteTesters(crt *cue.Runtime, val cue.Value, labels []string) ([]Tester, error) {
vals, err := cuetils.GetByAttrKeys(val, "test", labels, []string{})
testers := []Tester{}
for _, v := range vals {
Expand All @@ -77,7 +83,7 @@ func getValueTestSuiteTesters(val cue.Value, labels []string) ([]Tester, error)
if err != nil {
return testers, err
}
testers = append(testers, Tester{Name: v.Key, Type: typ, Value: v.Val})
testers = append(testers, Tester{Name: v.Key, Type: typ, CRT: crt, Value: v.Val})
}
return testers, err
}
Expand Down
15 changes: 15 additions & 0 deletions test/testers/api-test.cue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ basic: {
}
resp: {
status: 200
body: {
args: close({
cow: "moo"
})
...
}
}
}

Expand All @@ -42,6 +48,15 @@ basic: {
}
resp: {
status: 200
body: {
args: close({
foo: "bar"
})
json: {
cow: "moo"
}
...
}
}
}

Expand Down

0 comments on commit 2a4b237

Please sign in to comment.