Files
opsxdb/server/internal/db/search_test.go
T

56 lines
1.3 KiB
Go

package db
import (
"context"
"testing"
)
// R2-B6:sqlite 搜索命中表与视图,类型区分正确。
func TestSearchObjectsSQLite(t *testing.T) {
c := newSQLiteFile(t,
`CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)`,
`CREATE TABLE orders (id INTEGER PRIMARY KEY)`,
`CREATE VIEW v_users AS SELECT id, name FROM users`,
)
c.Keyword = "user"
hits, err := SearchObjects(context.Background(), c)
if err != nil {
t.Fatal(err)
}
if len(hits) != 2 {
t.Fatalf("应命中 2 个(user 前缀的表和视图): %+v", hits)
}
byName := map[string]ObjectHit{}
for _, h := range hits {
byName[h.Name] = h
}
if byName["users"].Type != "table" {
t.Fatalf("users 应为 table: %+v", byName["users"])
}
if byName["v_users"].Type != "view" {
t.Fatalf("v_users 应为 view: %+v", byName["v_users"])
}
// 不命中
c.Keyword = "nothing_match"
hits, err = SearchObjects(context.Background(), c)
if err != nil {
t.Fatal(err)
}
if len(hits) != 0 {
t.Fatalf("不应命中: %+v", hits)
}
}
// redis 返回空结果而非报错。
func TestSearchObjectsRedis(t *testing.T) {
c := ConnReq{Driver: "redis", Keyword: "k"}
hits, err := SearchObjects(context.Background(), c)
if err != nil {
t.Fatal(err)
}
if len(hits) != 0 {
t.Fatalf("redis 应返回空: %+v", hits)
}
}