61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestGlobalSearch(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
s, e := OpenStore(filepath.Join(dir, "search.db"))
|
||
|
|
if e != nil {
|
||
|
|
t.Fatal(e)
|
||
|
|
}
|
||
|
|
defer s.db.Close()
|
||
|
|
a := NewApp()
|
||
|
|
a.store = s
|
||
|
|
|
||
|
|
if hits, e := a.GlobalSearch(" "); e != nil || len(hits) != 0 {
|
||
|
|
t.Fatalf("blank query should return empty, got %v err=%v", hits, e)
|
||
|
|
}
|
||
|
|
|
||
|
|
if _, e := s.SaveProject(0, ProjectInput{Name: "CodeCounter", Path: t.TempDir()}); e != nil {
|
||
|
|
t.Fatal(e)
|
||
|
|
}
|
||
|
|
if _, e := a.SaveTodo(Todo{Title: "写周报 counter 汇总"}); e != nil {
|
||
|
|
t.Fatal(e)
|
||
|
|
}
|
||
|
|
deleted, e := a.SaveTodo(Todo{Title: "counter 已删待办"})
|
||
|
|
if e != nil {
|
||
|
|
t.Fatal(e)
|
||
|
|
}
|
||
|
|
if e := a.DeleteTodo(deleted.ID); e != nil {
|
||
|
|
t.Fatal(e)
|
||
|
|
}
|
||
|
|
|
||
|
|
hits, e := a.GlobalSearch("counter")
|
||
|
|
if e != nil {
|
||
|
|
t.Fatal(e)
|
||
|
|
}
|
||
|
|
var nProj, nTodo int
|
||
|
|
for _, h := range hits {
|
||
|
|
switch h.Kind {
|
||
|
|
case "project":
|
||
|
|
nProj++
|
||
|
|
case "todo":
|
||
|
|
nTodo++
|
||
|
|
if h.Title == "counter 已删待办" {
|
||
|
|
t.Fatal("deleted todo must be excluded")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if nProj != 1 || nTodo != 1 {
|
||
|
|
t.Fatalf("want 1 project + 1 todo, got proj=%d todo=%d (%v)", nProj, nTodo, hits)
|
||
|
|
}
|
||
|
|
|
||
|
|
// LIKE 通配符按字面匹配:% 不应命中所有行。
|
||
|
|
if hits, e := a.GlobalSearch("%"); e != nil || len(hits) != 0 {
|
||
|
|
t.Fatalf("literal %% should not match, got %v err=%v", hits, e)
|
||
|
|
}
|
||
|
|
}
|