Skip to content

Commit

Permalink
Improve gotests by checking whether rule action fired or not
Browse files Browse the repository at this point in the history
  • Loading branch information
rameshpolishetti committed May 7, 2019
1 parent f97757b commit 4427af8
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions ruleapi/tests/retract_1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package tests

import (
"context"
"testing"

"github.com/project-flogo/rules/common/model"
"github.com/project-flogo/rules/ruleapi"
"testing"
)

//Retract
Expand All @@ -19,7 +20,9 @@ func Test_Retract_1(t *testing.T) {
t.Logf("%s", err)
t.FailNow()
}
rule.SetAction(assert_action)
ruleActionCtx := make(map[string]string)
rule.SetContext(ruleActionCtx)
rule.SetAction(assertAction)
rule.SetPriority(1)
err = rs.AddRule(rule)
if err != nil {
Expand All @@ -34,7 +37,7 @@ func Test_Retract_1(t *testing.T) {
t.FailNow()
}

//assert a t1
// Case1: assert a t1
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t1", "t1")
Expand All @@ -44,7 +47,8 @@ func Test_Retract_1(t *testing.T) {
t.FailNow()
}
}
//assert a t3 so that the rule fires for keys t1 and t3

// Case2: assert a t3 so that the rule fires for keys t1 and t3
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
Expand All @@ -53,18 +57,27 @@ func Test_Retract_1(t *testing.T) {
t.Logf("%s", err)
t.FailNow()
}
// make sure that rule action got fired by inspecting the rule context
isActionFired, ok := ruleActionCtx["isActionFired"]
if !ok || isActionFired != "Fired" {
t.Log("Case2: rule action not fired")
t.FailNow()
}
delete(ruleActionCtx, "isActionFired") // clear the context for next test case
}
//now retract t3

// Case3: now retract t3
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
rs.Retract(ctx, tuple)
}

/**
Case4:
now assert with same key again, see that the test does not fail, and rule fires
for keys t1 and t3
*/

*/
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
Expand All @@ -73,21 +86,31 @@ func Test_Retract_1(t *testing.T) {
t.Logf("%s", err)
t.FailNow()
}
// make sure that rule action got fired by inspecting the rule context
isActionFired, ok := ruleActionCtx["isActionFired"]
if !ok || isActionFired != "Fired" {
t.Log("Case4: rule action not fired")
t.FailNow()
}
delete(ruleActionCtx, "isActionFired") // clear the context for next test case
}

/**
Case5:
now retract t3 again, just to check if a subsequent t1 does not fire the rule
there by proving that t3 has been retracted
*/
*/
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t3", "t3")
rs.Retract(ctx, tuple)
}

/**
Case6:
now assert another t1 with a different key and observe that rule does not fire
for keys t3 and t11 (since t3 has been retracted)
*/
*/
{
ctx := context.WithValue(context.TODO(), "key", t)
tuple, _ := model.NewTupleWithKeyValues("t1", "t11")
Expand All @@ -96,14 +119,24 @@ func Test_Retract_1(t *testing.T) {
t.Logf("%s", err)
t.FailNow()
}
// make sure that rule action doesn't fire by inspecting the rule context
_, ok := ruleActionCtx["isActionFired"]
if ok {
t.Log("Case6: rule action should not fire")
t.FailNow()
}
}
rs.Unregister()

}

func assert_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
func assertAction(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t := ctx.Value("key").(*testing.T)
t1 := tuples["t1"]
t3 := tuples["t3"]
t.Logf("Rule fired.. [%s], [%s]\n", t1.GetKey().String(), t3.GetKey().String())
}

// add isActionFired to rule context
usableCtx := ruleCtx.(map[string]string)
usableCtx["isActionFired"] = "Fired"
}

0 comments on commit 4427af8

Please sign in to comment.