@@ -267,6 +267,93 @@ func TestMemoryPostValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemoryPostImages(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
mem := NewMemory()
|
||||
homeowner := &User{Username: "images", PasswordHash: "hash", Role: RoleUser}
|
||||
if err := mem.CreateUser(ctx, homeowner); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := &Post{
|
||||
ID: "image-root",
|
||||
AuthorID: homeowner.ID,
|
||||
Title: "What is leaking?",
|
||||
Body: "Here are two photos.",
|
||||
Images: []PostImage{
|
||||
{ID: "image-a", ObjectKey: "posts/image-root/image-a.jpg", PublicURL: "https://cdn.example/image-a.jpg", Description: " Supply valve ", Position: 3, Width: 1200, Height: 900},
|
||||
{ID: "image-b", ObjectKey: "posts/image-root/image-b.png", PublicURL: "https://cdn.example/image-b.png", Width: 900, Height: 1200},
|
||||
},
|
||||
}
|
||||
if err := mem.CreatePost(ctx, root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if root.Images[0].Position != 0 ||
|
||||
root.Images[1].Position != 1 ||
|
||||
root.Images[0].PostID != root.ID ||
|
||||
root.Images[0].Description != "Supply valve" {
|
||||
t.Fatalf("created images were not normalized: %+v", root.Images)
|
||||
}
|
||||
|
||||
loaded, err := mem.GetPost(ctx, root.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded.Images[0].Description = "mutated outside store"
|
||||
reloaded, err := mem.GetPost(ctx, root.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reloaded.Images[0].Description != "Supply valve" {
|
||||
t.Fatalf("stored image mutated through clone: %+v", reloaded.Images[0])
|
||||
}
|
||||
|
||||
reloaded.Body = "Updated photos."
|
||||
reloaded.Images = []PostImage{
|
||||
reloaded.Images[1],
|
||||
{ID: "image-c", ObjectKey: "posts/image-root/image-c.jpg", PublicURL: "https://cdn.example/image-c.jpg", Description: "Trap connection", Width: 1600, Height: 1000},
|
||||
}
|
||||
if err := mem.UpdatePost(ctx, reloaded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
updated, err := mem.GetPost(ctx, root.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(updated.Images) != 2 ||
|
||||
updated.Images[0].ID != "image-b" ||
|
||||
updated.Images[0].Position != 0 ||
|
||||
updated.Images[1].ID != "image-c" ||
|
||||
updated.Images[1].Position != 1 {
|
||||
t.Fatalf("updated images = %+v", updated.Images)
|
||||
}
|
||||
|
||||
tooMany := &Post{
|
||||
AuthorID: homeowner.ID,
|
||||
Title: "Too many",
|
||||
Body: "Five photos.",
|
||||
Images: validPostImages(5),
|
||||
}
|
||||
if err := mem.CreatePost(ctx, tooMany); !errors.Is(err, ErrInvalidPost) {
|
||||
t.Fatalf("five-image create error = %v, want ErrInvalidPost", err)
|
||||
}
|
||||
}
|
||||
|
||||
func validPostImages(count int) []PostImage {
|
||||
images := make([]PostImage, count)
|
||||
for i := range images {
|
||||
images[i] = PostImage{
|
||||
ID: "image-" + string(rune('a'+i)),
|
||||
ObjectKey: "posts/key-" + string(rune('a'+i)) + ".jpg",
|
||||
PublicURL: "https://cdn.example/" + string(rune('a'+i)) + ".jpg",
|
||||
Width: 100,
|
||||
Height: 100,
|
||||
}
|
||||
}
|
||||
return images
|
||||
}
|
||||
|
||||
func ptr(value string) *string {
|
||||
return &value
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user