Skip to content

Commit

Permalink
Add go tests (#70)
Browse files Browse the repository at this point in the history
* Adding go tests with multiple tuples

* Added some go tests

* Add Identifier Go test

* Update Go test

* Added Multiple identifiers go test

* Add Go tests

* Update Go tests

* Removed go sum file

* Remove direct print statements (i.e. fmt) from utils.go
  • Loading branch information
ykalidin authored and rameshpolishetti committed Jul 30, 2019
1 parent 1ce1973 commit 7e1a7e9
Show file tree
Hide file tree
Showing 12 changed files with 1,171 additions and 2 deletions.
3 changes: 1 addition & 2 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"time"

"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -45,7 +44,7 @@ func generateRandomBytes(n int) (io.Reader, error) {
// GetAbsPathForResource gets the absolute resource path off $GOPATH
func GetAbsPathForResource(resourcepath string) string {
GOPATH := os.Getenv("GOPATH")
fmt.Printf("GOPATH - [%s]\n", GOPATH)
// fmt.Printf("GOPATH - [%s]\n", GOPATH)
paths := strings.Split(GOPATH, ":")
for _, path := range paths {
// fmt.Printf("path[%s]\n", path)
Expand Down
74 changes: 74 additions & 0 deletions ruleapi/tests/identifier_1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tests

import (
"context"
"testing"

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

var count int

//Check if all combination of tuples t1 and t3 are triggering actions
func Test_I1(t *testing.T) {

rs, _ := createRuleSession()

rule := ruleapi.NewRule("I1")
rule.AddCondition("I1_c1", []string{"t1.none", "t3.none"}, trueCondition, nil)
rule.SetAction(i1_action)
rule.SetPriority(1)
rs.AddRule(rule)
t.Logf("Rule added: [%s]\n", rule.GetName())

rs.Start(nil)

t1, _ := model.NewTupleWithKeyValues("t1", "t10")
rs.Assert(context.TODO(), t1)

t2, _ := model.NewTupleWithKeyValues("t1", "t11")
rs.Assert(context.TODO(), t2)

t3, _ := model.NewTupleWithKeyValues("t3", "t12")
rs.Assert(context.TODO(), t3)

//Check if the 2 combinations {t1=t10,t3=t12} and {t1=t11,t3=t12} triggers action twice
if count != 2 {
t.Errorf("Expecting [2] actions, got [%d]", count)
t.FailNow()
}

t4, _ := model.NewTupleWithKeyValues("t3", "t13")
rs.Assert(context.TODO(), t4)

//Check if the 2 combinations {t1=t10,t3=t13} and {t1=t11,t3=t13} triggers action two more times making the total action count 4
if count != 4 {
t.Errorf("Expecting [4] actions, got [%d]", count)
t.FailNow()
}

rs.Unregister()

}

func i1_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t1 := tuples[model.TupleType("t1")].(model.MutableTuple)
id1, _ := t1.GetString("id")

t2 := tuples[model.TupleType("t3")].(model.MutableTuple)
id3, _ := t2.GetString("id")

if id1 == "t11" && id3 == "t12" {
count++
}
if id1 == "t10" && id3 == "t12" {
count++
}
if id1 == "t11" && id3 == "t13" {
count++
}
if id1 == "t10" && id3 == "t13" {
count++
}
}
144 changes: 144 additions & 0 deletions ruleapi/tests/identifier_2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package tests

import (
"context"
"testing"

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

var cnt int

//Using 3 Identifiers, different Join conditions and triggering respective actions --->Verify order of actions and count.
func Test_I2(t *testing.T) {

rs, _ := createRuleSession()

//actionMap := make(map[string]string)

rule := ruleapi.NewRule("I21")
rule.AddCondition("I2_c1", []string{"t1.none", "t2.none"}, trueCondition, nil)
rule.SetAction(i21_action)
rule.SetPriority(1)
//rule.SetContext(actionMap)
rs.AddRule(rule)
t.Logf("Rule added: [%s]\n", rule.GetName())

rule1 := ruleapi.NewRule("I22")
rule1.AddCondition("I2_c2", []string{"t1.none", "t3.none"}, trueCondition, nil)
rule1.SetAction(i22_action)
rule1.SetPriority(1)
//rule.SetContext(actionMap)
rs.AddRule(rule1)
t.Logf("Rule added: [%s]\n", rule1.GetName())

rule2 := ruleapi.NewRule("I23")
rule2.AddCondition("I2_c3", []string{"t2.none", "t3.none"}, trueCondition, nil)
rule2.SetAction(i23_action)
rule2.SetPriority(1)
//rule.SetContext(actionMap)
rs.AddRule(rule2)
t.Logf("Rule added: [%s]\n", rule1.GetName())

rule3 := ruleapi.NewRule("I24")
rule3.AddCondition("I2_c4", []string{"t1.none", "t2.none", "t3.none"}, trueCondition, nil)
rule3.SetAction(i24_action)
rule3.SetPriority(1)
//rule.SetContext(actionMap)
rs.AddRule(rule3)
t.Logf("Rule added: [%s]\n", rule2.GetName())

rs.Start(nil)

t1, _ := model.NewTupleWithKeyValues("t1", "t10")
rs.Assert(context.TODO(), t1)

t2, _ := model.NewTupleWithKeyValues("t2", "t11")
rs.Assert(context.TODO(), t2)

if cnt != 1 {
t.Errorf("Expecting [1] actions, got [%d]", cnt)
t.FailNow()
}

t3, _ := model.NewTupleWithKeyValues("t3", "t12")
rs.Assert(context.TODO(), t3)

if cnt != 2 {
t.Errorf("Expecting [2] actions, got [%d]", cnt)
t.FailNow()
}

t4, _ := model.NewTupleWithKeyValues("t2", "t13")
rs.Assert(context.TODO(), t4)

if cnt != 5 {
t.Errorf("Expecting [5] actions, got [%d]", cnt)
t.FailNow()
}

rs.Unregister()

}

func i21_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t1 := tuples[model.TupleType("t1")].(model.MutableTuple)
id1, _ := t1.GetString("id")

t2 := tuples[model.TupleType("t2")].(model.MutableTuple)
id2, _ := t2.GetString("id")

if id1 == "t10" && id2 == "t11" && cnt == 0 {
cnt++
}

if id1 == "t10" && id2 == "t13" {
if cnt >= 2 && cnt <= 4 {
cnt++
}
}
}

func i22_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t1 := tuples[model.TupleType("t1")].(model.MutableTuple)
id1, _ := t1.GetString("id")

t2 := tuples[model.TupleType("t3")].(model.MutableTuple)
id3, _ := t2.GetString("id")

if id1 == "t10" && id3 == "t12" && cnt == 1 {
cnt++
}
}

func i23_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t1 := tuples[model.TupleType("t2")].(model.MutableTuple)
id1, _ := t1.GetString("id")

t2 := tuples[model.TupleType("t3")].(model.MutableTuple)
id3, _ := t2.GetString("id")

if id1 == "t13" && id3 == "t12" {
if cnt >= 2 && cnt <= 4 {
cnt++
}
}
}

func i24_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t1 := tuples[model.TupleType("t1")].(model.MutableTuple)
id1, _ := t1.GetString("id")

t2 := tuples[model.TupleType("t2")].(model.MutableTuple)
id2, _ := t2.GetString("id")

t3 := tuples[model.TupleType("t3")].(model.MutableTuple)
id3, _ := t3.GetString("id")

if id1 == "t10" && id2 == "t13" && id3 == "t12" {
if cnt >= 2 && cnt <= 4 {
cnt++
}
}
}
94 changes: 94 additions & 0 deletions ruleapi/tests/rtctxn_10_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package tests

