Files
opencloud/services/search/pkg/query/normalize_test.go
T
Dominik Schmidt 00f85c5496 refactor(search): resolve KQL keys next to the field type table
The field facts lived in two packages: search had the field type table,
query had the key resolution and a second copy of the field types. Key
resolution and the geopoint check move to search, next to FieldType, and
query reads them from there. query stays the lowering pass for the
engines; search can now check a parsed query against the fields without
an import cycle.
2026-12-04 11:01:26 +01:00

184 lines
6.6 KiB
Go

package query_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/opencloud-eu/opencloud/pkg/ast"
"github.com/opencloud-eu/opencloud/services/search/pkg/query"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
// This is the single place the shared KQL lowering pass is tested (field
// resolution, media-type expansion, group-key defaulting, pointer conversion).
// The backend query compilers consume its canonical output and must not re-test
// it.
func norm(nodes ...ast.Node) []ast.Node {
return query.Normalize(&ast.Ast{Nodes: nodes}, search.ResolveField).Nodes
}
var _ = Describe("FieldIsCaseInsensitive", func() {
It("reports the CaseInsensitive override fields", func() {
// keyword fields are case-insensitive by default, facets included
for _, f := range []string{"Name", "Title", "Tags", "audio.artist", "photo.cameraMake"} {
Expect(query.FieldIsCaseInsensitive(f)).To(BeTrue(), f)
}
// opted out (ids, favorites, path, mime type) or not a keyword at all
for _, f := range []string{"MimeType", "ID", "RootID", "ParentID", "Favorites", "Content", "Path", "Size", "unknown"} {
Expect(query.FieldIsCaseInsensitive(f)).To(BeFalse(), f)
}
})
})
var _ = Describe("FieldIsWordBroken", func() {
It("reports the word-broken fields, keywords unless opted out", func() {
for _, f := range []string{"Name", "Title", "audio.artist", "photo.cameraMake"} {
Expect(query.FieldIsWordBroken(f)).To(BeTrue(), f)
}
// opted out (labels, ids, mime type), paths and full text are not
for _, f := range []string{"Tags", "Favorites", "MimeType", "ID", "Content", "Path", "unknown"} {
Expect(query.FieldIsWordBroken(f)).To(BeFalse(), f)
}
})
})
var _ = Describe("FieldIsNumeric and FieldIsDatetime", func() {
It("report the field type, facets included", func() {
for _, f := range []string{"Size", "Type", "audio.duration", "photo.iso", "location.altitude"} {
Expect(query.FieldIsNumeric(f)).To(BeTrue(), f)
Expect(query.FieldIsDatetime(f)).To(BeFalse(), f)
}
Expect(query.FieldIsDatetime("Mtime")).To(BeTrue())
for _, f := range []string{"Name", "Tags", "Hidden", "Content", "Path", "unknown"} {
Expect(query.FieldIsNumeric(f)).To(BeFalse(), f)
Expect(query.FieldIsDatetime(f)).To(BeFalse(), f)
}
})
})
var _ = DescribeTable("ParseNumber",
func(value string, want float64, wantOK bool) {
got, ok := query.ParseNumber(value)
Expect(ok).To(Equal(wantOK))
Expect(got).To(Equal(want))
},
Entry("an integer", "50", 50.0, true),
Entry("a decimal", "1.5", 1.5, true),
Entry("a comparison", ">100", 0.0, false),
Entry("a range", "50..500", 0.0, false),
Entry("a wildcard", "5*", 0.0, false),
Entry("a word", "long", 0.0, false),
Entry("not a finite number", "NaN", 0.0, false),
Entry("nothing", "", 0.0, false),
)
var _ = Describe("Normalize", func() {
It("resolves fields and expands mediatype", func() {
got := norm(
&ast.StringNode{Key: "", Value: "free"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "TAG", Value: "x"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "photo.cameramake", Value: "canon"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "mediatype", Value: "file"},
&ast.OperatorNode{Value: "AND"},
ast.NumberNode{Key: "size", Value: 100},
)
Expect(got).To(Equal([]ast.Node{
&ast.StringNode{Key: "Name", Value: "free", CaseInsensitive: true},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "Tags", Value: "x", CaseInsensitive: true},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "photo.cameraMake", Value: "canon", CaseInsensitive: true},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{Nodes: []ast.Node{
&ast.OperatorNode{Value: "NOT"},
&ast.StringNode{Key: "MimeType", Value: "httpd/unix-directory"},
}},
&ast.OperatorNode{Value: "AND"},
&ast.NumberNode{Key: "Size", Value: 100},
}))
})
It("resolves driveId to RootID and completes the root id", func() {
got := norm(
&ast.StringNode{Key: "driveId", Value: "1$2"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "driveid", Value: "1$2!4"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "RootID", Value: "1$2"},
)
Expect(got).To(Equal([]ast.Node{
&ast.StringNode{Key: "RootID", Value: "1$2!2"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "RootID", Value: "1$2!4"},
&ast.OperatorNode{Value: "AND"},
&ast.StringNode{Key: "RootID", Value: "1$2!2"},
}))
})
// A bare restriction inside a named group inherits the group key; a keyed
// child keeps its own key; a bare restriction in an unnamed group falls
// back to Name.
It("defaults group keys", func() {
got := norm(
&ast.GroupNode{Key: "author", Nodes: []ast.Node{
&ast.StringNode{Value: "b"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "name", Value: "d"},
}},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Value: "e"},
}},
)
Expect(got).To(Equal([]ast.Node{
&ast.GroupNode{Key: "author", Nodes: []ast.Node{
&ast.StringNode{Key: "author", Value: "b"},
&ast.OperatorNode{Value: "OR"},
&ast.StringNode{Key: "Name", Value: "d", CaseInsensitive: true},
}},
&ast.OperatorNode{Value: "AND"},
&ast.GroupNode{Nodes: []ast.Node{
&ast.StringNode{Key: "Name", Value: "e", CaseInsensitive: true},
}},
}))
})
DescribeTable("normalizes path values to the stored relative form",
func(in, want string) {
got := norm(&ast.StringNode{Key: "path", Value: in})
Expect(got).To(Equal([]ast.Node{&ast.StringNode{Key: "Path", Value: want}}))
},
Entry("relative", "./parent", "./parent"),
Entry("trailing slash", "./parent/", "./parent"),
Entry("absolute", "/parent", "./parent"),
Entry("bare", "parent", "./parent"),
Entry("nested bare", "parent/child.jpg", "./parent/child.jpg"),
Entry("root", "/", "."),
Entry("empty", "", "."),
Entry("dot", ".", "."),
Entry("wildcard", "./par*/", "./par*"),
Entry("bare wildcard", "parent/*", "./parent/*"),
Entry("leading wildcard", "*child*", "*child*"),
Entry("leading wildcard covers the prefix", "*/parent", "*/parent"),
Entry("leading single-char wildcard covers the prefix", "?/parent", "?/parent"),
Entry("leading wildcard trailing slash", "*/parent/", "*/parent"),
)
It("converts value nodes to pointers", func() {
got := norm(
ast.StringNode{Key: "name", Value: "x"},
ast.OperatorNode{Value: "AND"},
ast.DateTimeNode{Key: "mtime"},
)
Expect(got).To(Equal([]ast.Node{
&ast.StringNode{Key: "Name", Value: "x", CaseInsensitive: true},
&ast.OperatorNode{Value: "AND"},
&ast.DateTimeNode{Key: "Mtime"},
}))
})
})