Skip to content

Commit

Permalink
Fixes all test (#400)
Browse files Browse the repository at this point in the history
Co-authored-by: Ferdinand Neman <[email protected]>
  • Loading branch information
newm4n and Ferdinand Neman authored Aug 29, 2023
1 parent 3765de3 commit cb643c1
Show file tree
Hide file tree
Showing 38 changed files with 145 additions and 68 deletions.
8 changes: 7 additions & 1 deletion editor/EvaluationRoute.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ func InitializeEvaluationRoute(router *mux.HyperMux) {
}

eng1 := &engine.GruleEngine{MaxCycle: 5}
kb := knowledgeLibrary.NewKnowledgeBaseInstance("Evaluator", "0.0.1")
kb, err := knowledgeLibrary.NewKnowledgeBaseInstance("Evaluator", "0.0.1")
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte(fmt.Sprintf("Grule Error : %s", err.Error())))

return
}
err = eng1.Execute(dataContext, kb)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
Expand Down
2 changes: 1 addition & 1 deletion editor/StaticRoute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package editor

import (
"embed"
"github.com/hyperjumptech/grule-rule-engine/editor/mime"
mux "github.com/hyperjumptech/hyper-mux"
"github.com/sirupsen/logrus"
"grule-rule-engine/editor/mime"
"net/http"
"os"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion editor/cmd/Main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"github.com/hyperjumptech/grule-rule-engine/editor"
"github.com/sirupsen/logrus"
"grule-rule-engine/editor"
)

func main() {
Expand Down
39 changes: 26 additions & 13 deletions engine/GruleEngine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func TestGrule_Execute(t *testing.T) {
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(rules)))
assert.NoError(t, err)
engine := NewGruleEngine()
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
start := time.Now()
err = engine.Execute(dctx, kb)
assert.NoError(t, err)
Expand Down Expand Up @@ -158,7 +159,8 @@ func TestEngine_ExecuteErr(t *testing.T) {

engine := NewGruleEngine()
engine.ReturnErrOnFailedRuleEvaluation = true
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
err = engine.Execute(dctx, kb)
assert.Error(t, err)
}
Expand Down Expand Up @@ -215,7 +217,8 @@ func TestEngine_ComplexRule1(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(complexRule1)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)

engine := NewGruleEngine()
err = engine.Execute(dctx, kb)
Expand Down Expand Up @@ -250,7 +253,8 @@ func TestEngine_ComplexRule2(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(complexRule2)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)

engine := NewGruleEngine()
err = engine.Execute(dctx, kb)
Expand Down Expand Up @@ -286,7 +290,8 @@ func TestEngine_ComplexRule3(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(complexRule3)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)

engine := NewGruleEngine()
err = engine.Execute(dctx, kb)
Expand Down Expand Up @@ -323,7 +328,8 @@ func TestEngine_ComplexRule4(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(complexRule4)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)

engine := NewGruleEngine()
err = engine.Execute(dctx, kb)
Expand Down Expand Up @@ -352,7 +358,8 @@ func TestEngine_OperatorPrecedence(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(OpPresedenceRule)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)

