tercul-backend/internal/app/localization/service_test.go
google-labs-jules[bot] 1c4dcbcf99 Refactor: Introduce service layer for application logic
This change introduces a service layer to encapsulate the business logic
for each domain aggregate. This will make the code more modular,
testable, and easier to maintain.

The following services have been created:
- author
- bookmark
- category
- collection
- comment
- like
- tag
- translation
- user

The main Application struct has been updated to use these new services.
The integration test suite has also been updated to use the new
Application struct and services.

This is a work in progress. The next step is to fix the compilation
errors and then refactor the resolvers to use the new services.
2025-09-09 02:28:25 +00:00

66 lines
1.7 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 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.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.GetTranslations(ctx, keys, language)
assert.NoError(t, err)
assert.Equal(t, expectedTranslations, translations)
repo.AssertExpectations(t)
}