Skip to content

Commit 8d0bcd2

Browse files
committed
Added tests for checking nested operators
1 parent dfb1b19 commit 8d0bcd2

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

pkg/services/alerting/eval_handler_test.go

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
)
99

1010
type conditionStub struct {
11-
firing bool
12-
matches []*EvalMatch
11+
firing bool
12+
operator string
13+
matches []*EvalMatch
1314
}
1415

1516
func (c *conditionStub) Eval(context *EvalContext) (*ConditionResult, error) {
16-
return &ConditionResult{Firing: c.firing, EvalMatches: c.matches}, nil
17+
return &ConditionResult{Firing: c.firing, EvalMatches: c.matches, Operator: c.operator}, nil
1718
}
1819

1920
func TestAlertingExecutor(t *testing.T) {
@@ -42,5 +43,81 @@ func TestAlertingExecutor(t *testing.T) {
4243
handler.Eval(context)
4344
So(context.Firing, ShouldEqual, false)
4445
})
46+
47+
Convey("Show return true if any of the condition is passing with OR operator", func() {
48+
context := NewEvalContext(context.TODO(), &Rule{
49+
Conditions: []Condition{
50+
&conditionStub{firing: true, operator: "and"},
51+
&conditionStub{firing: false, operator: "or"},
52+
},
53+
})
54+
55+
handler.Eval(context)
56+
So(context.Firing, ShouldEqual, true)
57+
})
58+
59+
Convey("Show return false if any of the condition is failing with AND operator", func() {
60+
context := NewEvalContext(context.TODO(), &Rule{
61+
Conditions: []Condition{
62+
&conditionStub{firing: true, operator: "and"},
63+
&conditionStub{firing: false, operator: "and"},
64+
},
65+
})
66+
67+
handler.Eval(context)
68+
So(context.Firing, ShouldEqual, false)
69+
})
70+
71+
Convey("Show return true if one condition is failing with nested OR operator", func() {
72+
context := NewEvalContext(context.TODO(), &Rule{
73+
Conditions: []Condition{
74+
&conditionStub{firing: true, operator: "and"},
75+
&conditionStub{firing: true, operator: "and"},
76+
&conditionStub{firing: false, operator: "or"},
77+
},
78+
})
79+
80+
handler.Eval(context)
81+
So(context.Firing, ShouldEqual, true)
82+
})
83+
84+
Convey("Show return false if one condition is passing with nested OR operator", func() {
85+
context := NewEvalContext(context.TODO(), &Rule{
86+
Conditions: []Condition{
87+
&conditionStub{firing: true, operator: "and"},
88+
&conditionStub{firing: false, operator: "and"},
89+
&conditionStub{firing: false, operator: "or"},
90+
},
91+
})
92+
93+
handler.Eval(context)
94+
So(context.Firing, ShouldEqual, false)
95+
})
96+
97+
Convey("Show return false if a condition is failing with nested AND operator", func() {
98+
context := NewEvalContext(context.TODO(), &Rule{
99+
Conditions: []Condition{
100+
&conditionStub{firing: true, operator: "and"},
101+
&conditionStub{firing: false, operator: "and"},
102+
&conditionStub{firing: true, operator: "and"},
103+
},
104+
})
105+
106+
handler.Eval(context)
107+
So(context.Firing, ShouldEqual, false)
108+
})
109+
110+
Convey("Show return true if a condition is passing with nested OR operator", func() {
111+
context := NewEvalContext(context.TODO(), &Rule{
112+
Conditions: []Condition{
113+
&conditionStub{firing: true, operator: "and"},
114+
&conditionStub{firing: false, operator: "or"},
115+
&conditionStub{firing: true, operator: "or"},
116+
},
117+
})
118+
119+
handler.Eval(context)
120+
So(context.Firing, ShouldEqual, true)
121+
})
45122
})
46123
}

0 commit comments

Comments
 (0)