diff --git a/helpers_test.go b/helpers_test.go index 51092b2..3304b7c 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -1,6 +1,9 @@ package gql -import "testing" +import ( + "reflect" + "testing" +) func (suite *Tests) Test_searchForKeysInMapStringInterface() { type args struct { @@ -41,3 +44,63 @@ func (suite *Tests) Test_searchForKeysInMapStringInterface() { }) } } + +func (suite *Tests) TestBaseClient_decodeResponse() { + type args struct { + response []byte + } + tests := []struct { + name string + args args + want any + wantErr bool + setType string + wantType string + }{ + { + name: "TestBaseClient_decodeResponse_mapstring", + args: args{ + response: []byte(`{"data":{"viewer":{"login":"lukaszraczylo"}}}`), + }, + want: map[string]interface{}{ + "data": map[string]interface{}{ + "viewer": map[string]interface{}{ + "login": "lukaszraczylo", + }, + }, + }, + setType: "mapstring", + wantType: "map[string]interface {}", + }, + { + name: "TestBaseClient_decodeResponse_string", + args: args{ + response: []byte(`{"data":{"viewer":{"login":"lukaszraczylo"}}}`), + }, + want: `{"data":{"viewer":{"login":"lukaszraczylo"}}}`, + setType: "string", + wantType: "string", + }, + { + name: "TestBaseClient_decodeResponse_byte", + args: args{ + response: []byte(`{"data":{"viewer":{"login":"lukaszraczylo"}}}`), + }, + want: []byte(`{"data":{"viewer":{"login":"lukaszraczylo"}}}`), + setType: "byte", + wantType: "[]uint8", + }, + } + for _, tt := range tests { + suite.T().Run(tt.name, func(t *testing.T) { + b := NewConnection() + b.SetOutput(tt.setType) + got, err := b.decodeResponse(tt.args.response) + if (err != nil) != tt.wantErr { + t.Errorf("decodeResponse() error = %v, wantErr %v", err, tt.wantErr) + return + } + assert.Equal(tt.wantType, reflect.TypeOf(got).String()) + }) + } +} diff --git a/query_test.go b/query_test.go index fb82a40..1790d79 100644 --- a/query_test.go +++ b/query_test.go @@ -235,6 +235,20 @@ func (suite *Tests) TestBaseClient_Query() { wantReturned_value: []byte(`{"__type":null}`), wantErr: false, }, + { + name: "TestBaseClient_Query_tgbotapp_valid_query_string", + graphQLServer: "https://telegram-bot.app/v1/graphql", + wantType: "string", + args: args{ + query: `query { + __type(name: "Query") { + name + } + }`, + }, + wantReturned_value: string(`{"__type":null}`), + wantErr: false, + }, { name: "TestBaseClient_Query_tgbotapp_valid_query_invalid_type", graphQLServer: "https://telegram-bot.app/v1/graphql",