This repository has been archived by the owner on Dec 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gql_test.go
179 lines (169 loc) · 4.85 KB
/
gql_test.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package gql
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/lukaszraczylo/pandati"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
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 {
data string
variables interface{}
}
tests := []struct {
name string
args args
want string
}{
{
name: "Simple query",
args: args{
data: `query listUserBots { tbl_bots { bot_name } }`,
variables: nil,
},
want: `{"query":"query listUserBots { tbl_bots { bot_name } }","variables":null}`,
},
{
name: "Advanced query",
args: args{
data: `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: map[string]interface{}{
"UserID": 37,
"GroupID": 11007,
},
},
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}}`,
},
}
for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) {
gets, err := queryBuilder(tt.args.data, tt.args.variables)
assert.Equal(suite.T(), tt.want, string(gets), "Unexpected query output in test: "+tt.name)
assert.Nil(suite.T(), err)
},
)
}
}
func (suite *TestSuite) Test_queryAgainstDatabaseExecution() {
os.Unsetenv("GRAPHQL_ENDPOINT")
jsonResponse := `{
"data": {"tbl_user_group_admins":[{"id":109,"is_admin":true}]}
}`
server := mockGraphQLServerResponses(jsonResponse)
defer server.Close()
headers := map[string]interface{}{
"x-hasura-user-id": 37,
"x-hasura-user-uuid": "bde3262e-b42e-4151-ac10-d43fb38f44a5",
"Authorization": "bearer LaPotatoDiBanani",
}
variables := map[string]interface{}{
"UserID": 37,
"GroupID": 11007,
}
var 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
}
}`
result, err := Query(query, variables, headers)
if err != nil {
suite.T().Log("Query execution errored. Is GQL server up?")
}
expected := `{"tbl_user_group_admins":[{"id":109,"is_admin":true}]}`
assert.Equal(suite.T(), expected, string(result), "Query result execution should be equal")
}
func ExampleQuery() {
variables := map[string]interface{}{
"fileHash": "123deadc0w321",
}
var query = `query searchFileKnown($fileHash: String) {
tbl_file_scans(where: {file_hash: {_eq: $fileHash}}) {
porn
racy
violence
virus
}
}`
result, err := Query(query, variables, nil)
if err != nil {
fmt.Println("Query error", err)
return
}
fmt.Println(result)
}
func (suite *TestSuite) Test_initialize() {
tests := []struct {
name string
expected string
env_endpoint string
local_endpoint string
}{
{
name: "Setting env variable for endpoint (ENV)",
env_endpoint: "new-hasura.local/v1/graphql",
expected: "new-hasura.local/v1/graphql",
},
{
name: "Setting default endpoint (ENV)",
env_endpoint: "",
local_endpoint: "",
expected: "http://127.0.0.1:9090/v1/graphql",
},
{
name: "Setting custom endpoint (ENV)",
env_endpoint: "http://127.0.0.1:8080/v1/graphql",
expected: "http://127.0.0.1:8080/v1/graphql",
},
{
name: "Setting custom endpoint (VAR)",
local_endpoint: "http://127.0.0.1:8090/v1/graphql",
expected: "http://127.0.0.1:8090/v1/graphql",
},
{
name: "Setting custom endpoint (VAR)",
local_endpoint: "hasura-def.local/v1/graphql",
expected: "hasura-def.local/v1/graphql",
},
}
backupEnv, present := os.LookupEnv("GRAPHQL_ENDPOINT")
for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) {
os.Unsetenv("GRAPHQL_ENDPOINT")
GraphQLUrl = ""
if !pandati.IsZero(tt.env_endpoint) {
os.Setenv("GRAPHQL_ENDPOINT", tt.env_endpoint)
t.Log(tt.env_endpoint, os.Getenv("GRAPHQL_ENDPOINT"))
}
if !pandati.IsZero(tt.local_endpoint) {
os.Unsetenv("GRAPHQL_ENDPOINT")
GraphQLUrl = tt.local_endpoint
}
prepare()
assert.Equal(suite.T(), tt.expected, GraphQLUrl, "Unexpected value of env variable: "+tt.name)
})
}
if present {
os.Setenv("GRAPHQL_ENDPOINT", backupEnv)
}
}