photoprism/internal/query/labels_test.go
Michael Mayer 8ccaaff4e5 Backend: Update label photo count and refactor entity fixtures
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-10 19:43:49 +02:00

84 lines
1.9 KiB
Go

package query
import (
"testing"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/form"
"github.com/stretchr/testify/assert"
)
func TestLabels(t *testing.T) {
t.Run("search with query", func(t *testing.T) {
query := form.NewLabelSearch("Query:C Count:1005 Order:slug")
result, err := Labels(query)
if err != nil {
t.Fatal(err)
}
t.Logf("results: %+v", result)
assert.LessOrEqual(t, 2, len(result))
for _, r := range result {
assert.IsType(t, LabelResult{}, r)
assert.NotEmpty(t, r.ID)
assert.NotEmpty(t, r.LabelName)
assert.NotEmpty(t, r.LabelSlug)
assert.NotEmpty(t, r.CustomSlug)
if fix, ok := entity.LabelFixtures[r.LabelSlug]; ok {
assert.Equal(t, fix.LabelName, r.LabelName)
assert.Equal(t, fix.LabelSlug, r.LabelSlug)
assert.Equal(t, fix.CustomSlug, r.CustomSlug)
}
}
})
t.Run("search for favorites", func(t *testing.T) {
query := form.NewLabelSearch("Favorites:true")
result, err := Labels(query)
if err != nil {
t.Fatal(err)
}
assert.LessOrEqual(t, 2, len(result))
for _, r := range result {
assert.True(t, r.LabelFavorite)
assert.IsType(t, LabelResult{}, r)
assert.NotEmpty(t, r.ID)
assert.NotEmpty(t, r.LabelName)
assert.NotEmpty(t, r.LabelSlug)
assert.NotEmpty(t, r.CustomSlug)
if fix, ok := entity.LabelFixtures[r.LabelSlug]; ok {
assert.Equal(t, fix.LabelName, r.LabelName)
assert.Equal(t, fix.LabelSlug, r.LabelSlug)
assert.Equal(t, fix.CustomSlug, r.CustomSlug)
}
}
})
t.Run("search with empty query", func(t *testing.T) {
query := form.NewLabelSearch("")
result, err := Labels(query)
if err != nil {
t.Fatal(err)
}
assert.LessOrEqual(t, 3, len(result))
})
t.Run("search with invalid query string", func(t *testing.T) {
query := form.NewLabelSearch("xxx:bla")
result, err := Labels(query)
assert.Error(t, err, "unknown filter")
assert.Empty(t, result)
})
}