-
Notifications
You must be signed in to change notification settings - Fork 2
/
manager_test.go
115 lines (103 loc) · 3.35 KB
/
manager_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package sensitive
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/sgoware/go-sensitive/store"
)
func Test_NewFilter(t *testing.T) {
type args struct {
storeOption StoreOption
filterOption FilterOption
}
tests := []struct {
name string
args args
}{
{
name: "memory+dfa",
args: args{
storeOption: StoreOption{
Type: StoreMemory,
},
filterOption: FilterOption{
Type: FilterDfa,
},
},
},
{
name: "mongo+dfa",
args: args{
storeOption: StoreOption{
Type: StoreMongo,
MongoConfig: &store.MongoConfig{
Address: "",
Port: "",
Username: "",
Password: "",
Database: "",
Collection: "",
},
},
filterOption: FilterOption{
Type: FilterDfa,
},
},
},
{
name: "mysql+dfa",
args: args{
storeOption: StoreOption{
Type: StoreMysql,
MysqlConfig: &store.MysqlConfig{
Dsn: "",
Database: "",
TableName: "",
},
},
filterOption: FilterOption{
Type: FilterDfa,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filterManager := NewFilter(tt.args.storeOption, tt.args.filterOption)
err := filterManager.LoadDictPath("./dict/default_dict.txt")
if err != nil {
fmt.Println(err)
}
err = filterManager.AddWord("敏感词1", "敏感词2", "敏感词3")
if err != nil {
t.Errorf("add sensitive word failed, err: %v", err)
}
time.Sleep(1 * time.Second)
isSensitive := filterManager.IsSensitive("敏感词1,这是敏感词2,这是敏感词3,这是敏感词1,这里没有敏感词")
if !reflect.DeepEqual(isSensitive, true) {
t.Errorf("IsSensitive() = %v, want %v", isSensitive, true)
}
filtered := filterManager.Remove("这是敏感词1,这是敏感词2,这是敏感词3,这是敏感词1,这里没有敏感词")
if !reflect.DeepEqual(filtered, "这是,这是,这是,这是,这里没有敏感词") {
t.Errorf("Remove() = %v, want %v", filtered, "这是,这是,这是,这是,这里没有敏感词")
}
replaced := filterManager.Replace("这是敏感词1,这是敏感词2,这是敏感词3,这是敏感词1,这里没有敏感词", '*')
if !reflect.DeepEqual(replaced, "这是****,这是****,这是****,这是****,这里没有敏感词") {
t.Errorf("Replace() = %v, want %v", replaced, "这是****,这是****,这是****,这是****,这里没有敏感词")
}
matchedOne := filterManager.FindOne("这是敏感词1,这是敏感词2,这是敏感词3,这是敏感词1,这里没有敏感词")
if !reflect.DeepEqual(matchedOne, "敏感词1") {
t.Errorf("FindOne() = %v, want %v", matchedOne, "敏感词1")
}
matchedAll := filterManager.FindAll("这是敏感词1,这是敏感词2,这是敏感词3,这是敏感词1,这里没有敏感词")
if !reflect.DeepEqual(matchedAll, []string{"敏感词1", "敏感词2", "敏感词3"}) {
t.Errorf("FindAll() = %v, want %v", matchedAll, []string{"敏感词1", "敏感词2", "敏感词3"})
}
matchedMap := filterManager.FindAllCount("这是敏感词1,这是敏感词2,这是敏感词3,这是敏感词1,这里没有敏感词")
if !reflect.DeepEqual(matchedMap, map[string]int{"敏感词1": 2, "敏感词2": 1, "敏感词3": 1}) {
t.Errorf("FindAllCount() = %v, want %v", matchedMap, map[string]int{"敏感词1": 2, "敏感词2": 1, "敏感词3": 1})
}
})
}
}