Skip to content

Commit 6ae0902

Browse files
Merge pull request #51 from project-flogo/chore-improve-gotests
Improve gotests by checking whether rule action fired or not
2 parents f97757b + 4427af8 commit 6ae0902

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

ruleapi/tests/retract_1_test.go

+44-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package tests
22

33
import (
44
"context"
5+
"testing"
6+
57
"github.com/project-flogo/rules/common/model"
68
"github.com/project-flogo/rules/ruleapi"
7-
"testing"
89
)
910

1011
//Retract
@@ -19,7 +20,9 @@ func Test_Retract_1(t *testing.T) {
1920
t.Logf("%s", err)
2021
t.FailNow()
2122
}
22-
rule.SetAction(assert_action)
23+
ruleActionCtx := make(map[string]string)
24+
rule.SetContext(ruleActionCtx)
25+
rule.SetAction(assertAction)
2326
rule.SetPriority(1)
2427
err = rs.AddRule(rule)
2528
if err != nil {
@@ -34,7 +37,7 @@ func Test_Retract_1(t *testing.T) {
3437
t.FailNow()
3538
}
3639

37-
//assert a t1
40+
// Case1: assert a t1
3841
{
3942
ctx := context.WithValue(context.TODO(), "key", t)
4043
tuple, _ := model.NewTupleWithKeyValues("t1", "t1")
@@ -44,7 +47,8 @@ func Test_Retract_1(t *testing.T) {
4447
t.FailNow()
4548
}
4649
}
47-
//assert a t3 so that the rule fires for keys t1 and t3
50+
51+
// Case2: assert a t3 so that the rule fires for keys t1 and t3
4852
{
4953
ctx := context.WithValue(context.TODO(), "key", t)
5054
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
@@ -53,18 +57,27 @@ func Test_Retract_1(t *testing.T) {
5357
t.Logf("%s", err)
5458
t.FailNow()
5559
}
60+
// make sure that rule action got fired by inspecting the rule context
61+
isActionFired, ok := ruleActionCtx["isActionFired"]
62+
if !ok || isActionFired != "Fired" {
63+
t.Log("Case2: rule action not fired")
64+
t.FailNow()
65+
}
66+
delete(ruleActionCtx, "isActionFired") // clear the context for next test case
5667
}
57-
//now retract t3
68+
69+
// Case3: now retract t3
5870
{
5971
ctx := context.WithValue(context.TODO(), "key", t)
6072
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
6173
rs.Retract(ctx, tuple)
6274
}
75+
6376
/**
77+
Case4:
6478
now assert with same key again, see that the test does not fail, and rule fires
6579
for keys t1 and t3
66-
*/
67-
80+
*/
6881
{
6982
ctx := context.WithValue(context.TODO(), "key", t)
7083
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
@@ -73,21 +86,31 @@ func Test_Retract_1(t *testing.T) {
7386
t.Logf("%s", err)
7487
t.FailNow()
7588
}
89+
// make sure that rule action got fired by inspecting the rule context
90+
isActionFired, ok := ruleActionCtx["isActionFired"]
91+
if !ok || isActionFired != "Fired" {
92+
t.Log("Case4: rule action not fired")
93+
t.FailNow()
94+
}
95+
delete(ruleActionCtx, "isActionFired") // clear the context for next test case
7696
}
97+
7798
/**
99+
Case5:
78100
now retract t3 again, just to check if a subsequent t1 does not fire the rule
79101
there by proving that t3 has been retracted
80-
*/
102+
*/
81103
{
82104
ctx := context.WithValue(context.TODO(), "key", t)
83105
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
84106
rs.Retract(ctx, tuple)
85107
}
86108

87109
/**
110+
Case6:
88111
now assert another t1 with a different key and observe that rule does not fire
89112
for keys t3 and t11 (since t3 has been retracted)
90-
*/
113+
*/
91114
{
92115
ctx := context.WithValue(context.TODO(), "key", t)
93116
tuple, _ := model.NewTupleWithKeyValues("t1", "t11")
@@ -96,14 +119,24 @@ func Test_Retract_1(t *testing.T) {
96119
t.Logf("%s", err)
97120
t.FailNow()
98121
}
122+
// make sure that rule action doesn't fire by inspecting the rule context
123+
_, ok := ruleActionCtx["isActionFired"]
124+
if ok {
125+
t.Log("Case6: rule action should not fire")
126+
t.FailNow()
127+
}
99128
}
100129
rs.Unregister()
101130

102131
}
103132

104-
func assert_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
133+
func assertAction(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
105134
t := ctx.Value("key").(*testing.T)
106135
t1 := tuples["t1"]
107136
t3 := tuples["t3"]
108137
t.Logf("Rule fired.. [%s], [%s]\n", t1.GetKey().String(), t3.GetKey().String())
109-
}
138+
139+
// add isActionFired to rule context
140+
usableCtx := ruleCtx.(map[string]string)
141+
usableCtx["isActionFired"] = "Fired"
142+
}

0 commit comments

Comments
 (0)