Dry the tests and produced json.

This commit is contained in:
2021-03-27 09:11:37 +00:00
parent 6b59e24be5
commit cbd102baef
3 changed files with 47 additions and 28 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ prepare:
go get -u -v github.com/go-critic/go-critic/cmd/gocritic
test:
gocritic check
# gocritic check
go test $(ADDITIONAL_BUILD_FLAGS) -v -coverprofile cover.out -memprofile mem.out
lint:
+8 -1
View File
@@ -26,7 +26,14 @@ func queryBuilder(data string, variables interface{}) []byte {
fmt.Println(err)
panic("Unable to process the query")
}
return j.Bytes()
// Attemt to decrease size of the generated JSON
compactedBuffer := new(bytes.Buffer)
err := json.Compact(compactedBuffer, j.Bytes())
if err != nil {
fmt.Println(err)
panic("Unable to compress json")
}
return []byte(compactedBuffer.String())
}
func Query(query string, variables interface{}, headers map[string]interface{}) string {
+38 -26
View File
@@ -1,38 +1,50 @@
package gql
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_simpleQueryBuilder(t *testing.T) {
assert := assert.New(t)
q := `query listUserBots {
tbl_bots {
bot_name
}
}`
result := queryBuilder(q, nil)
expected := `{"query":"query listUserBots {\n\t\ttbl_bots {\n \tbot_name\n \t}\n\t}","variables":null}`
assert.Equal(expected, string(result), "Produced simple query should be equal")
}
func Test_advancedQueryBuilder(t *testing.T) {
assert := assert.New(t)
q := `query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) {
tbl_user_group_admins(where: {is_admin: {_eq: "1"}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) {
id
is_admin
}
}`
v := map[string]interface{}{
"UserID": 37,
"GroupID": 11007,
func Test_queryBuilder(t *testing.T) {
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: "1"}, 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: \"1\"}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) { id is_admin } }","variables":{"GroupID":11007,"UserID":37}}`,
},
}
for _, tt := range tests {
assert := assert.New(t)
t.Run(tt.name, func(t *testing.T) {
gets := string(queryBuilder(tt.args.data, tt.args.variables))
assert.Equal(tt.want, gets, fmt.Sprintf("Unexpected query output in test %s", tt.name))
},
)
}
result := queryBuilder(q, v)
expected := `{"query":"query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) {\n\t\ttbl_user_group_admins(where: {is_admin: {_eq: \"1\"}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) {\n\t\t\tid\n\t\t\tis_admin\n\t\t}\n\t}","variables":{"GroupID":11007,"UserID":37}}`
assert.Equal(expected, string(result), "Produced advanced query should be equal")
}
func Test_queryAgainstDatabaseExecution(t *testing.T) {