import (
"context"
"testing"

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

//1 rtc ->Delete multiple tuple types and verify count.
func Test_T10(t *testing.T) {

rs, _ := createRuleSession()

rule := ruleapi.NewRule("R10")
rule.AddCondition("R10_c1", []string{"t1.none", "t3.none"}, trueCondition, nil)
rule.SetAction(r10_action)
rule.SetPriority(1)
rs.AddRule(rule)
t.Logf("Rule added: [%s]\n", rule.GetName())

txnCtx := txnCtx{t, 0}
rs.RegisterRtcTransactionHandler(t10Handler, &txnCtx)
rs.Start(nil)

t1, _ := model.NewTupleWithKeyValues("t1", "t10")
rs.Assert(context.TODO(), t1)

t3, _ := model.NewTupleWithKeyValues("t3", "t11")
rs.Assert(context.TODO(), t3)

rs.Unregister()

}

func r10_action(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t3 := tuples[model.TupleType("t3")].(model.MutableTuple)
//t1.SetString(ctx, "p3", "v3")
id, _ := t3.GetString("id")
if id == "t11" {
tk, _ := model.NewTupleKeyWithKeyValues("t1", "t10")
t10 := rs.GetAssertedTuple(tk).(model.MutableTuple)
if t10 != nil {
rs.Delete(ctx, t10)
}

tk1, _ := model.NewTupleKeyWithKeyValues("t3", "t11")
t11 := rs.GetAssertedTuple(tk1).(model.MutableTuple)
if t11 != nil {
rs.Delete(ctx, t11)
}
}
}

func t10Handler(ctx context.Context, rs model.RuleSession, rtxn model.RtcTxn, handlerCtx interface{}) {

txnCtx := handlerCtx.(*txnCtx)
txnCtx.TxnCnt = txnCtx.TxnCnt + 1
t := txnCtx.Testing
if txnCtx.TxnCnt == 2 {
lA := len(rtxn.GetRtcAdded())
if lA != 1 {
t.Errorf("RtcAdded: Types expected [%d], got [%d]\n", 1, lA)
printTuples(t, "Added", rtxn.GetRtcAdded())
}
lM := len(rtxn.GetRtcModified())
if lM != 0 {
t.Errorf("RtcModified: Expected [%d], got [%d]\n", 0, lM)
printModified(t, rtxn.GetRtcModified())
}
lD := len(rtxn.GetRtcDeleted())
if lD != 2 {
t.Errorf("RtcDeleted: Expected [%d], got [%d]\n", 2, lD)
printTuples(t, "Deleted", rtxn.GetRtcDeleted())
} else {
tuples, _ := rtxn.GetRtcDeleted()["t1"]
if tuples != nil {
if len(tuples) != 1 {
t.Errorf("RtcDeleted: Expected [%d], got [%d]\n", 1, len(tuples))
printTuples(t, "Deleted", rtxn.GetRtcDeleted())
}
}

tuples3, _ := rtxn.GetRtcDeleted()["t3"]
if tuples3 != nil {
if len(tuples3) != 1 {
t.Errorf("RtcDeleted: Expected [%d], got [%d]\n", 1, len(tuples3))
printTuples(t, "Deleted", rtxn.GetRtcDeleted())
}
}
}
}
}
Loading

0 comments on commit 7e1a7e9

Please sign in to comment.