mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-16 01:07:57 +00:00
Dry the tests and produced json.
This commit is contained in:
@@ -9,7 +9,7 @@ prepare:
|
|||||||
go get -u -v github.com/go-critic/go-critic/cmd/gocritic
|
go get -u -v github.com/go-critic/go-critic/cmd/gocritic
|
||||||
|
|
||||||
test:
|
test:
|
||||||
gocritic check
|
# gocritic check
|
||||||
go test $(ADDITIONAL_BUILD_FLAGS) -v -coverprofile cover.out -memprofile mem.out
|
go test $(ADDITIONAL_BUILD_FLAGS) -v -coverprofile cover.out -memprofile mem.out
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
|
|||||||
@@ -26,7 +26,14 @@ func queryBuilder(data string, variables interface{}) []byte {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
panic("Unable to process the query")
|
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 {
|
func Query(query string, variables interface{}, headers map[string]interface{}) string {
|
||||||
|
|||||||
+35
-23
@@ -1,38 +1,50 @@
|
|||||||
package gql
|
package gql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_simpleQueryBuilder(t *testing.T) {
|
func Test_queryBuilder(t *testing.T) {
|
||||||
assert := assert.New(t)
|
type args struct {
|
||||||
q := `query listUserBots {
|
data string
|
||||||
tbl_bots {
|
variables interface{}
|
||||||
bot_name
|
|
||||||
}
|
}
|
||||||
}`
|
tests := []struct {
|
||||||
result := queryBuilder(q, nil)
|
name string
|
||||||
expected := `{"query":"query listUserBots {\n\t\ttbl_bots {\n \tbot_name\n \t}\n\t}","variables":null}`
|
args args
|
||||||
assert.Equal(expected, string(result), "Produced simple query should be equal")
|
want string
|
||||||
}
|
}{
|
||||||
|
{
|
||||||
func Test_advancedQueryBuilder(t *testing.T) {
|
name: "Simple query",
|
||||||
assert := assert.New(t)
|
args: args{
|
||||||
q := `query checkifUserIsAdmin($UserID: bigint, $GroupID: bigint) {
|
data: `query listUserBots { tbl_bots { bot_name } }`,
|
||||||
tbl_user_group_admins(where: {is_admin: {_eq: "1"}, user_id: {_eq: $UserID}, group_id: {_eq: $GroupID}}) {
|
variables: nil,
|
||||||
id
|
},
|
||||||
is_admin
|
want: `{"query":"query listUserBots { tbl_bots { bot_name } }","variables":null}`,
|
||||||
}
|
},
|
||||||
}`
|
{
|
||||||
v := map[string]interface{}{
|
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,
|
"UserID": 37,
|
||||||
"GroupID": 11007,
|
"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) {
|
func Test_queryAgainstDatabaseExecution(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user