mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit refactors the GraphQL test suite to resolve persistent build failures and establish a stable, mock-based unit testing environment. The key changes include: - Consolidating all GraphQL test helper functions into a single, canonical file (`internal/adapters/graphql/graphql_test_utils_test.go`). - Removing duplicated test helper code from `integration_test.go` and other test files. - Creating a new, dedicated unit test file for the `like` and `unlike` mutations (`internal/adapters/graphql/like_resolvers_unit_test.go`) using a mock-based approach. - Introducing mock services (`MockLikeService`, `MockAnalyticsService`) and updating mock repositories (`MockLikeRepository`, `MockWorkRepository`) in the `internal/testutil` package to support `testify/mock`. - Adding a `ContextWithUserID` helper function to `internal/platform/auth/middleware.go` to facilitate testing of authenticated resolvers. These changes resolve the `redeclared in this block` and package collision errors, resulting in a clean and passing test suite. This provides a solid foundation for future Test-Driven Development.
97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package graphql_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
)
|
|
|
|
// GraphQLRequest represents a GraphQL request
|
|
type GraphQLRequest struct {
|
|
Query string `json:"query"`
|
|
OperationName string `json:"operationName,omitempty"`
|
|
Variables map[string]interface{} `json:"variables,omitempty"`
|
|
}
|
|
|
|
// GraphQLResponse represents a generic GraphQL response
|
|
type GraphQLResponse[T any] struct {
|
|
Data T `json:"data,omitempty"`
|
|
Errors []map[string]interface{} `json:"errors,omitempty"`
|
|
}
|
|
|
|
// graphQLTestServer defines the interface for a test server that can execute GraphQL requests.
|
|
type graphQLTestServer interface {
|
|
getURL() string
|
|
getClient() *http.Client
|
|
}
|
|
|
|
// executeGraphQL executes a GraphQL query against a test server and decodes the response.
|
|
func executeGraphQL[T any](s graphQLTestServer, query string, variables map[string]interface{}, token *string) (*GraphQLResponse[T], error) {
|
|
request := GraphQLRequest{
|
|
Query: query,
|
|
Variables: variables,
|
|
}
|
|
|
|
requestBody, err := json.Marshal(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", s.getURL(), bytes.NewBuffer(requestBody))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if token != nil {
|
|
req.Header.Set("Authorization", "Bearer "+*token)
|
|
}
|
|
|
|
resp, err := s.getClient().Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var response GraphQLResponse[T]
|
|
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &response, nil
|
|
}
|
|
|
|
// Implement the graphQLTestServer interface for GraphQLIntegrationSuite
|
|
func (s *GraphQLIntegrationSuite) getURL() string {
|
|
return s.server.URL
|
|
}
|
|
|
|
func (s *GraphQLIntegrationSuite) getClient() *http.Client {
|
|
return s.client
|
|
}
|
|
|
|
// MockGraphQLServer provides a mock server for unit tests that don't require the full integration suite.
|
|
type MockGraphQLServer struct {
|
|
Server *httptest.Server
|
|
Client *http.Client
|
|
}
|
|
|
|
func NewMockGraphQLServer(h http.Handler) *MockGraphQLServer {
|
|
ts := httptest.NewServer(h)
|
|
return &MockGraphQLServer{
|
|
Server: ts,
|
|
Client: ts.Client(),
|
|
}
|
|
}
|
|
|
|
func (s *MockGraphQLServer) getURL() string {
|
|
return s.Server.URL
|
|
}
|
|
|
|
func (s *MockGraphQLServer) getClient() *http.Client {
|
|
return s.Client
|
|
}
|
|
|
|
func (s *MockGraphQLServer) Close() {
|
|
s.Server.Close()
|
|
} |