tercul-backend/internal/app/like/commands.go
google-labs-jules[bot] 80cfe71e59 refactor: Refactor GraphQL tests to use mock-based unit tests
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.
2025-10-03 09:21:41 +00:00

45 lines
1.0 KiB
Go

package like
import (
"context"
"tercul/internal/domain"
)
// LikeCommands contains the command handlers for the like aggregate.
type LikeCommands struct {
repo domain.LikeRepository
}
// NewLikeCommands creates a new LikeCommands handler.
func NewLikeCommands(repo domain.LikeRepository) *LikeCommands {
return &LikeCommands{repo: repo}
}
// CreateLikeInput represents the input for creating a new like.
type CreateLikeInput struct {
UserID uint
WorkID *uint
TranslationID *uint
CommentID *uint
}
// CreateLike creates a new like.
func (c *LikeCommands) CreateLike(ctx context.Context, input CreateLikeInput) (*domain.Like, error) {
like := &domain.Like{
UserID: input.UserID,
WorkID: input.WorkID,
TranslationID: input.TranslationID,
CommentID: input.CommentID,
}
err := c.repo.Create(ctx, like)
if err != nil {
return nil, err
}
return like, nil
}
// DeleteLike deletes a like by ID.
func (c *LikeCommands) DeleteLike(ctx context.Context, id uint) error {
return c.repo.Delete(ctx, id)
}