mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 00:31:35 +00:00
- Core Go application with GraphQL API using gqlgen - Comprehensive data models for literary works, authors, translations - Repository pattern with caching layer - Authentication and authorization system - Linguistics analysis capabilities with multiple adapters - Vector search integration with Weaviate - Docker containerization support - Python data migration and analysis scripts - Clean architecture with proper separation of concerns - Production-ready configuration and middleware - Proper .gitignore excluding vendor/, database files, and build artifacts
191 lines
5.7 KiB
Go
191 lines
5.7 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"tercul/models"
|
|
"tercul/repositories"
|
|
)
|
|
|
|
// MockTranslationRepository is an in-memory implementation of TranslationRepository
|
|
type MockTranslationRepository struct {
|
|
items []models.Translation
|
|
}
|
|
|
|
func NewMockTranslationRepository() *MockTranslationRepository {
|
|
return &MockTranslationRepository{items: []models.Translation{}}
|
|
}
|
|
|
|
var _ repositories.TranslationRepository = (*MockTranslationRepository)(nil)
|
|
|
|
// BaseRepository methods with context support
|
|
func (m *MockTranslationRepository) Create(ctx context.Context, t *models.Translation) error {
|
|
if t == nil {
|
|
return errors.New("nil translation")
|
|
}
|
|
t.ID = uint(len(m.items) + 1)
|
|
m.items = append(m.items, *t)
|
|
return nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) GetByID(ctx context.Context, id uint) (*models.Translation, error) {
|
|
for i := range m.items {
|
|
if m.items[i].ID == id {
|
|
cp := m.items[i]
|
|
return &cp, nil
|
|
}
|
|
}
|
|
return nil, repositories.ErrEntityNotFound
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Update(ctx context.Context, t *models.Translation) error {
|
|
for i := range m.items {
|
|
if m.items[i].ID == t.ID {
|
|
m.items[i] = *t
|
|
return nil
|
|
}
|
|
}
|
|
return repositories.ErrEntityNotFound
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Delete(ctx context.Context, id uint) error {
|
|
for i := range m.items {
|
|
if m.items[i].ID == id {
|
|
m.items = append(m.items[:i], m.items[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
return repositories.ErrEntityNotFound
|
|
}
|
|
|
|
func (m *MockTranslationRepository) List(ctx context.Context, page, pageSize int) (*repositories.PaginatedResult[models.Translation], error) {
|
|
all := append([]models.Translation(nil), m.items...)
|
|
total := int64(len(all))
|
|
start := (page - 1) * pageSize
|
|
end := start + pageSize
|
|
if start > len(all) {
|
|
return &repositories.PaginatedResult[models.Translation]{Items: []models.Translation{}, TotalCount: total}, nil
|
|
}
|
|
if end > len(all) {
|
|
end = len(all)
|
|
}
|
|
return &repositories.PaginatedResult[models.Translation]{Items: all[start:end], TotalCount: total}, nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListAll(ctx context.Context) ([]models.Translation, error) {
|
|
return append([]models.Translation(nil), m.items...), nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Count(ctx context.Context) (int64, error) {
|
|
return int64(len(m.items)), nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*models.Translation, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]models.Translation, error) {
|
|
all := append([]models.Translation(nil), m.items...)
|
|
end := offset + batchSize
|
|
if end > len(all) {
|
|
end = len(all)
|
|
}
|
|
if offset > len(all) {
|
|
return []models.Translation{}, nil
|
|
}
|
|
return all[offset:end], nil
|
|
}
|
|
|
|
// New BaseRepository methods
|
|
func (m *MockTranslationRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *models.Translation) error {
|
|
return m.Create(ctx, entity)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) GetByIDWithOptions(ctx context.Context, id uint, options *repositories.QueryOptions) (*models.Translation, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *models.Translation) error {
|
|
return m.Update(ctx, entity)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
|
|
return m.Delete(ctx, id)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListWithOptions(ctx context.Context, options *repositories.QueryOptions) ([]models.Translation, error) {
|
|
result, err := m.List(ctx, 1, 1000)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Items, nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) CountWithOptions(ctx context.Context, options *repositories.QueryOptions) (int64, error) {
|
|
return m.Count(ctx)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Exists(ctx context.Context, id uint) (bool, error) {
|
|
_, err := m.GetByID(ctx, id)
|
|
return err == nil, nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
|
|
return fn(nil)
|
|
}
|
|
|
|
// TranslationRepository specific methods
|
|
func (m *MockTranslationRepository) ListByWorkID(ctx context.Context, workID uint) ([]models.Translation, error) {
|
|
return m.ListByEntity(ctx, "Work", workID)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListByEntity(ctx context.Context, entityType string, entityID uint) ([]models.Translation, error) {
|
|
var out []models.Translation
|
|
for i := range m.items {
|
|
tr := m.items[i]
|
|
if tr.TranslatableType == entityType && tr.TranslatableID == entityID {
|
|
out = append(out, tr)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListByTranslatorID(ctx context.Context, translatorID uint) ([]models.Translation, error) {
|
|
var out []models.Translation
|
|
for i := range m.items {
|
|
if m.items[i].TranslatorID != nil && *m.items[i].TranslatorID == translatorID {
|
|
out = append(out, m.items[i])
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListByStatus(ctx context.Context, status models.TranslationStatus) ([]models.Translation, error) {
|
|
var out []models.Translation
|
|
for i := range m.items {
|
|
if m.items[i].Status == status {
|
|
out = append(out, m.items[i])
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Test helper: add a translation for a Work
|
|
func (m *MockTranslationRepository) AddTranslationForWork(workID uint, language string, content string, isOriginal bool) {
|
|
m.Create(context.Background(), &models.Translation{
|
|
Title: "",
|
|
Content: content,
|
|
Description: "",
|
|
Language: language,
|
|
Status: models.TranslationStatusPublished,
|
|
TranslatableID: workID,
|
|
TranslatableType: "Work",
|
|
IsOriginalLanguage: isOriginal,
|
|
})
|
|
}
|