88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
|
|
package cache
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"img_infinite_canvas/internal/domain/design"
|
||
|
|
"img_infinite_canvas/internal/infrastructure/memory"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestProjectRepositoryCachesProjectReads(t *testing.T) {
|
||
|
|
ctx := context.Background()
|
||
|
|
base := memory.NewProjectRepository()
|
||
|
|
store := NewMemoryStore()
|
||
|
|
repo := NewProjectRepository(base, store, time.Minute)
|
||
|
|
|
||
|
|
project := design.Project{
|
||
|
|
ID: "project-1",
|
||
|
|
Title: "Original",
|
||
|
|
Brief: "brief",
|
||
|
|
Status: design.StatusReady,
|
||
|
|
UpdatedAt: time.Now(),
|
||
|
|
}
|
||
|
|
if err := base.Save(ctx, project); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
first, err := repo.Get(ctx, project.ID)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
project.Title = "Changed behind cache"
|
||
|
|
if err := base.Save(ctx, project); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
second, err := repo.Get(ctx, project.ID)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if second.Title != first.Title {
|
||
|
|
t.Fatalf("expected cached title %q, got %q", first.Title, second.Title)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestProjectRepositoryInvalidatesListOnSave(t *testing.T) {
|
||
|
|
ctx := context.Background()
|
||
|
|
base := memory.NewProjectRepository()
|
||
|
|
repo := NewProjectRepository(base, NewMemoryStore(), time.Minute)
|
||
|
|
|
||
|
|
if err := repo.Save(ctx, design.Project{
|
||
|
|
ID: "project-1",
|
||
|
|
Title: "First",
|
||
|
|
Brief: "brief",
|
||
|
|
Status: design.StatusReady,
|
||
|
|
UpdatedAt: time.Now(),
|
||
|
|
}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
first, err := repo.List(ctx)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(first) != 1 {
|
||
|
|
t.Fatalf("expected first list length 1, got %d", len(first))
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := repo.Save(ctx, design.Project{
|
||
|
|
ID: "project-2",
|
||
|
|
Title: "Second",
|
||
|
|
Brief: "brief",
|
||
|
|
Status: design.StatusReady,
|
||
|
|
UpdatedAt: time.Now(),
|
||
|
|
}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
second, err := repo.List(ctx)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(second) != 2 {
|
||
|
|
t.Fatalf("expected invalidated list length 2, got %d", len(second))
|
||
|
|
}
|
||
|
|
}
|