2021-09-18 15:32:39 +02:00
|
|
|
package search
|
2021-08-29 16:16:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
|
2021-08-29 16:16:49 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLikeAny(t *testing.T) {
|
|
|
|
t.Run("and_or_search", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAny("k.keyword", "table spoon & usa | img json", true, false); len(w) != 2 {
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon%' OR k.keyword LIKE 'table%'", w[0])
|
2021-08-29 19:19:54 +02:00
|
|
|
assert.Equal(t, "k.keyword LIKE 'json%' OR k.keyword LIKE 'usa'", w[1])
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
})
|
2021-08-31 17:18:48 +02:00
|
|
|
t.Run(" exact and_or_search", func(t *testing.T) {
|
|
|
|
if w := LikeAny("k.keyword", "table spoon & usa | img json", true, true); len(w) != 2 {
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon' OR k.keyword LIKE 'table'", w[0])
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'json' OR k.keyword LIKE 'usa'", w[1])
|
|
|
|
}
|
|
|
|
})
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Run("and_or_search_en", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAny("k.keyword", "table spoon and usa or img json", true, false); len(w) != 2 {
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon%' OR k.keyword LIKE 'table%'", w[0])
|
2021-08-29 19:19:54 +02:00
|
|
|
assert.Equal(t, "k.keyword LIKE 'json%' OR k.keyword LIKE 'usa'", w[1])
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("table spoon usa img json", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAny("k.keyword", "table spoon usa img json", true, false); len(w) != 1 {
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Fatal("one where condition expected")
|
|
|
|
} else {
|
2021-08-29 19:19:54 +02:00
|
|
|
assert.Equal(t, "k.keyword LIKE 'json%' OR k.keyword LIKE 'spoon%' OR k.keyword LIKE 'table%' OR k.keyword LIKE 'usa'", w[0])
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cat dog", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAny("k.keyword", "cat dog", true, false); len(w) != 1 {
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Fatal("one where condition expected")
|
|
|
|
} else {
|
2021-08-29 19:19:54 +02:00
|
|
|
assert.Equal(t, "k.keyword LIKE 'cat' OR k.keyword LIKE 'dog'", w[0])
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cats dogs", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAny("k.keyword", "cats dogs", true, false); len(w) != 1 {
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Fatal("one where condition expected")
|
|
|
|
} else {
|
2021-08-29 19:19:54 +02:00
|
|
|
assert.Equal(t, "k.keyword LIKE 'cats%' OR k.keyword LIKE 'cat' OR k.keyword LIKE 'dogs%' OR k.keyword LIKE 'dog'", w[0])
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("spoon", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAny("k.keyword", "spoon", true, false); len(w) != 1 {
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Fatal("one where condition expected")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon%'", w[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("img", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAny("k.keyword", "img", true, false); len(w) > 0 {
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Fatal("no where condition expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAny("k.keyword", "", true, false); len(w) > 0 {
|
2021-08-29 16:16:49 +02:00
|
|
|
t.Fatal("no where condition expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-29 19:19:54 +02:00
|
|
|
func TestLikeAnyKeyword(t *testing.T) {
|
|
|
|
t.Run("and_or_search", func(t *testing.T) {
|
|
|
|
if w := LikeAnyKeyword("k.keyword", "table spoon & usa | img json"); len(w) != 2 {
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon%' OR k.keyword LIKE 'table%'", w[0])
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'json%' OR k.keyword LIKE 'usa'", w[1])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("and_or_search_en", func(t *testing.T) {
|
|
|
|
if w := LikeAnyKeyword("k.keyword", "table spoon and usa or img json"); len(w) != 2 {
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon%' OR k.keyword LIKE 'table%'", w[0])
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'json%' OR k.keyword LIKE 'usa'", w[1])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLikeAnyWord(t *testing.T) {
|
|
|
|
t.Run("and_or_search", func(t *testing.T) {
|
|
|
|
if w := LikeAnyWord("k.keyword", "table spoon & usa | img json"); len(w) != 2 {
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon%' OR k.keyword LIKE 'table%'", w[0])
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'img%' OR k.keyword LIKE 'json%' OR k.keyword LIKE 'usa%'", w[1])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("and_or_search_en", func(t *testing.T) {
|
|
|
|
if w := LikeAnyWord("k.keyword", "table spoon and usa or img json"); len(w) != 2 {
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'spoon%' OR k.keyword LIKE 'table%'", w[0])
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'img%' OR k.keyword LIKE 'json%' OR k.keyword LIKE 'usa%'", w[1])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:16:49 +02:00
|
|
|
func TestLikeAll(t *testing.T) {
|
|
|
|
t.Run("keywords", func(t *testing.T) {
|
2021-08-30 11:26:57 +02:00
|
|
|
if w := LikeAll("k.keyword", "Jo Mander 李", true, false); len(w) == 2 {
|
2021-08-29 19:19:54 +02:00
|
|
|
assert.Equal(t, "k.keyword LIKE 'mander%'", w[0])
|
|
|
|
assert.Equal(t, "k.keyword LIKE '李'", w[1])
|
|
|
|
} else {
|
|
|
|
t.Logf("wheres: %#v", w)
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
}
|
|
|
|
})
|
2021-08-30 11:26:57 +02:00
|
|
|
t.Run("exact", func(t *testing.T) {
|
|
|
|
if w := LikeAll("k.keyword", "Jo Mander 李", true, true); len(w) == 2 {
|
|
|
|
assert.Equal(t, "k.keyword LIKE 'mander'", w[0])
|
|
|
|
assert.Equal(t, "k.keyword LIKE '李'", w[1])
|
|
|
|
} else {
|
|
|
|
t.Logf("wheres: %#v", w)
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
}
|
|
|
|
})
|
2021-08-31 17:18:48 +02:00
|
|
|
t.Run("string empty", func(t *testing.T) {
|
|
|
|
w := LikeAll("k.keyword", "", true, true)
|
|
|
|
assert.Empty(t, w)
|
|
|
|
})
|
|
|
|
t.Run("0 words", func(t *testing.T) {
|
|
|
|
w := LikeAll("k.keyword", "ab", true, true)
|
|
|
|
assert.Empty(t, w)
|
|
|
|
})
|
2021-08-29 19:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLikeAllKeywords(t *testing.T) {
|
|
|
|
t.Run("keywords", func(t *testing.T) {
|
|
|
|
if w := LikeAllKeywords("k.keyword", "Jo Mander 李"); len(w) == 2 {
|
2021-08-29 16:16:49 +02:00
|
|
|
assert.Equal(t, "k.keyword LIKE 'mander%'", w[0])
|
2021-08-29 19:19:54 +02:00
|
|
|
assert.Equal(t, "k.keyword LIKE '李'", w[1])
|
2021-08-29 16:16:49 +02:00
|
|
|
} else {
|
|
|
|
t.Logf("wheres: %#v", w)
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-29 19:19:54 +02:00
|
|
|
func TestLikeAllWords(t *testing.T) {
|
|
|
|
t.Run("keywords", func(t *testing.T) {
|
|
|
|
if w := LikeAllWords("k.name", "Jo Mander 王"); len(w) == 3 {
|
|
|
|
assert.Equal(t, "k.name LIKE 'jo%'", w[0])
|
|
|
|
assert.Equal(t, "k.name LIKE 'mander%'", w[1])
|
|
|
|
assert.Equal(t, "k.name LIKE '王%'", w[2])
|
|
|
|
} else {
|
|
|
|
t.Logf("wheres: %#v", w)
|
|
|
|
t.Fatal("two where conditions expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-08-30 11:26:57 +02:00
|
|
|
|
|
|
|
func TestLikeAllNames(t *testing.T) {
|
2021-09-01 12:48:17 +02:00
|
|
|
t.Run("MultipleNames", func(t *testing.T) {
|
2021-09-06 14:16:46 +02:00
|
|
|
if w := LikeAllNames(Cols{"k.name"}, "j Mander 王"); len(w) == 1 {
|
|
|
|
assert.Equal(t, "k.name LIKE 'j Mander 王%'", w[0])
|
2021-09-01 12:48:17 +02:00
|
|
|
} else {
|
2021-09-06 14:16:46 +02:00
|
|
|
t.Fatalf("one where conditions expected: %#v", w)
|
2021-09-01 12:48:17 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("MultipleColumns", func(t *testing.T) {
|
2021-09-06 14:16:46 +02:00
|
|
|
if w := LikeAllNames(Cols{"a.col1", "b.col2"}, "Mo Mander"); len(w) == 1 {
|
|
|
|
assert.Equal(t, "a.col1 LIKE 'Mo Mander%' OR b.col2 LIKE 'Mo Mander%'", w[0])
|
2021-08-30 11:26:57 +02:00
|
|
|
} else {
|
2021-09-06 14:16:46 +02:00
|
|
|
t.Fatalf("one where conditions expected: %#v", w)
|
2021-08-30 11:26:57 +02:00
|
|
|
}
|
|
|
|
})
|
2021-09-01 12:48:17 +02:00
|
|
|
t.Run("EmptyName", func(t *testing.T) {
|
|
|
|
w := LikeAllNames(Cols{"k.name"}, "")
|
2021-08-31 17:18:48 +02:00
|
|
|
assert.Empty(t, w)
|
|
|
|
})
|
2021-09-01 12:48:17 +02:00
|
|
|
t.Run("NoWords", func(t *testing.T) {
|
|
|
|
w := LikeAllNames(Cols{"k.name"}, "a")
|
2021-08-31 17:18:48 +02:00
|
|
|
assert.Empty(t, w)
|
|
|
|
})
|
2021-09-06 14:16:46 +02:00
|
|
|
t.Run("FullNames", func(t *testing.T) {
|
|
|
|
if w := LikeAllNames(Cols{"j.name", "j.alias"}, "Bill & Melinda Gates"); len(w) == 2 {
|
|
|
|
assert.Equal(t, "j.name LIKE 'Bill' OR j.name LIKE 'Bill %' OR j.alias LIKE 'Bill' OR j.alias LIKE 'Bill %'", w[0])
|
|
|
|
assert.Equal(t, "j.name LIKE 'Melinda Gates%' OR j.alias LIKE 'Melinda Gates%'", w[1])
|
|
|
|
} else {
|
|
|
|
t.Fatalf("two where conditions expected: %#v", w)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Plus", func(t *testing.T) {
|
2021-09-18 15:32:39 +02:00
|
|
|
if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("Paul + Paula")); len(w) == 2 {
|
2021-09-06 14:16:46 +02:00
|
|
|
assert.Equal(t, "name LIKE 'paul' OR name LIKE 'paul %'", w[0])
|
|
|
|
assert.Equal(t, "name LIKE '%paula%'", w[1])
|
|
|
|
} else {
|
|
|
|
t.Fatalf("two where conditions expected: %#v", w)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Ane", func(t *testing.T) {
|
2021-09-18 15:32:39 +02:00
|
|
|
if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("Paul and Paula")); len(w) == 2 {
|
2021-09-06 14:16:46 +02:00
|
|
|
assert.Equal(t, "name LIKE 'paul' OR name LIKE 'paul %'", w[0])
|
|
|
|
assert.Equal(t, "name LIKE '%paula%'", w[1])
|
|
|
|
} else {
|
|
|
|
t.Fatalf("two where conditions expected: %#v", w)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Or", func(t *testing.T) {
|
2021-09-18 15:32:39 +02:00
|
|
|
if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("Paul or Paula")); len(w) == 1 {
|
2021-09-06 14:16:46 +02:00
|
|
|
assert.Equal(t, "name LIKE 'paul' OR name LIKE 'paul %' OR name LIKE '%paula%'", w[0])
|
|
|
|
} else {
|
|
|
|
t.Fatalf("one where conditions expected: %#v", w)
|
|
|
|
}
|
|
|
|
})
|
2021-08-30 11:26:57 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 16:16:49 +02:00
|
|
|
func TestAnySlug(t *testing.T) {
|
|
|
|
t.Run("table spoon usa img json", func(t *testing.T) {
|
|
|
|
where := AnySlug("custom_slug", "table spoon usa img json", " ")
|
|
|
|
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) {
|
|
|
|
where := AnySlug("custom_slug", "cat dog", " ")
|
|
|
|
assert.Equal(t, "custom_slug = 'cat' OR custom_slug = 'dog'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cats dogs", func(t *testing.T) {
|
|
|
|
where := AnySlug("custom_slug", "cats dogs", " ")
|
|
|
|
assert.Equal(t, "custom_slug = 'cats' OR custom_slug = 'cat' OR custom_slug = 'dogs' OR custom_slug = 'dog'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("spoon", func(t *testing.T) {
|
|
|
|
where := AnySlug("custom_slug", "spoon", " ")
|
|
|
|
assert.Equal(t, "custom_slug = 'spoon'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("img", func(t *testing.T) {
|
|
|
|
where := AnySlug("custom_slug", "img", " ")
|
|
|
|
assert.Equal(t, "custom_slug = 'img'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
where := AnySlug("custom_slug", "", " ")
|
|
|
|
assert.Equal(t, "", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("comma separated", func(t *testing.T) {
|
2021-09-18 15:32:39 +02:00
|
|
|
where := AnySlug("custom_slug", "botanical-garden|landscape|bay", txt.Or)
|
2021-08-29 16:16:49 +02:00
|
|
|
assert.Equal(t, "custom_slug = 'botanical-garden' OR custom_slug = 'landscape' OR custom_slug = 'bay'", where)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("len = 0", func(t *testing.T) {
|
|
|
|
where := AnySlug("custom_slug", " ", "")
|
|
|
|
assert.Equal(t, "custom_slug = '' OR custom_slug = ''", where)
|
|
|
|
})
|
|
|
|
}
|