tercul-backend/internal/data/cache/cached_author_repository_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

232 lines
6.8 KiB
Go

package cache
import (
"context"
"encoding/json"
"errors"
"sync"
"testing"
"time"
"tercul/internal/domain"
"gorm.io/gorm"
"github.com/stretchr/testify/require"
)
// memCache (duplicated) - tiny in-memory Cache impl for tests
type memCacheA struct {
m map[string][]byte
mu sync.RWMutex
}
func newMemCacheA() *memCacheA { return &memCacheA{m: map[string][]byte{}} }
func (c *memCacheA) Get(_ context.Context, key string, value interface{}) error {
c.mu.RLock()
defer c.mu.RUnlock()
b, ok := c.m[key]
if !ok {
return errors.New("cache miss")
}
return json.Unmarshal(b, value)
}
func (c *memCacheA) Set(_ context.Context, key string, value interface{}, expiration time.Duration) error {
b, err := json.Marshal(value)
if err != nil {
return err
}
c.mu.Lock()
defer c.mu.Unlock()
c.m[key] = b
return nil
}
func (c *memCacheA) Delete(_ context.Context, key string) error {
c.mu.Lock()
delete(c.m, key)
c.mu.Unlock()
return nil
}
func (c *memCacheA) Clear(_ context.Context) error {
c.mu.Lock()
c.m = map[string][]byte{}
c.mu.Unlock()
return nil
}
func (c *memCacheA) GetMulti(_ context.Context, keys []string) (map[string][]byte, error) {
res := map[string][]byte{}
c.mu.RLock()
defer c.mu.RUnlock()
for _, k := range keys {
if v, ok := c.m[k]; ok {
res[k] = v
}
}
return res, nil
}
func (c *memCacheA) SetMulti(_ context.Context, items map[string]interface{}, expiration time.Duration) error {
for k, v := range items {
b, _ := json.Marshal(v)
c.m[k] = b
}
return nil
}
// dummyAuthorRepo implements domain.AuthorRepository minimal methods for tests
type dummyAuthorRepo struct {
store map[uint]*domain.Author
calls int
mu sync.Mutex
}
func newDummyAuthorRepo() *dummyAuthorRepo { return &dummyAuthorRepo{store: map[uint]*domain.Author{}} }
func (d *dummyAuthorRepo) Create(_ context.Context, entity *domain.Author) error {
d.mu.Lock()
defer d.mu.Unlock()
d.calls++
d.store[entity.ID] = entity
return nil
}
func (d *dummyAuthorRepo) CreateInTx(_ context.Context, tx *gorm.DB, entity *domain.Author) error {
return errors.New("not implemented")
}
func (d *dummyAuthorRepo) GetByID(_ context.Context, id uuid.UUID) (*domain.Author, error) {
d.mu.Lock()
defer d.mu.Unlock()
d.calls++
a, ok := d.store[id]
if !ok {
return nil, errors.New("not found")
}
cp := *a
return &cp, nil
}
func (d *dummyAuthorRepo) GetByIDWithOptions(_ context.Context, id uint, options *domain.QueryOptions) (*domain.Author, error) {
return d.GetByID(context.Background(), id)
}
func (d *dummyAuthorRepo) Update(_ context.Context, entity *domain.Author) error {
d.mu.Lock()
defer d.mu.Unlock()
d.calls++
if _, ok := d.store[entity.ID]; !ok {
return errors.New("not found")
}
d.store[entity.ID] = entity
return nil
}
func (d *dummyAuthorRepo) UpdateInTx(_ context.Context, tx *gorm.DB, entity *domain.Author) error {
return errors.New("not implemented")
}
func (d *dummyAuthorRepo) Delete(_ context.Context, id uint) error {
d.mu.Lock()
defer d.mu.Unlock()
d.calls++
if _, ok := d.store[id]; !ok {
return errors.New("not found")
}
delete(d.store, id)
return nil
}
func (d *dummyAuthorRepo) DeleteInTx(_ context.Context, tx *gorm.DB, id uint) error {
return errors.New("not implemented")
}
func (d *dummyAuthorRepo) List(_ context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Author], error) {
return &domain.PaginatedResult[domain.Author]{Items: []domain.Author{}}, nil
}
func (d *dummyAuthorRepo) ListWithOptions(_ context.Context, options *domain.QueryOptions) ([]domain.Author, error) {
return nil, nil
}
func (d *dummyAuthorRepo) ListAll(_ context.Context) ([]domain.Author, error) { return nil, nil }
func (d *dummyAuthorRepo) Count(_ context.Context) (int64, error) { return 0, nil }
func (d *dummyAuthorRepo) CountWithOptions(_ context.Context, options *domain.QueryOptions) (int64, error) {
return 0, nil
}
func (d *dummyAuthorRepo) FindWithPreload(_ context.Context, preloads []string, id uint) (*domain.Author, error) {
return d.GetByID(context.Background(), id)
}
func (d *dummyAuthorRepo) GetAllForSync(_ context.Context, batchSize, offset int) ([]domain.Author, error) {
return nil, nil
}
func (d *dummyAuthorRepo) Exists(_ context.Context, id uint) (bool, error) {
_, ok := d.store[id]
return ok, nil
}
func (d *dummyAuthorRepo) BeginTx(_ context.Context) (*gorm.DB, error) { return nil, nil }
func (d *dummyAuthorRepo) WithTx(_ context.Context, fn func(tx *gorm.DB) error) error { return fn(nil) }
func (d *dummyAuthorRepo) FindByName(_ context.Context, name string) (*domain.Author, error) {
return nil, nil
}
func (d *dummyAuthorRepo) ListByWorkID(_ context.Context, workID uint) ([]domain.Author, error) {
return nil, nil
}
func (d *dummyAuthorRepo) ListByBookID(_ context.Context, bookID uint) ([]domain.Author, error) {
return nil, nil
}
func (d *dummyAuthorRepo) ListByCountryID(_ context.Context, countryID uint) ([]domain.Author, error) {
return nil, nil
}
func (d *dummyAuthorRepo) GetWithTranslations(_ context.Context, id uint) (*domain.Author, error) {
return d.GetByID(context.Background(), id)
}
func TestCachedAuthor_GetByID_Caches(t *testing.T) {
ctx := context.Background()
d := newDummyAuthorRepo()
d.store[1] = &domain.Author{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}}, Name: "Tolstoy"}
mc := newMemCacheA()
ca := NewCachedAuthorRepository(d, mc, nil)
a, err := ca.GetByID(ctx, 1)
require.NoError(t, err)
require.Equal(t, "Tolstoy", a.Name)
require.Equal(t, 1, d.calls)
// second read should hit cache
a2, err := ca.GetByID(ctx, 1)
require.NoError(t, err)
require.Equal(t, "Tolstoy", a2.Name)
require.Equal(t, 1, d.calls)
}
func TestCachedAuthor_Update_Invalidates(t *testing.T) {
ctx := context.Background()
d := newDummyAuthorRepo()
d.store[1] = &domain.Author{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}}, Name: "Tolstoy"}
mc := newMemCacheA()
ca := NewCachedAuthorRepository(d, mc, nil)
_, err := ca.GetByID(ctx, 1)
require.NoError(t, err)
require.Equal(t, 1, d.calls)
// update underlying
d.store[1] = &domain.Author{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}}, Name: "Leo"}
err = ca.Update(ctx, d.store[1])
require.NoError(t, err)
// next get should fetch again
a, err := ca.GetByID(ctx, 1)
require.NoError(t, err)
require.Equal(t, "Leo", a.Name)
require.Equal(t, 3, d.calls)
}
func TestCachedAuthor_Delete_Invalidates(t *testing.T) {
ctx := context.Background()
d := newDummyAuthorRepo()
d.store[1] = &domain.Author{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}}, Name: "Tolstoy"}
mc := newMemCacheA()
ca := NewCachedAuthorRepository(d, mc, nil)
_, err := ca.GetByID(ctx, 1)
require.NoError(t, err)
err = ca.Delete(ctx, 1)
require.NoError(t, err)
_, err = ca.GetByID(ctx, 1)
require.Error(t, err)
}