engine := NewGruleEngine()
err = engine.Execute(dctx, kb)
Expand Down Expand Up @@ -398,7 +405,8 @@ And another`
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(escapedRules)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)

assert.False(t, es.Result1)
assert.False(t, es.Result2)
Expand Down Expand Up @@ -439,7 +447,8 @@ rule KeepSleep "test string escaping" salience 10 {
}
`)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("TestTimer", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("TestTimer", "0.1.1")
assert.NoError(t, err)
engine := NewGruleEngine()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
Expand Down Expand Up @@ -514,7 +523,8 @@ func TestGruleEngine_FetchMatchingRules_Having_Same_Salience(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("conflict_rules_test", "0.1.1", pkg.NewBytesResource([]byte(duplicateRules)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1")
assert.NoError(t, err)

//When
engine := NewGruleEngine()
Expand All @@ -537,7 +547,8 @@ func TestEngine_FetchMatchingRulesErr(t *testing.T) {

engine := NewGruleEngine()
engine.ReturnErrOnFailedRuleEvaluation = true
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
mr, err := engine.FetchMatchingRules(dctx, kb)
assert.Error(t, err)
assert.Nil(t, mr)
Expand Down Expand Up @@ -598,7 +609,8 @@ func TestGruleEngine_FetchMatchingRules_Having_Diff_Salience(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("conflict_rules_test", "0.1.1", pkg.NewBytesResource([]byte(duplicateRulesWithDiffSalience)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1")
assert.NoError(t, err)

//When
engine := NewGruleEngine()
Expand Down Expand Up @@ -661,7 +673,8 @@ func TestGruleEngine_Follows_logical_operator_precedence(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("logical_operator_rules_test", "0.1.1", pkg.NewBytesResource([]byte(logicalOperatorPrecedenceRules)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("logical_operator_rules_test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("logical_operator_rules_test", "0.1.1")
assert.NoError(t, err)

//When
engine := NewGruleEngine()
Expand Down
6 changes: 4 additions & 2 deletions examples/AgeCheckSample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func TestMyPoGo_GetStringLength(t *testing.T) {
ruleBuilder := builder.NewRuleBuilder(lib)
err = ruleBuilder.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(rule2)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 1}
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
Expand Down Expand Up @@ -111,7 +112,8 @@ func TestMyPoGo_Compare(t *testing.T) {

err = ruleBuilder.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(rule3)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 100}
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion examples/ArraySliceMap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ rule SetTreeName "Set the top most tree name" {
ruleBuilder := builder.NewRuleBuilder(lib)
err = ruleBuilder.BuildRuleFromResource("TestFuncChaining", "0.0.1", pkg.NewBytesResource([]byte(rule)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1")
kb, err := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1")
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 1}
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
Expand Down
4 changes: 3 additions & 1 deletion examples/CallingFactFunctionExample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func TestCallingFactFunction(t *testing.T) {
lib := ast.NewKnowledgeLibrary()
ruleBuilder := builder.NewRuleBuilder(lib)
err = ruleBuilder.BuildRuleFromResource("CallingFactFunction", "0.1.1", pkg.NewBytesResource([]byte(CallFactFuncGRL)))
knowledgeBase := lib.NewKnowledgeBaseInstance("CallingFactFunction", "0.1.1")
assert.NoError(t, err)

knowledgeBase, err := lib.NewKnowledgeBaseInstance("CallingFactFunction", "0.1.1")
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 500}
err = eng1.Execute(dataContext, knowledgeBase)
Expand Down
3 changes: 2 additions & 1 deletion examples/CallingLogExample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func TestCallingLog(t *testing.T) {
err := ruleBuilder.BuildRuleFromResource("CallingLog", "0.1.1", pkg.NewBytesResource([]byte(GRL)))
assert.NoError(t, err)

knowledgeBase := lib.NewKnowledgeBaseInstance("CallingLog", "0.1.1")
knowledgeBase, err := lib.NewKnowledgeBaseInstance("CallingLog", "0.1.1")
assert.NoError(t, err)

eng1 := &engine.GruleEngine{MaxCycle: 1}
err = eng1.Execute(dataContext, knowledgeBase)
Expand Down
3 changes: 2 additions & 1 deletion examples/CloneTableIssue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func TestSliceFunctionPanicTest(t *testing.T) {
ruleBuilder := builder.NewRuleBuilder(knowledgeLibrary)
err = ruleBuilder.BuildRuleFromResource("test", "0.0.1", pkg.NewBytesResource([]byte(panickingRule)))
assert.NoError(t, err)
knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("test", "0.0.1")
knowledgeBase, err := knowledgeLibrary.NewKnowledgeBaseInstance("test", "0.0.1")
assert.NoError(t, err)
engine := engine.NewGruleEngine()

err = engine.Execute(dataContext, knowledgeBase)
Expand Down
2 changes: 1 addition & 1 deletion examples/Concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func beginThread(threadName string, lib *ast.KnowledgeLibrary, t *testing.T) {
engine := &engine.GruleEngine{MaxCycle: 100}

// Get an instance of our KnowledgeBase from KnowledgeLibrary
kb := lib.NewKnowledgeBaseInstance("VibonaciTest", "0.0.1")
kb, _ := lib.NewKnowledgeBaseInstance("VibonaciTest", "0.0.1")

// Execute the KnowledgeBase against DataContext
err = engine.Execute(dataContext, kb)
Expand Down
4 changes: 3 additions & 1 deletion examples/EvaluateMissingDataContext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package examples

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"

"github.com/hyperjumptech/grule-rule-engine/ast"
Expand Down Expand Up @@ -36,7 +37,8 @@ func TestDataContextMissingFact(t *testing.T) {

// add JSON fact
json := []byte(`{"blabla":"bla","name":{"first":"john","last":"doe"}}`)
kb := lib.NewKnowledgeBaseInstance("Test", "0.0.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.0.1")
assert.NoError(t, err)
dcx := ast.NewDataContext()

err = dcx.Add("R", oresult)
Expand Down
6 changes: 4 additions & 2 deletions examples/FunctionCallChaining_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ rule SetTreeName "Set the top most tree name" {
ruleBuilder := builder.NewRuleBuilder(lib)
err = ruleBuilder.BuildRuleFromResource("TestFuncChaining", "0.0.1", pkg.NewBytesResource([]byte(rule)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1")
kb, err := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1")
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 1}
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
Expand Down Expand Up @@ -119,7 +120,8 @@ rule SetTreeName "Set the top most tree name" {
ruleBuilder := builder.NewRuleBuilder(lib)
err = ruleBuilder.BuildRuleFromResource("TestFuncChaining", "0.0.1", pkg.NewBytesResource([]byte(rule)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1")
kb, err := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1")
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 1}
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion examples/InterfaceDataContext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ then
rb := builder.NewRuleBuilder(lib)
err := rb.BuildRuleFromResource("CarPriceTest", "0.1.1", pkg.NewBytesResource([]byte(rule)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("CarPriceTest", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("CarPriceTest", "0.1.1")
assert.NoError(t, err)
eng := &engine.GruleEngine{MaxCycle: 3}

dataContext := ast.NewDataContext()
Expand Down
3 changes: 2 additions & 1 deletion examples/Issue108_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func TestIssue108(t *testing.T) {
err = rb.BuildRuleFromResource("Test108", "0.0.1", pkg.NewBytesResource([]byte(Rule108)))
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 5}
kb := lib.NewKnowledgeBaseInstance("Test108", "0.0.1")
kb, err := lib.NewKnowledgeBaseInstance("Test108", "0.0.1")
assert.NoError(t, err)
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
assert.Equal(t, 3, len(Obj.Sequence))
Expand Down
6 changes: 4 additions & 2 deletions examples/Issue328_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func TestMethodCall_SliceOOR(t *testing.T) {
assert.NoError(t, err)

eng1 := &engine.GruleEngine{MaxCycle: 5}
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)

eng1 = &engine.GruleEngine{MaxCycle: 5, ReturnErrOnFailedRuleEvaluation: true}
kb = lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err = lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
err = eng1.Execute(dataContext, kb)
assert.Error(t, err)
}
3 changes: 2 additions & 1 deletion examples/Issue4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func TestMethodCall_Issue4(t *testing.T) {
rb := builder.NewRuleBuilder(lib)
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(Rule4)))
assert.NoError(t, err)
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 3}
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion examples/Issue5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func TestMethodCall_Issue5(t *testing.T) {
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(Rule)))
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 5}
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
assert.Equal(t, "FromRuleScope3", user.GetName())
Expand Down
3 changes: 2 additions & 1 deletion examples/Issue7_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func TestMethodCall_Issue7(t *testing.T) {
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(Rule7)))
assert.NoError(t, err)
eng1 := &engine.GruleEngine{MaxCycle: 5}
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
assert.NoError(t, err)
err = eng1.Execute(dataContext, kb)
assert.NoError(t, err)
assert.Equal(t, "FromRule", user.GetName())
Expand Down
6 changes: 4 additions & 2 deletions examples/ItemArrayExample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (cf *ItemPriceChecker) CheckPrices(t *testing.T) {
err := rb.BuildRuleFromResource("PriceCheck", "0.0.1", pkg.NewBytesResource([]byte(PriceCheckRule1)))
assert.NoError(t, err)

kb := lib.NewKnowledgeBaseInstance("PriceCheck", "0.0.1")
kb, err := lib.NewKnowledgeBaseInstance("PriceCheck", "0.0.1")
assert.NoError(t, err)

// Prepare the engine
eng := engine.NewGruleEngine()
Expand Down Expand Up @@ -168,7 +169,8 @@ func (cf *ItemPriceChecker) CheckCart(t *testing.T) {
err := rb.BuildRuleFromResource("Cart Check Rules", "0.0.1", pkg.NewBytesResource([]byte(PriceCheckRule2)))
assert.NoError(t, err)

kb := lib.NewKnowledgeBaseInstance("Cart Check Rules", "0.0.1")
kb, err := lib.NewKnowledgeBaseInstance("Cart Check Rules", "0.0.1")
assert.NoError(t, err)

// Prepare the engine
eng := engine.NewGruleEngine()
Expand Down
Loading

0 comments on commit cb643c1

Please sign in to comment.