add tests
This commit is contained in:
276
tests/graphql/basic_test.go
Normal file
276
tests/graphql/basic_test.go
Normal file
@@ -0,0 +1,276 @@
|
||||
package graphql
|
||||
|
||||
import (
|
||||
"omega-server/local/service"
|
||||
"omega-server/tests"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestGraphQLBasic(t *testing.T) {
|
||||
// Initialize test suite
|
||||
testSuite := tests.NewTestSuite()
|
||||
testSuite.Setup(t)
|
||||
defer testSuite.Teardown(t)
|
||||
|
||||
// Skip if no test database
|
||||
testSuite.SkipIfNoTestDB(t)
|
||||
|
||||
// Create membership service
|
||||
membershipService := createMembershipService(t, testSuite.DB)
|
||||
|
||||
// Create GraphQL test utils
|
||||
gqlUtils := NewGraphQLTestUtils(testSuite.DB, membershipService)
|
||||
|
||||
t.Run("GetSchema", func(t *testing.T) {
|
||||
schema := gqlUtils.GetSchema(t)
|
||||
if schema == "" {
|
||||
t.Fatal("Expected non-empty GraphQL schema")
|
||||
}
|
||||
|
||||
// Check for basic types
|
||||
if !contains(schema, "type User") {
|
||||
t.Error("Schema should contain User type")
|
||||
}
|
||||
if !contains(schema, "type Project") {
|
||||
t.Error("Schema should contain Project type")
|
||||
}
|
||||
if !contains(schema, "type Task") {
|
||||
t.Error("Schema should contain Task type")
|
||||
}
|
||||
if !contains(schema, "type Query") {
|
||||
t.Error("Schema should contain Query type")
|
||||
}
|
||||
if !contains(schema, "type Mutation") {
|
||||
t.Error("Schema should contain Mutation type")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CreateUser", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteCreateUserMutation(t, "test@example.com", "password123", "Test User")
|
||||
|
||||
// Should not have errors for valid user creation
|
||||
gqlUtils.AssertNoErrors(t, response)
|
||||
gqlUtils.AssertDataNotNil(t, response)
|
||||
|
||||
// Extract user data
|
||||
userEmail := gqlUtils.ExtractString(t, response, "createUser", "email")
|
||||
userFullName := gqlUtils.ExtractString(t, response, "createUser", "fullName")
|
||||
userId := gqlUtils.ExtractString(t, response, "createUser", "id")
|
||||
|
||||
if userEmail != "test@example.com" {
|
||||
t.Errorf("Expected email 'test@example.com', got '%s'", userEmail)
|
||||
}
|
||||
if userFullName != "Test User" {
|
||||
t.Errorf("Expected full name 'Test User', got '%s'", userFullName)
|
||||
}
|
||||
if userId == "" {
|
||||
t.Error("Expected non-empty user ID")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CreateUserWithInvalidEmail", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteCreateUserMutation(t, "invalid-email", "password123", "Test User")
|
||||
|
||||
// Should have errors for invalid email
|
||||
gqlUtils.AssertHasErrors(t, response)
|
||||
})
|
||||
|
||||
t.Run("Login", func(t *testing.T) {
|
||||
// First create a user
|
||||
createResponse := gqlUtils.ExecuteCreateUserMutation(t, "login@example.com", "password123", "Login User")
|
||||
gqlUtils.AssertNoErrors(t, createResponse)
|
||||
|
||||
// Then try to login
|
||||
loginResponse := gqlUtils.ExecuteLoginMutation(t, "login@example.com", "password123")
|
||||
|
||||
gqlUtils.AssertNoErrors(t, loginResponse)
|
||||
gqlUtils.AssertDataNotNil(t, loginResponse)
|
||||
|
||||
// Extract token and user data
|
||||
token := gqlUtils.ExtractString(t, loginResponse, "login", "token")
|
||||
userEmail := gqlUtils.ExtractString(t, loginResponse, "login", "user", "email")
|
||||
|
||||
if token == "" {
|
||||
t.Error("Expected non-empty token")
|
||||
}
|
||||
if userEmail != "login@example.com" {
|
||||
t.Errorf("Expected email 'login@example.com', got '%s'", userEmail)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("LoginWithInvalidCredentials", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteLoginMutation(t, "nonexistent@example.com", "wrongpassword")
|
||||
|
||||
// Should have errors for invalid credentials
|
||||
gqlUtils.AssertHasErrors(t, response)
|
||||
gqlUtils.AssertErrorMessage(t, response, "Invalid credentials")
|
||||
})
|
||||
|
||||
t.Run("MeQuery", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteMeQuery(t)
|
||||
|
||||
// Should return mock user data
|
||||
gqlUtils.AssertNoErrors(t, response)
|
||||
gqlUtils.AssertDataNotNil(t, response)
|
||||
|
||||
// Extract user data
|
||||
userEmail := gqlUtils.ExtractString(t, response, "me", "email")
|
||||
userId := gqlUtils.ExtractString(t, response, "me", "id")
|
||||
|
||||
if userEmail == "" {
|
||||
t.Error("Expected non-empty email")
|
||||
}
|
||||
if userId == "" {
|
||||
t.Error("Expected non-empty user ID")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("UsersQuery", func(t *testing.T) {
|
||||
// Create a few test users first
|
||||
gqlUtils.ExecuteCreateUserMutation(t, "user1@example.com", "password123", "User One")
|
||||
gqlUtils.ExecuteCreateUserMutation(t, "user2@example.com", "password123", "User Two")
|
||||
|
||||
response := gqlUtils.ExecuteUsersQuery(t)
|
||||
|
||||
gqlUtils.AssertNoErrors(t, response)
|
||||
gqlUtils.AssertDataNotNil(t, response)
|
||||
|
||||
// Extract users array
|
||||
users := gqlUtils.ExtractArray(t, response, "users")
|
||||
|
||||
// Should have at least the users we created (plus possibly the admin user)
|
||||
if len(users) < 2 {
|
||||
t.Errorf("Expected at least 2 users, got %d", len(users))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CreateProject", func(t *testing.T) {
|
||||
// Create a user first to be the owner
|
||||
userResponse := gqlUtils.ExecuteCreateUserMutation(t, "owner@example.com", "password123", "Project Owner")
|
||||
gqlUtils.AssertNoErrors(t, userResponse)
|
||||
ownerID := gqlUtils.ExtractString(t, userResponse, "createUser", "id")
|
||||
|
||||
response := gqlUtils.ExecuteCreateProjectMutation(t, "Test Project", "A test project", ownerID)
|
||||
|
||||
gqlUtils.AssertNoErrors(t, response)
|
||||
gqlUtils.AssertDataNotNil(t, response)
|
||||
|
||||
// Extract project data
|
||||
projectName := gqlUtils.ExtractString(t, response, "createProject", "name")
|
||||
projectOwnerID := gqlUtils.ExtractString(t, response, "createProject", "ownerId")
|
||||
projectID := gqlUtils.ExtractString(t, response, "createProject", "id")
|
||||
|
||||
if projectName != "Test Project" {
|
||||
t.Errorf("Expected project name 'Test Project', got '%s'", projectName)
|
||||
}
|
||||
if projectOwnerID != ownerID {
|
||||
t.Errorf("Expected owner ID '%s', got '%s'", ownerID, projectOwnerID)
|
||||
}
|
||||
if projectID == "" {
|
||||
t.Error("Expected non-empty project ID")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ProjectsQuery", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteProjectsQuery(t)
|
||||
|
||||
// Should return empty array for mock implementation
|
||||
gqlUtils.AssertNoErrors(t, response)
|
||||
gqlUtils.AssertDataNotNil(t, response)
|
||||
|
||||
projects := gqlUtils.ExtractArray(t, response, "projects")
|
||||
// Mock implementation returns empty array
|
||||
if len(projects) != 0 {
|
||||
t.Errorf("Expected 0 projects from mock implementation, got %d", len(projects))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CreateTask", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteCreateTaskMutation(t, "Test Task", "A test task", "todo", "medium", "project-id")
|
||||
|
||||
gqlUtils.AssertNoErrors(t, response)
|
||||
gqlUtils.AssertDataNotNil(t, response)
|
||||
|
||||
// Extract task data
|
||||
taskTitle := gqlUtils.ExtractString(t, response, "createTask", "title")
|
||||
taskStatus := gqlUtils.ExtractString(t, response, "createTask", "status")
|
||||
taskPriority := gqlUtils.ExtractString(t, response, "createTask", "priority")
|
||||
taskID := gqlUtils.ExtractString(t, response, "createTask", "id")
|
||||
|
||||
if taskTitle != "Test Task" {
|
||||
t.Errorf("Expected task title 'Test Task', got '%s'", taskTitle)
|
||||
}
|
||||
if taskStatus != "todo" {
|
||||
t.Errorf("Expected task status 'todo', got '%s'", taskStatus)
|
||||
}
|
||||
if taskPriority != "medium" {
|
||||
t.Errorf("Expected task priority 'medium', got '%s'", taskPriority)
|
||||
}
|
||||
if taskID == "" {
|
||||
t.Error("Expected non-empty task ID")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TasksQuery", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteTasksQuery(t, nil)
|
||||
|
||||
// Should return empty array for mock implementation
|
||||
gqlUtils.AssertNoErrors(t, response)
|
||||
gqlUtils.AssertDataNotNil(t, response)
|
||||
|
||||
tasks := gqlUtils.ExtractArray(t, response, "tasks")
|
||||
// Mock implementation returns empty array
|
||||
if len(tasks) != 0 {
|
||||
t.Errorf("Expected 0 tasks from mock implementation, got %d", len(tasks))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidQuery", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteQuery(t, "invalid query syntax", nil)
|
||||
|
||||
// Should have errors for invalid query
|
||||
gqlUtils.AssertHasErrors(t, response)
|
||||
})
|
||||
|
||||
t.Run("UnsupportedQuery", func(t *testing.T) {
|
||||
response := gqlUtils.ExecuteQuery(t, "query { unsupportedField }", nil)
|
||||
|
||||
// Should have errors for unsupported query
|
||||
gqlUtils.AssertHasErrors(t, response)
|
||||
gqlUtils.AssertErrorMessage(t, response, "Query not supported")
|
||||
})
|
||||
}
|
||||
|
||||
// createMembershipService creates a membership service for testing
|
||||
func createMembershipService(t *testing.T, db *gorm.DB) *service.MembershipService {
|
||||
// This would normally involve creating repository and other dependencies
|
||||
// For now, we'll create a basic service that works with our test setup
|
||||
// Note: This is a simplified version for testing
|
||||
|
||||
// In a real implementation, you would:
|
||||
// 1. Create repository with DB
|
||||
// 2. Set up all dependencies
|
||||
// 3. Configure the service properly
|
||||
|
||||
// For this test, we'll return nil and handle it in the GraphQL handler
|
||||
// The handler should gracefully handle the basic operations we're testing
|
||||
return nil
|
||||
}
|
||||
|
||||
// contains checks if a string contains a substring
|
||||
func contains(str, substr string) bool {
|
||||
return len(str) >= len(substr) && (str == substr || len(substr) == 0 ||
|
||||
(len(substr) > 0 && findSubstring(str, substr)))
|
||||
}
|
||||
|
||||
// findSubstring finds if substr exists in str
|
||||
func findSubstring(str, substr string) bool {
|
||||
for i := 0; i <= len(str)-len(substr); i++ {
|
||||
if str[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
427
tests/graphql/graphql_test_utils.go
Normal file
427
tests/graphql/graphql_test_utils.go
Normal file
@@ -0,0 +1,427 @@
|
||||
package graphql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"omega-server/local/graphql/handler"
|
||||
"omega-server/local/service"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GraphQLTestUtils provides utilities for testing GraphQL endpoints
|
||||
type GraphQLTestUtils struct {
|
||||
App *fiber.App
|
||||
Handler *handler.GraphQLHandler
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
// GraphQLTestRequest represents a GraphQL test request
|
||||
type GraphQLTestRequest struct {
|
||||
Query string `json:"query"`
|
||||
Variables map[string]interface{} `json:"variables,omitempty"`
|
||||
}
|
||||
|
||||
// GraphQLTestResponse represents a GraphQL test response
|
||||
type GraphQLTestResponse struct {
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Errors []GraphQLError `json:"errors,omitempty"`
|
||||
}
|
||||
|
||||
// GraphQLError represents a GraphQL error in test responses
|
||||
type GraphQLError struct {
|
||||
Message string `json:"message"`
|
||||
Path []string `json:"path,omitempty"`
|
||||
}
|
||||
|
||||
// NewGraphQLTestUtils creates a new GraphQL test utilities instance
|
||||
func NewGraphQLTestUtils(db *gorm.DB, membershipService *service.MembershipService) *GraphQLTestUtils {
|
||||
app := fiber.New(fiber.Config{
|
||||
DisableStartupMessage: true,
|
||||
})
|
||||
|
||||
graphqlHandler := handler.NewGraphQLHandler(membershipService)
|
||||
app.Post("/graphql", graphqlHandler.Handle)
|
||||
app.Get("/graphql", func(c *fiber.Ctx) error {
|
||||
return c.SendString(graphqlHandler.GetSchema())
|
||||
})
|
||||
|
||||
return &GraphQLTestUtils{
|
||||
App: app,
|
||||
Handler: graphqlHandler,
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
// ExecuteQuery executes a GraphQL query and returns the response
|
||||
func (gtu *GraphQLTestUtils) ExecuteQuery(t *testing.T, query string, variables map[string]interface{}) *GraphQLTestResponse {
|
||||
request := GraphQLTestRequest{
|
||||
Query: query,
|
||||
Variables: variables,
|
||||
}
|
||||
|
||||
body, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal GraphQL request: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/graphql", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := gtu.App.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to execute GraphQL request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var response GraphQLTestResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
||||
t.Fatalf("Failed to decode GraphQL response: %v", err)
|
||||
}
|
||||
|
||||
return &response
|
||||
}
|
||||
|
||||
// ExecuteQueryWithContext executes a GraphQL query with context
|
||||
func (gtu *GraphQLTestUtils) ExecuteQueryWithContext(t *testing.T, ctx context.Context, query string, variables map[string]interface{}) *GraphQLTestResponse {
|
||||
request := GraphQLTestRequest{
|
||||
Query: query,
|
||||
Variables: variables,
|
||||
}
|
||||
|
||||
body, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal GraphQL request: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/graphql", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
resp, err := gtu.App.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to execute GraphQL request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var response GraphQLTestResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
||||
t.Fatalf("Failed to decode GraphQL response: %v", err)
|
||||
}
|
||||
|
||||
return &response
|
||||
}
|
||||
|
||||
// ExecuteLoginMutation executes a login mutation
|
||||
func (gtu *GraphQLTestUtils) ExecuteLoginMutation(t *testing.T, email, password string) *GraphQLTestResponse {
|
||||
query := `
|
||||
mutation Login($email: String!, $password: String!) {
|
||||
login(input: {email: $email, password: $password}) {
|
||||
token
|
||||
user {
|
||||
id
|
||||
email
|
||||
fullName
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
variables := map[string]interface{}{
|
||||
"email": email,
|
||||
"password": password,
|
||||
}
|
||||
|
||||
return gtu.ExecuteQuery(t, query, variables)
|
||||
}
|
||||
|
||||
// ExecuteCreateUserMutation executes a create user mutation
|
||||
func (gtu *GraphQLTestUtils) ExecuteCreateUserMutation(t *testing.T, email, password, fullName string) *GraphQLTestResponse {
|
||||
query := `
|
||||
mutation CreateUser($email: String!, $password: String!, $fullName: String!) {
|
||||
createUser(input: {email: $email, password: $password, fullName: $fullName}) {
|
||||
id
|
||||
email
|
||||
fullName
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
variables := map[string]interface{}{
|
||||
"email": email,
|
||||
"password": password,
|
||||
"fullName": fullName,
|
||||
}
|
||||
|
||||
return gtu.ExecuteQuery(t, query, variables)
|
||||
}
|
||||
|
||||
// ExecuteMeQuery executes a me query
|
||||
func (gtu *GraphQLTestUtils) ExecuteMeQuery(t *testing.T) *GraphQLTestResponse {
|
||||
query := `
|
||||
query Me {
|
||||
me {
|
||||
id
|
||||
email
|
||||
fullName
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
return gtu.ExecuteQuery(t, query, nil)
|
||||
}
|
||||
|
||||
// ExecuteUsersQuery executes a users query
|
||||
func (gtu *GraphQLTestUtils) ExecuteUsersQuery(t *testing.T) *GraphQLTestResponse {
|
||||
query := `
|
||||
query Users {
|
||||
users {
|
||||
id
|
||||
email
|
||||
fullName
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
return gtu.ExecuteQuery(t, query, nil)
|
||||
}
|
||||
|
||||
// ExecuteUserQuery executes a user query by ID
|
||||
func (gtu *GraphQLTestUtils) ExecuteUserQuery(t *testing.T, userID string) *GraphQLTestResponse {
|
||||
query := `
|
||||
query User($id: String!) {
|
||||
user(id: $id) {
|
||||
id
|
||||
email
|
||||
fullName
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
variables := map[string]interface{}{
|
||||
"id": userID,
|
||||
}
|
||||
|
||||
return gtu.ExecuteQuery(t, query, variables)
|
||||
}
|
||||
|
||||
// ExecuteCreateProjectMutation executes a create project mutation
|
||||
func (gtu *GraphQLTestUtils) ExecuteCreateProjectMutation(t *testing.T, name, description, ownerID string) *GraphQLTestResponse {
|
||||
query := `
|
||||
mutation CreateProject($name: String!, $description: String, $ownerId: String!) {
|
||||
createProject(input: {name: $name, description: $description, ownerId: $ownerId}) {
|
||||
id
|
||||
name
|
||||
description
|
||||
ownerId
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
variables := map[string]interface{}{
|
||||
"name": name,
|
||||
"description": description,
|
||||
"ownerId": ownerID,
|
||||
}
|
||||
|
||||
return gtu.ExecuteQuery(t, query, variables)
|
||||
}
|
||||
|
||||
// ExecuteProjectsQuery executes a projects query
|
||||
func (gtu *GraphQLTestUtils) ExecuteProjectsQuery(t *testing.T) *GraphQLTestResponse {
|
||||
query := `
|
||||
query Projects {
|
||||
projects {
|
||||
id
|
||||
name
|
||||
description
|
||||
ownerId
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
return gtu.ExecuteQuery(t, query, nil)
|
||||
}
|
||||
|
||||
// ExecuteCreateTaskMutation executes a create task mutation
|
||||
func (gtu *GraphQLTestUtils) ExecuteCreateTaskMutation(t *testing.T, title, description, status, priority, projectID string) *GraphQLTestResponse {
|
||||
query := `
|
||||
mutation CreateTask($title: String!, $description: String, $status: String, $priority: String, $projectId: String!) {
|
||||
createTask(input: {title: $title, description: $description, status: $status, priority: $priority, projectId: $projectId}) {
|
||||
id
|
||||
title
|
||||
description
|
||||
status
|
||||
priority
|
||||
projectId
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
variables := map[string]interface{}{
|
||||
"title": title,
|
||||
"description": description,
|
||||
"status": status,
|
||||
"priority": priority,
|
||||
"projectId": projectID,
|
||||
}
|
||||
|
||||
return gtu.ExecuteQuery(t, query, variables)
|
||||
}
|
||||
|
||||
// ExecuteTasksQuery executes a tasks query
|
||||
func (gtu *GraphQLTestUtils) ExecuteTasksQuery(t *testing.T, projectID *string) *GraphQLTestResponse {
|
||||
query := `
|
||||
query Tasks($projectId: String) {
|
||||
tasks(projectId: $projectId) {
|
||||
id
|
||||
title
|
||||
description
|
||||
status
|
||||
priority
|
||||
projectId
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
variables := make(map[string]interface{})
|
||||
if projectID != nil {
|
||||
variables["projectId"] = *projectID
|
||||
}
|
||||
|
||||
return gtu.ExecuteQuery(t, query, variables)
|
||||
}
|
||||
|
||||
// GetSchema returns the GraphQL schema
|
||||
func (gtu *GraphQLTestUtils) GetSchema(t *testing.T) string {
|
||||
return gtu.Handler.GetSchema()
|
||||
}
|
||||
|
||||
// AssertNoErrors asserts that the GraphQL response has no errors
|
||||
func (gtu *GraphQLTestUtils) AssertNoErrors(t *testing.T, response *GraphQLTestResponse) {
|
||||
if len(response.Errors) > 0 {
|
||||
t.Fatalf("Expected no GraphQL errors, but got: %+v", response.Errors)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertHasErrors asserts that the GraphQL response has errors
|
||||
func (gtu *GraphQLTestUtils) AssertHasErrors(t *testing.T, response *GraphQLTestResponse) {
|
||||
if len(response.Errors) == 0 {
|
||||
t.Fatalf("Expected GraphQL errors, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
// AssertErrorMessage asserts that the GraphQL response contains a specific error message
|
||||
func (gtu *GraphQLTestUtils) AssertErrorMessage(t *testing.T, response *GraphQLTestResponse, expectedMessage string) {
|
||||
if len(response.Errors) == 0 {
|
||||
t.Fatalf("Expected GraphQL errors, but got none")
|
||||
}
|
||||
|
||||
for _, err := range response.Errors {
|
||||
if err.Message == expectedMessage {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
t.Fatalf("Expected error message '%s', but not found in errors: %+v", expectedMessage, response.Errors)
|
||||
}
|
||||
|
||||
// AssertDataNotNil asserts that the GraphQL response data is not nil
|
||||
func (gtu *GraphQLTestUtils) AssertDataNotNil(t *testing.T, response *GraphQLTestResponse) {
|
||||
if response.Data == nil {
|
||||
t.Fatalf("Expected GraphQL data to not be nil, but it was")
|
||||
}
|
||||
}
|
||||
|
||||
// AssertDataNil asserts that the GraphQL response data is nil
|
||||
func (gtu *GraphQLTestUtils) AssertDataNil(t *testing.T, response *GraphQLTestResponse) {
|
||||
if response.Data != nil {
|
||||
t.Fatalf("Expected GraphQL data to be nil, but got: %+v", response.Data)
|
||||
}
|
||||
}
|
||||
|
||||
// ExtractField extracts a field from the GraphQL response data
|
||||
func (gtu *GraphQLTestUtils) ExtractField(t *testing.T, response *GraphQLTestResponse, fieldPath ...string) interface{} {
|
||||
if response.Data == nil {
|
||||
t.Fatalf("Cannot extract field from nil data")
|
||||
}
|
||||
|
||||
data := response.Data
|
||||
for _, field := range fieldPath {
|
||||
if dataMap, ok := data.(map[string]interface{}); ok {
|
||||
if value, exists := dataMap[field]; exists {
|
||||
data = value
|
||||
} else {
|
||||
t.Fatalf("Field '%s' not found in data: %+v", field, dataMap)
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("Cannot extract field '%s' from non-map data: %+v", field, data)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// ExtractString extracts a string field from the GraphQL response data
|
||||
func (gtu *GraphQLTestUtils) ExtractString(t *testing.T, response *GraphQLTestResponse, fieldPath ...string) string {
|
||||
value := gtu.ExtractField(t, response, fieldPath...)
|
||||
if str, ok := value.(string); ok {
|
||||
return str
|
||||
}
|
||||
t.Fatalf("Expected string value for field %v, but got: %+v", fieldPath, value)
|
||||
return ""
|
||||
}
|
||||
|
||||
// ExtractInt extracts an integer field from the GraphQL response data
|
||||
func (gtu *GraphQLTestUtils) ExtractInt(t *testing.T, response *GraphQLTestResponse, fieldPath ...string) int {
|
||||
value := gtu.ExtractField(t, response, fieldPath...)
|
||||
if floatVal, ok := value.(float64); ok {
|
||||
return int(floatVal)
|
||||
}
|
||||
if intVal, ok := value.(int); ok {
|
||||
return intVal
|
||||
}
|
||||
t.Fatalf("Expected int value for field %v, but got: %+v", fieldPath, value)
|
||||
return 0
|
||||
}
|
||||
|
||||
// ExtractBool extracts a boolean field from the GraphQL response data
|
||||
func (gtu *GraphQLTestUtils) ExtractBool(t *testing.T, response *GraphQLTestResponse, fieldPath ...string) bool {
|
||||
value := gtu.ExtractField(t, response, fieldPath...)
|
||||
if boolVal, ok := value.(bool); ok {
|
||||
return boolVal
|
||||
}
|
||||
t.Fatalf("Expected bool value for field %v, but got: %+v", fieldPath, value)
|
||||
return false
|
||||
}
|
||||
|
||||
// ExtractArray extracts an array field from the GraphQL response data
|
||||
func (gtu *GraphQLTestUtils) ExtractArray(t *testing.T, response *GraphQLTestResponse, fieldPath ...string) []interface{} {
|
||||
value := gtu.ExtractField(t, response, fieldPath...)
|
||||
if arrVal, ok := value.([]interface{}); ok {
|
||||
return arrVal
|
||||
}
|
||||
t.Fatalf("Expected array value for field %v, but got: %+v", fieldPath, value)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user