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

79 lines
2.3 KiB
Go

package cache
import (
"context"
"encoding/json"
"fmt"
"os"
"testing"
"time"
"tercul/internal/domain"
platform_cache "tercul/internal/platform/cache"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestRedisIntegration_CachedWorkRepository(t *testing.T) {
if os.Getenv("INTEGRATION_TESTS") != "true" {
t.Skip("skipping integration test; set INTEGRATION_TESTS=true to run")
}
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "redis:7.0",
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForListeningPort("6379/tcp").WithStartupTimeout(30 * time.Second),
}
redisC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ContainerRequest: req, Started: true})
require.NoError(t, err)
defer redisC.Terminate(ctx)
host, err := redisC.Host(ctx)
require.NoError(t, err)
port, err := redisC.MappedPort(ctx, "6379")
require.NoError(t, err)
addr := fmt.Sprintf("%s:%s", host, port.Port())
rcClient := redis.NewClient(&redis.Options{Addr: addr})
defer rcClient.Close()
require.NoError(t, rcClient.Ping(ctx).Err())
// create platform redis cache using key prefix that caches expect
keyGen := platform_cache.NewDefaultKeyGenerator("tercul:repo:")
rc := platform_cache.NewRedisCache(rcClient, keyGen, 0)
// create dummy repo and wrap
d := newDummyWorkRepo()
d.store[1] = &domain.Work{Title: "intwork", TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}, Language: "en"}}
cw := NewCachedWorkRepository(d, rc, nil)
// call and assert Redis stored entity
w, err := cw.GetByID(ctx, 1)
require.NoError(t, err)
require.Equal(t, "intwork", w.Title)
key := keyGen.EntityKey("work", 1)
val, err := rcClient.Get(ctx, key).Bytes()
require.NoError(t, err)
var stored domain.Work
require.NoError(t, json.Unmarshal(val, &stored))
require.Equal(t, "intwork", stored.Title)
// list caching test
d.store[2] = &domain.Work{Title: "intwork2", TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 2}, Language: "en"}}
// call list
_, err = cw.List(ctx, 1, 10)
require.NoError(t, err)
listKey := keyGen.ListKey("work", 1, 10)
_, err = rcClient.Get(ctx, listKey).Bytes()
require.NoError(t, err)
}