2020-01-05 14:18:40 +01:00
|
|
|
package query
|
2019-12-11 08:18:48 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-11 08:18:48 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-11 14:49:00 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-11 08:18:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
log = logrus.StandardLogger()
|
|
|
|
log.SetLevel(logrus.DebugLevel)
|
2020-04-30 22:39:38 +02:00
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
db := entity.InitTestDb(os.Getenv("PHOTOPRISM_TEST_DRIVER"), os.Getenv("PHOTOPRISM_TEST_DSN"))
|
|
|
|
defer db.Close()
|
2020-04-30 22:39:38 +02:00
|
|
|
|
2019-12-11 08:18:48 +01:00
|
|
|
code := m.Run()
|
2020-04-30 22:39:38 +02:00
|
|
|
|
2019-12-11 08:18:48 +01:00
|
|
|
os.Exit(code)
|
|
|
|
}
|
2020-05-11 14:49:00 +02:00
|
|
|
|
|
|
|
func TestLikeAny(t *testing.T) {
|
|
|
|
t.Run("table spoon usa img json", func(t *testing.T) {
|
|
|
|
where := LikeAny("k.keyword", "table spoon usa img json")
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'json%' OR k.keyword LIKE 'spoon%' OR k.keyword LIKE 'table%' OR k.keyword = 'usa'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cat dog", func(t *testing.T) {
|
|
|
|
where := LikeAny("k.keyword", "cat dog")
|
|
|
|
assert.Equal(t, "k.keyword = 'cat' OR k.keyword = 'dog'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("spoon", func(t *testing.T) {
|
|
|
|
where := LikeAny("k.keyword", "spoon")
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon%'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("img", func(t *testing.T) {
|
|
|
|
where := LikeAny("k.keyword", "img")
|
|
|
|
assert.Equal(t, "", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
where := LikeAny("k.keyword", "")
|
|
|
|
assert.Equal(t, "", where)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnySlug(t *testing.T) {
|
|
|
|
t.Run("table spoon usa img json", func(t *testing.T) {
|
2020-05-29 18:04:30 +02:00
|
|
|
where := AnySlug("custom_slug", "table spoon usa img json", " ")
|
2020-05-11 14:49:00 +02:00
|
|
|
assert.Equal(t, "custom_slug = 'table' OR custom_slug = 'spoon' OR custom_slug = 'usa' OR custom_slug = 'img' OR custom_slug = 'json'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cat dog", func(t *testing.T) {
|
2020-05-29 18:04:30 +02:00
|
|
|
where := AnySlug("custom_slug", "cat dog", " ")
|
2020-05-11 14:49:00 +02:00
|
|
|
assert.Equal(t, "custom_slug = 'cat' OR custom_slug = 'dog'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("spoon", func(t *testing.T) {
|
2020-05-29 18:04:30 +02:00
|
|
|
where := AnySlug("custom_slug", "spoon", " ")
|
2020-05-11 14:49:00 +02:00
|
|
|
assert.Equal(t, "custom_slug = 'spoon'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("img", func(t *testing.T) {
|
2020-05-29 18:04:30 +02:00
|
|
|
where := AnySlug("custom_slug", "img", " ")
|
2020-05-11 14:49:00 +02:00
|
|
|
assert.Equal(t, "custom_slug = 'img'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
2020-05-29 18:04:30 +02:00
|
|
|
where := AnySlug("custom_slug", "", " ")
|
2020-05-11 14:49:00 +02:00
|
|
|
assert.Equal(t, "", where)
|
|
|
|
})
|
2020-05-29 18:04:30 +02:00
|
|
|
|
|
|
|
t.Run("comma separated", func(t *testing.T) {
|
|
|
|
where := AnySlug("custom_slug", "botanical-garden,landscape,bay", ",")
|
|
|
|
assert.Equal(t, "custom_slug = 'botanical-garden' OR custom_slug = 'landscape' OR custom_slug = 'bay'", where)
|
|
|
|
})
|
2020-05-11 14:49:00 +02:00
|
|
|
}
|