feat: characterized the existing use cases as a baseline; every repo fake behaves like the real ones and can be told to fail, every use case is covered; gremlins runs clean on useCases
This commit is contained in:
1 parent
09bd5afa4a
commit
7c57173497
12 files changed
+1245
-92
No files matched your search
+44
-13
@@ -1,40 +1,71 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/image"
|
||||
imageDomain "RenewCMS/internal/domain/image"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type mockImageRepo struct {
|
||||
images map[uint32]image.Image
|
||||
var errUnsupportedImageContentType = errors.New("the file must be a PNG, JPEG, WEBP, or SVG image")
|
||||
|
||||
var supportedImageContentTypes = map[string]bool{
|
||||
"image/png": true,
|
||||
"image/jpeg": true,
|
||||
"image/webp": true,
|
||||
"image/svg+xml": true,
|
||||
}
|
||||
|
||||
func (m *mockImageRepo) Create(input image.ImageInput) (image.Image, error) {
|
||||
newId := uint32(len(m.images) + 1)
|
||||
img := image.Image{
|
||||
ID: newId,
|
||||
Path: "/static/" + input.Filename,
|
||||
type mockImageRepo struct {
|
||||
images map[uint32]imageDomain.Image
|
||||
nextID uint32
|
||||
fail failSwitch
|
||||
}
|
||||
|
||||
func newMockImageRepo() *mockImageRepo {
|
||||
return &mockImageRepo{images: make(map[uint32]imageDomain.Image)}
|
||||
}
|
||||
|
||||
func (m *mockImageRepo) Create(input imageDomain.ImageInput) (imageDomain.Image, error) {
|
||||
if err := m.fail.trigger("Create"); err != nil {
|
||||
return imageDomain.Image{}, err
|
||||
}
|
||||
m.images[newId] = img
|
||||
if !supportedImageContentTypes[input.ContentType] {
|
||||
return imageDomain.Image{}, errUnsupportedImageContentType
|
||||
}
|
||||
if input.Content != nil {
|
||||
_, _ = io.ReadAll(input.Content)
|
||||
}
|
||||
m.nextID++
|
||||
img := imageDomain.Image{ID: m.nextID, Path: "/static/uploadedImages/" + input.Filename}
|
||||
m.images[img.ID] = img
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func (m *mockImageRepo) Delete(id uint32) error {
|
||||
if err := m.fail.trigger("Delete"); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, ok := m.images[id]; !ok {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
delete(m.images, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ image.Repository = &mockImageRepo{}
|
||||
var _ imageDomain.Repository = &mockImageRepo{}
|
||||
|
||||
func TestImageUseCases(t *testing.T) {
|
||||
repo := &mockImageRepo{images: make(map[uint32]image.Image)}
|
||||
repo := newMockImageRepo()
|
||||
createUc := useCases.NewCreateImageUseCase(repo)
|
||||
deleteUc := useCases.NewDeleteImageUseCase(repo)
|
||||
|
||||
t.Run("Create Image", func(t *testing.T) {
|
||||
input := image.ImageInput{
|
||||
input := imageDomain.ImageInput{
|
||||
Content: strings.NewReader("fake-image-data"),
|
||||
Filename: "test.png",
|
||||
ContentType: "image/png",
|
||||
@@ -53,7 +84,7 @@ func TestImageUseCases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Delete Image", func(t *testing.T) {
|
||||
repo.images[99] = image.Image{ID: 99, Path: "/static/delete-me.png"}
|
||||
repo.images[99] = imageDomain.Image{ID: 99, Path: "/static/delete-me.png"}
|
||||
|
||||
err := deleteUc.DeleteImage(99)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user