Improve testing. (#3)

* Improve testing.
This commit is contained in:
Lukasz Raczylo
2021-05-14 22:42:27 +01:00
committed by GitHub
parent 9a6f2e1034
commit 4540e85684
3 changed files with 42 additions and 22 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/compress v1.12.2 // indirect github.com/klauspost/compress v1.12.2 // indirect
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.7.5 github.com/tidwall/gjson v1.8.0
github.com/valyala/fasthttp v1.24.0 github.com/valyala/fasthttp v1.24.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
) )
+2 -2
View File
@@ -13,8 +13,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.7.5 h1:zmAN/xmX7OtpAkv4Ovfso60r/BiCi5IErCDYGNJu+uc= github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
github.com/tidwall/gjson v1.7.5/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk= github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE= github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8= github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
+39 -19
View File
@@ -2,13 +2,36 @@ package gql
import ( import (
"fmt" "fmt"
"net/http"
"net/http/httptest"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
) )
func Test_queryBuilder(t *testing.T) { type TestSuite struct {
suite.Suite
}
func (suite *TestSuite) SetupTest() {
os.Unsetenv("GRAPHQL_ENDPOINT")
}
func TestSuiteRun(t *testing.T) {
suite.Run(t, new(TestSuite))
}
func mockGraphQLServerResponses(responseQuery string) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(responseQuery))
}))
GraphQLUrl = server.URL + "/v1/graphql"
return server
}
func (suite *TestSuite) Test_queryBuilder() {
type args struct { type args struct {
data string data string
variables interface{} variables interface{}
@@ -38,23 +61,23 @@ func Test_queryBuilder(t *testing.T) {
want: `{"query":"query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) { tbl_user_group_admins(where: {is_admin: {_eq: true}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) { id is_admin } }","variables":{"GroupID":11007,"UserID":37}}`, want: `{"query":"query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) { tbl_user_group_admins(where: {is_admin: {_eq: true}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) { id is_admin } }","variables":{"GroupID":11007,"UserID":37}}`,
}, },
} }
GraphQLUrl = "http://hasura.local/v1/graphql"
assert := assert.New(t)
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { suite.T().Run(tt.name, func(t *testing.T) {
gets, err := queryBuilder(tt.args.data, tt.args.variables) gets, err := queryBuilder(tt.args.data, tt.args.variables)
assert.Equal(tt.want, string(gets), "Unexpected query output in test: "+tt.name) assert.Equal(suite.T(), tt.want, string(gets), "Unexpected query output in test: "+tt.name)
assert.Nil(err) assert.Nil(suite.T(), err)
}, },
) )
} }
} }
func Test_queryAgainstDatabaseExecution(t *testing.T) { func (suite *TestSuite) Test_queryAgainstDatabaseExecution() {
if testing.Short() { os.Unsetenv("GRAPHQL_ENDPOINT")
t.Skip("Skipping test in short / CI mode") jsonResponse := `{
} "data": {"tbl_user_group_admins":[{"id":109,"is_admin":true}]}
assert := assert.New(t) }`
server := mockGraphQLServerResponses(jsonResponse)
defer server.Close()
headers := map[string]interface{}{ headers := map[string]interface{}{
"x-hasura-user-id": 37, "x-hasura-user-id": 37,
"x-hasura-user-uuid": "bde3262e-b42e-4151-ac10-d43fb38f44a5", "x-hasura-user-uuid": "bde3262e-b42e-4151-ac10-d43fb38f44a5",
@@ -70,13 +93,12 @@ func Test_queryAgainstDatabaseExecution(t *testing.T) {
is_admin is_admin
} }
}` }`
GraphQLUrl = "http://hasura.local/v1/graphql"
result, err := Query(query, variables, headers) result, err := Query(query, variables, headers)
if err != nil { if err != nil {
t.Log("Query execution errored. Is GQL server up?") suite.T().Log("Query execution errored. Is GQL server up?")
} }
expected := `{"tbl_user_group_admins":[{"id":109,"is_admin":true}]}` expected := `{"tbl_user_group_admins":[{"id":109,"is_admin":true}]}`
assert.Equal(expected, string(result), "Query result execution should be equal") assert.Equal(suite.T(), expected, string(result), "Query result execution should be equal")
} }
func ExampleQuery() { func ExampleQuery() {
@@ -99,7 +121,7 @@ func ExampleQuery() {
fmt.Println(result) fmt.Println(result)
} }
func Test_initialize(t *testing.T) { func (suite *TestSuite) Test_initialize() {
tests := []struct { tests := []struct {
name string name string
expected string expected string
@@ -133,10 +155,9 @@ func Test_initialize(t *testing.T) {
expected: "hasura-def.local/v1/graphql", expected: "hasura-def.local/v1/graphql",
}, },
} }
assert := assert.New(t)
backupEnv, present := os.LookupEnv("GRAPHQL_ENDPOINT") backupEnv, present := os.LookupEnv("GRAPHQL_ENDPOINT")
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { suite.T().Run(tt.name, func(t *testing.T) {
os.Unsetenv("GRAPHQL_ENDPOINT") os.Unsetenv("GRAPHQL_ENDPOINT")
GraphQLUrl = "" GraphQLUrl = ""
if tt.env_endpoint != "" { if tt.env_endpoint != "" {
@@ -148,8 +169,7 @@ func Test_initialize(t *testing.T) {
GraphQLUrl = tt.local_endpoint GraphQLUrl = tt.local_endpoint
} }
prepare() prepare()
assert.Equal(tt.expected, GraphQLUrl, "Unexpected value of env variable: "+tt.name) assert.Equal(suite.T(), tt.expected, GraphQLUrl, "Unexpected value of env variable: "+tt.name)
os.Unsetenv("GRAPHQL_ENDPOINT")
}) })
} }
if present { if present {