From cbd102baef9c23fadb4e06eb6c29b3bed245dd46 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Sat, 27 Mar 2021 07:46:19 +0000 Subject: [PATCH] Dry the tests and produced json. --- Makefile | 2 +- gql.go | 9 +++++++- gql_test.go | 64 +++++++++++++++++++++++++++++++---------------------- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 8e64ed0..0b056f2 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/gql.go b/gql.go index 61489dc..63efc3a 100644 --- a/gql.go +++ b/gql.go @@ -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 { diff --git a/gql_test.go b/gql_test.go index fa87684..1e732ce 100644 --- a/gql_test.go +++ b/gql_test.go @@ -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) {