-
Notifications
You must be signed in to change notification settings - Fork 24
/
search_test.go
81 lines (61 loc) · 1.92 KB
/
search_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-FileCopyrightText: 2021-2024 caixw
//
// SPDX-License-Identifier: MIT
package cnregion
import (
"os"
"strings"
"testing"
"github.com/issue9/assert/v4"
"github.com/issue9/cnregion/v2/id"
)
func TestDB_Search(t *testing.T) {
a := assert.New(t, false)
rs := obj.Search(&Options{Text: "合肥"})
a.Equal(1, len(rs)).
Equal(rs[0].name, "合肥")
rs = obj.Search(&Options{Parent: "340000000000", Text: "合肥"})
a.Equal(1, len(rs)).
Equal(rs[0].name, "合肥")
rs = obj.Search(&Options{Parent: "000000000000", Text: "合肥"})
a.Equal(1, len(rs)).
Equal(rs[0].name, "合肥")
// 限定 level 只能是省以及 parent 为 34 开头
rs = obj.Search(&Options{Parent: "340000000000", Level: id.Province, Text: "合肥"})
a.Equal(0, len(rs))
// 未限定 parent 且 level 正确
rs = obj.Search(&Options{Level: id.City, Text: "合肥"})
a.Equal(1, len(rs))
rs = obj.Search(&Options{Level: id.City, Text: "湖"})
a.Equal(2, len(rs))
rs = obj.Search(&Options{Level: id.City, Parent: "340000000000", Text: "湖"})
a.Equal(2, len(rs))
// parent = 浙江
rs = obj.Search(&Options{Parent: "330000000000", Text: "合肥"})
a.Equal(0, len(rs))
// parent 不存在
rs = obj.Search(&Options{Parent: "110000000000", Text: "合肥"})
a.Equal(0, len(rs))
// 只有 Level
rs = obj.Search(&Options{Level: id.City})
a.Equal(4, len(rs))
for _, r := range rs {
a.True(strings.HasSuffix(r.fullID, "00000000"))
}
// 只有 Level
rs = obj.Search(&Options{Level: id.City + id.Town})
a.Equal(4, len(rs))
// 只有 Level
rs = obj.Search(&Options{Level: id.City + id.Province})
a.Equal(6, len(rs))
}
func TestDB_SearchWithData(t *testing.T) {
a := assert.New(t, false)
obj, err := LoadFS(os.DirFS("./data"), "regions.db", "-", true)
a.NotError(err).NotNil(obj)
got := obj.Search(&Options{Text: "温州"})
a.NotEmpty(got)
// Level 不匹配
got = obj.Search(&Options{Text: "温州", Level: id.Province})
a.Empty(got)
}