/
asserts.go
37 lines (31 loc) · 1020 Bytes
/
asserts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package gqltesting
import (
"encoding/json"
"github.com/chirino/graphql"
"github.com/stretchr/testify/assert"
"testing"
)
func AssertQuery(t *testing.T, engine *graphql.Engine, query string, expected string) {
request := graphql.Request{}
request.Query = query
AssertRequest(t, engine, request, expected)
}
func AssertRequestString(t *testing.T, engine *graphql.Engine, req string, expected string) {
request := graphql.Request{}
jsonUnmarshal(t, req, &request)
AssertRequest(t, engine, request, expected)
}
func AssertRequest(t *testing.T, engine *graphql.Engine, request graphql.Request, expected string) {
response := engine.ServeGraphQL(&request)
actual := jsonMarshal(t, response)
assert.Equal(t, expected, actual)
}
func jsonMarshal(t *testing.T, value interface{}) string {
data, err := json.Marshal(value)
assert.NoError(t, err)
return string(data)
}
func jsonUnmarshal(t *testing.T, from string, target interface{}) {
err := json.Unmarshal([]byte(from), target)
assert.NoError(t, err)
}