-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassifier_test.go
More file actions
176 lines (129 loc) · 4.35 KB
/
classifier_test.go
File metadata and controls
176 lines (129 loc) · 4.35 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package task
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
"github.com/0xef53/go-task/classifiers"
test_utils "github.com/0xef53/go-task/internal/testing"
)
func TestClassifier_Register_EmptyClassifier(t *testing.T) {
format := test_utils.FormatResultString
rootCls := newRootClassifier()
if _, err := rootCls.Register(nil); !errors.Is(err, ErrRegistrationFailed) {
t.Fatal(format(ErrRegistrationFailed, err))
}
}
func TestClassifier_Register_WithDefinedNames(t *testing.T) {
format := test_utils.FormatResultString
rootCls := newRootClassifier()
cls := classifiers.NewUniqueLabelClassifier()
clsNames := []string{"unique-labels", "single-labels", "special-labels"}
if names, err := rootCls.Register(cls, clsNames...); err == nil {
if !reflect.DeepEqual(clsNames, names) {
t.Fatal(format(clsNames, names, "Registered names"))
}
} else {
t.Fatal(format(nil, err))
}
}
func TestClassifier_Register_WithDefaultName(t *testing.T) {
format := test_utils.FormatResultString
rootCls := newRootClassifier()
cls := classifiers.NewUniqueLabelClassifier()
if names, err := rootCls.Register(cls); err == nil {
if len(names) != 1 {
t.Fatal(format(1, len(names)))
}
} else {
t.Fatal(format(nil, err))
}
}
func TestClassifier_Deregister(t *testing.T) {
format := test_utils.FormatResultString
rootCls := newRootClassifier()
cls := classifiers.NewUniqueLabelClassifier()
for i := 1; i <= 5; i++ {
if _, err := rootCls.Register(cls, "unique-labels"); err != nil {
t.Fatal(format(nil, err, fmt.Sprintf("Before deregister (attempt %d)", i)))
}
if err := rootCls.Deregister("unique-labels"); err != nil {
t.Fatal(format(nil, err, fmt.Sprintf("Deregister (attempt %d)", i)))
}
for k := 1; k <= 5; k++ {
if err := rootCls.Deregister("unique-labels"); err != nil {
t.Fatal(format(nil, err, fmt.Sprintf("Repeated deregister (attempt %d)", k)))
}
}
}
}
func TestClassifier_Assign_WithEmptyDefinition(t *testing.T) {
format := test_utils.FormatResultString
rootCls := newRootClassifier()
cls := classifiers.NewUniqueLabelClassifier()
if _, err := rootCls.Register(cls, "unique-labels"); err != nil {
t.Fatal(format(nil, err, "Before assign"))
}
if err := rootCls.Assign(context.Background(), nil, "id-aabbcc123"); !errors.Is(err, ErrAssignmentFailed) {
t.Fatal(format(ErrAssignmentFailed, err))
}
}
func TestClassifier_Assign_WithInvalidDefinition(t *testing.T) {
format := test_utils.FormatResultString
rootCls := newRootClassifier()
cls := classifiers.NewUniqueLabelClassifier()
if _, err := rootCls.Register(cls, "unique-labels"); err != nil {
t.Fatal(format(nil, err, "Before assign"))
}
def := &TaskClassifierDefinition{
Name: "",
}
if err := rootCls.Assign(context.Background(), def, "id-aabbcc123"); !errors.Is(err, ErrAssignmentFailed) {
t.Fatal(format(ErrAssignmentFailed, err))
}
def = &TaskClassifierDefinition{
Name: "unique-labels",
}
if err := rootCls.Assign(context.Background(), def, "id-aabbcc123"); !errors.Is(err, ErrAssignmentFailed) {
t.Fatal(format(ErrAssignmentFailed, err))
}
}
func TestClassifier_Assign_WithUnknownClassifier(t *testing.T) {
format := test_utils.FormatResultString
rootCls := newRootClassifier()
cls := classifiers.NewUniqueLabelClassifier()
if _, err := rootCls.Register(cls, "unique-labels"); err != nil {
t.Fatal(format(nil, err, "Before assign"))
}
def := &TaskClassifierDefinition{
Name: "non-existent-classifier-name",
Opts: &classifiers.UniqueLabelOptions{Label: "task"},
}
if err := rootCls.Assign(context.Background(), def, "id-aabbcc123"); !errors.Is(err, ErrAssignmentFailed) {
t.Fatal(format(ErrAssignmentFailed, err))
}
}
func TestClassifier_Unassign(t *testing.T) {
format := test_utils.FormatResultString
rootCls := newRootClassifier()
cls := classifiers.NewGroupLabelClassifier()
if _, err := rootCls.Register(cls, "group-labels"); err != nil {
t.Fatal(format(nil, err, "Before unassign"))
}
def := &TaskClassifierDefinition{
Name: "group-labels",
Opts: &classifiers.UniqueLabelOptions{Label: "group-1"},
}
for i := range 5001 {
if err := rootCls.Assign(context.Background(), def, fmt.Sprintf("id-%d", i)); err != nil {
t.Fatal(format(nil, err, "Before unassign"))
}
}
for i := range 5000 {
rootCls.Unassign(fmt.Sprintf("id-%d", i))
}
if n := len(rootCls.Get("group-1")); n > 1 {
t.Fatal(format(1, n))
}
}