Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Jul 30, 2023
1 parent 1e7652b commit 769e2c4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ variables:
}
```

### Caching layer

DSL helpers that given the same input have deterministic output and are I/O intensive, should set the `Cacheable` property to `true`. This avoids to invoke the function multiple times and return always the same result.

### Thanks

Knetic/govaluate - For providing a powerful expression evaluation package.
Expand Down
33 changes: 31 additions & 2 deletions dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/Knetic/govaluate"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

func TestIndex(t *testing.T) {
Expand Down Expand Up @@ -469,8 +470,36 @@ func TestRandIntDslExpressions(t *testing.T) {
}
}

func evaluateExpression(t *testing.T, dslExpression string) interface{} {
compiledExpression, err := govaluate.NewEvaluableExpressionWithFunctions(dslExpression, DefaultHelperFunctions)
func TestCachingLayer(t *testing.T) {
var (
callCount int
expectedResult = "static value"
cacheableFunc = dslFunction{
IsCacheable: true,
Name: "cacheable_func",
NumberOfArgs: 0,
Signatures: nil,
ExpressionFunction: func(args ...interface{}) (interface{}, error) {
time.Sleep(time.Second)
callCount++
return expectedResult, nil
},
}
)

for i := 0; i < 100; i++ {
result := evaluateExpression(t, "cacheable_func()", cacheableFunc)
require.Equal(t, expectedResult, result)
}
require.Equal(t, 1, callCount)
}

func evaluateExpression(t *testing.T, dslExpression string, functions ...dslFunction) interface{} {
helperFunctions := maps.Clone(DefaultHelperFunctions)
for _, function := range functions {
helperFunctions[function.Name] = function.Exec
}
compiledExpression, err := govaluate.NewEvaluableExpressionWithFunctions(dslExpression, helperFunctions)
require.NoError(t, err, "Error while compiling the %q expression", dslExpression)

actualResult, err := compiledExpression.Evaluate(make(map[string]interface{}))
Expand Down
2 changes: 1 addition & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Regex(regxp string) (*regexp.Regexp, error) {
if err != nil {
return nil, err
}
RegexStore.Set(regxp, compiled)
_ = RegexStore.Set(regxp, compiled)

return compiled, nil
}
2 changes: 1 addition & 1 deletion func.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d dslFunction) Exec(args ...interface{}) (interface{}, error) {
result, err := d.ExpressionFunction(args...)

if d.IsCacheable {
resultCache.Set(functionHash, result)
_ = resultCache.Set(functionHash, result)
}

return result, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/sashabaranov/go-openai v1.14.1
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0
)

require (
Expand All @@ -41,7 +42,6 @@ require (
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
Expand Down

0 comments on commit 769e2c4

Please sign in to comment.