Skip to content

Commit 9baa835

Browse files
committed
include unit test for filters
Signed-off-by: Jeeva Kandasamy <jkandasa@gmail.com>
1 parent 4913a87 commit 9baa835

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package helper
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mycontroller-org/server/v2/pkg/types/cmap"
7+
sfTY "github.com/mycontroller-org/server/v2/pkg/types/service_filter"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestIsMine(t *testing.T) {
12+
testData := []struct {
13+
name string
14+
filter sfTY.ServiceFilter
15+
targetType string
16+
targetID string
17+
targetLabels cmap.CustomStringMap
18+
expectedResult bool
19+
}{
20+
{
21+
name: "empty match",
22+
filter: sfTY.ServiceFilter{},
23+
expectedResult: true,
24+
},
25+
{
26+
name: "targetType match",
27+
targetType: "type-123",
28+
filter: sfTY.ServiceFilter{
29+
MatchAll: false,
30+
Types: []string{"type-123"},
31+
},
32+
expectedResult: true,
33+
},
34+
{
35+
name: "targetID match",
36+
targetID: "type-123",
37+
filter: sfTY.ServiceFilter{
38+
MatchAll: false,
39+
IDs: []string{"type-1", "type-123"},
40+
},
41+
expectedResult: true,
42+
},
43+
{
44+
name: "label match",
45+
targetLabels: cmap.CustomStringMap{
46+
"location": "india",
47+
},
48+
filter: sfTY.ServiceFilter{
49+
MatchAll: false,
50+
Labels: cmap.CustomStringMap{
51+
"location": "india",
52+
},
53+
},
54+
expectedResult: true,
55+
},
56+
{
57+
name: "label or type match",
58+
targetType: "device1",
59+
targetLabels: cmap.CustomStringMap{
60+
"location": "india",
61+
},
62+
filter: sfTY.ServiceFilter{
63+
MatchAll: false,
64+
Types: []string{"device2"},
65+
Labels: cmap.CustomStringMap{
66+
"location": "india",
67+
},
68+
},
69+
expectedResult: true,
70+
},
71+
{
72+
name: "type or label",
73+
targetLabels: cmap.CustomStringMap{
74+
"location": "india",
75+
},
76+
targetType: "mqtt",
77+
filter: sfTY.ServiceFilter{
78+
MatchAll: false,
79+
Types: []string{"mqtt"},
80+
Labels: cmap.CustomStringMap{
81+
"location": "tn",
82+
},
83+
},
84+
expectedResult: true,
85+
},
86+
{
87+
name: "id or type or label",
88+
targetLabels: cmap.CustomStringMap{
89+
"location": "india",
90+
},
91+
targetType: "mqtt",
92+
targetID: "hello123",
93+
filter: sfTY.ServiceFilter{
94+
MatchAll: false,
95+
Types: []string{"serial"},
96+
IDs: []string{"hello123"},
97+
Labels: cmap.CustomStringMap{
98+
"location": "tn",
99+
},
100+
},
101+
expectedResult: true,
102+
},
103+
{
104+
name: "match all",
105+
targetLabels: cmap.CustomStringMap{
106+
"location": "india",
107+
},
108+
targetType: "mqtt",
109+
targetID: "hello123",
110+
filter: sfTY.ServiceFilter{
111+
MatchAll: true,
112+
Types: []string{"mqtt"},
113+
IDs: []string{"hello123"},
114+
Labels: cmap.CustomStringMap{
115+
"location": "india",
116+
},
117+
},
118+
expectedResult: true,
119+
},
120+
{
121+
name: "match all not match",
122+
targetLabels: cmap.CustomStringMap{
123+
"location": "india",
124+
},
125+
targetType: "mqtt",
126+
targetID: "hello123",
127+
filter: sfTY.ServiceFilter{
128+
MatchAll: true,
129+
Types: []string{"serial"},
130+
IDs: []string{"hello123"},
131+
Labels: cmap.CustomStringMap{
132+
"location": "india",
133+
},
134+
},
135+
expectedResult: false,
136+
},
137+
{
138+
name: "match all no match all",
139+
targetLabels: cmap.CustomStringMap{
140+
"location": "india",
141+
},
142+
targetType: "mqtt",
143+
targetID: "hello123",
144+
filter: sfTY.ServiceFilter{
145+
MatchAll: true,
146+
Types: []string{"serial"},
147+
IDs: []string{"hi123"},
148+
Labels: cmap.CustomStringMap{
149+
"location": "tn",
150+
},
151+
},
152+
expectedResult: false,
153+
},
154+
{
155+
name: "target type not match",
156+
targetType: "my-id-123",
157+
filter: sfTY.ServiceFilter{
158+
MatchAll: false,
159+
Types: []string{"type-123"},
160+
},
161+
expectedResult: false,
162+
},
163+
}
164+
165+
for _, test := range testData {
166+
t.Run(test.name, func(t *testing.T) {
167+
actualResult := IsMine(&test.filter, test.targetType, test.targetID, test.targetLabels)
168+
require.Equal(t, test.expectedResult, actualResult)
169+
})
170+
}
171+
172+
}

0 commit comments

Comments
 (0)