2021-09-18 15:32:39 +02:00
|
|
|
package search
|
2020-01-28 20:59:02 +01:00
|
|
|
|
|
|
|
import (
|
2020-11-21 18:08:41 +01:00
|
|
|
"testing"
|
|
|
|
|
2020-04-30 14:32:48 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-01-28 20:59:02 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
func TestLabels(t *testing.T) {
|
2020-01-28 20:59:02 +01:00
|
|
|
t.Run("search with query", func(t *testing.T) {
|
|
|
|
query := form.NewLabelSearch("Query:C Count:1005 Order:slug")
|
2020-05-08 15:41:01 +02:00
|
|
|
result, err := Labels(query)
|
2020-01-28 20:59:02 +01:00
|
|
|
|
2020-04-30 14:32:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("results: %+v", result)
|
|
|
|
|
|
|
|
assert.LessOrEqual(t, 2, len(result))
|
|
|
|
|
|
|
|
for _, r := range result {
|
2021-09-18 15:32:39 +02:00
|
|
|
assert.IsType(t, Label{}, r)
|
2020-04-30 14:32:48 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 20:59:02 +01:00
|
|
|
})
|
|
|
|
|
2020-05-14 15:05:10 +02:00
|
|
|
t.Run("search for cow", func(t *testing.T) {
|
|
|
|
query := form.NewLabelSearch("Query:cow Count:1005 Order:slug")
|
|
|
|
result, err := Labels(query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("results: %+v", result)
|
|
|
|
|
|
|
|
assert.LessOrEqual(t, 1, len(result))
|
|
|
|
|
|
|
|
for _, r := range result {
|
2021-09-18 15:32:39 +02:00
|
|
|
assert.IsType(t, Label{}, r)
|
2020-05-14 15:05:10 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-01-28 20:59:02 +01:00
|
|
|
t.Run("search for favorites", func(t *testing.T) {
|
2020-05-14 19:03:12 +02:00
|
|
|
query := form.NewLabelSearch("Favorite:true Count:15")
|
2020-05-08 15:41:01 +02:00
|
|
|
result, err := Labels(query)
|
2020-01-28 20:59:02 +01:00
|
|
|
|
2020-04-30 14:32:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.LessOrEqual(t, 2, len(result))
|
|
|
|
|
|
|
|
for _, r := range result {
|
|
|
|
assert.True(t, r.LabelFavorite)
|
2021-09-18 15:32:39 +02:00
|
|
|
assert.IsType(t, Label{}, r)
|
2020-04-30 14:32:48 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 20:59:02 +01:00
|
|
|
})
|
2020-02-02 13:02:40 +01:00
|
|
|
|
|
|
|
t.Run("search with empty query", func(t *testing.T) {
|
|
|
|
query := form.NewLabelSearch("")
|
2020-05-08 15:41:01 +02:00
|
|
|
result, err := Labels(query)
|
2020-04-30 14:32:48 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-13 11:57:54 +02:00
|
|
|
t.Log(result)
|
2020-04-30 14:32:48 +02:00
|
|
|
assert.LessOrEqual(t, 3, len(result))
|
2020-02-02 13:02:40 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("search with invalid query string", func(t *testing.T) {
|
|
|
|
query := form.NewLabelSearch("xxx:bla")
|
2020-05-08 15:41:01 +02:00
|
|
|
result, err := Labels(query)
|
2020-04-30 14:32:48 +02:00
|
|
|
|
2020-02-02 13:02:40 +01:00
|
|
|
assert.Error(t, err, "unknown filter")
|
2020-04-30 14:32:48 +02:00
|
|
|
assert.Empty(t, result)
|
2020-02-02 13:02:40 +01:00
|
|
|
})
|
2020-05-13 11:57:54 +02:00
|
|
|
|
|
|
|
t.Run("search for ID", func(t *testing.T) {
|
|
|
|
f := form.LabelSearch{
|
2020-05-14 19:03:12 +02:00
|
|
|
Query: "",
|
|
|
|
ID: "lt9k3pw1wowuy3c4",
|
|
|
|
Slug: "",
|
|
|
|
Name: "",
|
|
|
|
All: false,
|
|
|
|
Favorite: false,
|
|
|
|
Count: 0,
|
|
|
|
Offset: 0,
|
|
|
|
Order: "",
|
2020-05-13 11:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result, err := Labels(f)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "cake", result[0].LabelSlug)
|
|
|
|
})
|
2020-07-13 12:29:11 +02:00
|
|
|
|
|
|
|
t.Run("search for label landscape", func(t *testing.T) {
|
|
|
|
f := form.LabelSearch{
|
|
|
|
Query: "landscape",
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := Labels(f)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "flower", result[0].LabelSlug)
|
|
|
|
})
|
2020-01-28 20:59:02 +01:00
|
|
|
}
|