tercul-backend/internal/app/localization/service_test.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

94 lines
2.6 KiB
Go

package localization
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockLocalizationRepository struct {
mock.Mock
}
func (m *mockLocalizationRepository) GetTranslation(ctx context.Context, key string, language string) (string, error) {
args := m.Called(ctx, key, language)
return args.String(0), args.Error(1)
}
func (m *mockLocalizationRepository) GetTranslations(ctx context.Context, keys []string, language string) (map[string]string, error) {
args := m.Called(ctx, keys, language)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(map[string]string), args.Error(1)
}
func (m *mockLocalizationRepository) GetAuthorBiography(ctx context.Context, authorID uint, language string) (string, error) {
args := m.Called(ctx, authorID, language)
return args.String(0), args.Error(1)
}
func (m *mockLocalizationRepository) GetWorkContent(ctx context.Context, workID uint, language string) (string, error) {
args := m.Called(ctx, workID, language)
return args.String(0), args.Error(1)
}
func TestLocalizationService_GetTranslation(t *testing.T) {
repo := new(mockLocalizationRepository)
service := NewService(repo)
ctx := context.Background()
key := "test_key"
language := "en"
expectedTranslation := "Test Translation"
repo.On("GetTranslation", ctx, key, language).Return(expectedTranslation, nil)
translation, err := service.Queries.GetTranslation(ctx, key, language)
assert.NoError(t, err)
assert.Equal(t, expectedTranslation, translation)
repo.AssertExpectations(t)
}
func TestLocalizationService_GetTranslations(t *testing.T) {
repo := new(mockLocalizationRepository)
service := NewService(repo)
ctx := context.Background()
keys := []string{"key1", "key2"}
language := "en"
expectedTranslations := map[string]string{
"key1": "Translation 1",
"key2": "Translation 2",
}
repo.On("GetTranslations", ctx, keys, language).Return(expectedTranslations, nil)
translations, err := service.Queries.GetTranslations(ctx, keys, language)
assert.NoError(t, err)
assert.Equal(t, expectedTranslations, translations)
repo.AssertExpectations(t)
}
func TestLocalizationService_GetAuthorBiography(t *testing.T) {
repo := new(mockLocalizationRepository)
service := NewService(repo)
ctx := context.Background()
authorID := uint(1)
language := "en"
expectedBiography := "This is a test biography."
repo.On("GetAuthorBiography", ctx, authorID, language).Return(expectedBiography, nil)
biography, err := service.Queries.GetAuthorBiography(ctx, authorID, language)
assert.NoError(t, err)
assert.Equal(t, expectedBiography, biography)
repo.AssertExpectations(t)
}