-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule_test.go
117 lines (108 loc) · 3.04 KB
/
rule_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
116
117
package rulesengine
import (
"testing"
)
func TestNewRule(t *testing.T) {
t.Run("Valid priority types", func(t *testing.T) {
testCases := []struct {
name string
priority int
expected int
expectError bool // Add a field to indicate if an error is expected
}{
{"valid priority 4", 4, 4, false}, // Valid priority should succeed
{"invalid priority -1", 100, 100, false}, // Invalid priority should return an error
{"invalid priority 0", 1, 1, false}, // Priority 0 should return an error
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
allConditions := []*Condition{}
options := RuleConfig{
Name: "Test Rule",
Priority: &tc.priority,
Conditions: Condition{
All: allConditions,
},
Event: EventConfig{
Type: "test",
},
}
rule, err := NewRule(&options)
if tc.expectError {
// Expect an error for invalid priority
if err == nil {
t.Errorf("Expected an error for priority %d, but got none", tc.priority)
}
} else {
// No error expected, validate the rule creation and priority
if err != nil {
t.Errorf("Expected rule creation to succeed, but got error: %v", err)
}
if rule.Priority != tc.expected {
t.Errorf("Expected priority to be %d, but got %d", tc.expected, rule.Priority)
}
}
})
}
})
t.Run("Invalid priority types", func(t *testing.T) {
testCases := []struct {
name string
priority int
expected int
expectError bool // Add a field to indicate if an error is expected
}{
{"invalid priority 0", 0, 0, true}, // Valid priority should succeed
{"invalid priority -1", -1, 0, true}, // Valid priority should succeed
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
allConditions := []*Condition{}
options := RuleConfig{
Name: "Test Rule",
Priority: &tc.priority,
Conditions: Condition{
All: allConditions,
},
Event: EventConfig{
Type: "test",
},
}
rule, err := NewRule(&options)
if tc.expectError {
// Expect an error for invalid priority
if err == nil {
t.Errorf("Expected an error for priority %d, but got none", tc.priority)
}
} else {
// No error expected, validate the rule creation and priority
if err != nil {
t.Errorf("Expected rule creation to succeed, but got error: %v", err)
}
if rule.Priority != tc.expected {
t.Errorf("Expected priority to be %d, but got %d", tc.expected, rule.Priority)
}
}
})
}
})
t.Run("Default priority", func(t *testing.T) {
allConditions := []*Condition{}
options := RuleConfig{
Name: "Test Rule",
Conditions: Condition{
All: allConditions,
},
Event: EventConfig{
Type: "test",
},
}
rule, err := NewRule(&options)
if err != nil {
t.Errorf("Expected rule creation to succeed, but got error: %v", err)
}
if rule.Priority != 1 {
t.Errorf("Expected priority to be 1 (default), but got %d", rule.Priority)
}
})
}