Labels: Refactor label entity and cache in label.go and label_cache.go

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-01-17 03:35:10 +01:00
parent f1bfa4a0ec
commit f24149fd49
5 changed files with 178 additions and 116 deletions

View File

@@ -0,0 +1,40 @@
package entity
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFindLabel(t *testing.T) {
t.Run("Success", func(t *testing.T) {
label := &Label{LabelSlug: "find-me-label", LabelName: "Find Me"}
if err := label.Save(); err != nil {
t.Fatal(err)
}
uncached, findErr := FindLabel("find-me-label", false)
assert.NoError(t, findErr)
assert.Equal(t, "Find Me", uncached.LabelName)
cached, cacheErr := FindLabel("find-me-label", true)
assert.NoError(t, cacheErr)
assert.Equal(t, "Find Me", cached.LabelName)
assert.Equal(t, uncached.LabelSlug, cached.LabelSlug)
assert.Equal(t, uncached.ID, cached.ID)
assert.Equal(t, uncached.LabelUID, cached.LabelUID)
})
t.Run("NotFound", func(t *testing.T) {
result, err := FindLabel("XXX", true)
assert.Error(t, err)
assert.NotNil(t, result)
})
t.Run("EmptyName", func(t *testing.T) {
result, err := FindLabel("", true)
assert.Error(t, err)
assert.NotNil(t, result)
})
}