Skip to content

Commit

Permalink
fixup some api tests, enable expected failures, run api tests in work…
Browse files Browse the repository at this point in the history
…flow

Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Aug 8, 2021
1 parent d545a85 commit d3a29ef
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/default.cue
Expand Up @@ -32,6 +32,11 @@ ghacue.#Workflow & {
env: {
GITHUB_TOKEN: "${secrets.GITHUB_TOKEN}"
}
},{
name: "Run extra tests"
run: """
hof test test/testers/api/postman.cue
"""
}]
}
}
2 changes: 2 additions & 0 deletions .github/workflows/default.yml
Expand Up @@ -31,4 +31,6 @@ jobs:
hof test test.cue -s lib -t test -t mod
env:
GITHUB_TOKEN: ${secrets.GITHUB_TOKEN}
- name: Run extra tests
run: hof test test/testers/api/postman.cue
services: {}
34 changes: 23 additions & 11 deletions lib/test/api.go
Expand Up @@ -17,7 +17,7 @@ import (
const HTTP2_GOAWAY_CHECK = "http2: server sent GOAWAY and closed the connection"

func RunAPI(T *Tester, verbose int) (err error) {
fmt.Println("api:", T.Name)
// fmt.Println("api:", T.Name)

// make sure we resolve references and unifications
val := T.Value.Eval()
Expand Down Expand Up @@ -49,7 +49,15 @@ func runCase(T *Tester, verbose int, val cue.Value) (err error) {
return err
}

err = checkResponse(T, verbose, actual, expected)

fail := val.LookupPath(cue.ParsePath("fail"))
failVal, err := fail.Bool()
if err != nil {
// likely not found
failVal = false
}

err = checkResponse(T, verbose, actual, expected, failVal)

return err
}
Expand All @@ -66,11 +74,12 @@ func buildRequest(T *Tester, verbose int, val cue.Value) (R *gorequest.SuperAgen
}

host := req.LookupPath(cue.ParsePath("host"))
path := req.LookupPath(cue.ParsePath("path"))
hostStr, err := host.String()
if err != nil {
return
}

path := req.LookupPath(cue.ParsePath("path"))
pathStr, err := path.String()
if err != nil {
return
Expand Down Expand Up @@ -203,12 +212,12 @@ func makeRequest(T *Tester, verbose int, R *gorequest.SuperAgent) (gorequest.Res
if verbose > 0 {
fmt.Println(body)
}
fmt.Println(body)
// fmt.Println(body)

return resp, nil
}

func checkResponse(T *Tester, verbose int, actual gorequest.Response, expect cue.Value) (err error) {
func checkResponse(T *Tester, verbose int, actual gorequest.Response, expect cue.Value, expectFail bool) (err error) {
expect = expect.Eval()

S, err := expect.Struct()
Expand All @@ -220,7 +229,7 @@ func checkResponse(T *Tester, verbose int, actual gorequest.Response, expect cue
label := iter.Label()
value := iter.Value()

fmt.Println("checking:", label)
// fmt.Println("checking:", label)

switch label {
case "status":
Expand Down Expand Up @@ -250,14 +259,17 @@ func checkResponse(T *Tester, verbose int, actual gorequest.Response, expect cue
// TODO: bi-directional subsume to check for equality?
result := value.Unify(V)
if result.Err() != nil {
return result.Err()
if !expectFail {
return result.Err()
}
}
fmt.Println("result: ", result)
// fmt.Println("result: ", result)
err = result.Validate()
if err != nil {
fmt.Println(value)

return err
if !expectFail {
fmt.Println(value)
return err
}
}


Expand Down
26 changes: 21 additions & 5 deletions test/testers/api-test.cue → test/testers/api/postman.cue
Expand Up @@ -41,21 +41,37 @@ basic: {

post: _ @test(api,basic,post)
post: {
#args: {
foo: "bar"
req: basePost & {
data: {
cow: "moo"
}
}
resp: {
status: 200
body: {
json: close({
cow: "moo"
})
...
}
}
}

fail: _ @test(api,basic,fail)
fail: {
fail: true
req: basePost & {
data: {
cow: "moo"
foo: "bar"
}
}
resp: {
status: 200
body: {
args: #args
json: {
json: close({
cow: "moo"
}
})
...
}
}
Expand Down

0 comments on commit d3a29ef

Please sign in to comment.