mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
standardize processor instance creation in tests
This commit is contained in:
@@ -3,7 +3,6 @@ package processor_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
@@ -83,14 +82,16 @@ func TestScriptExprBadConfigNonCompilingExpression(t *testing.T) {
|
||||
|
||||
func TestGoodScriptExpr(t *testing.T) {
|
||||
tests := []struct {
|
||||
program string
|
||||
name string
|
||||
params map[string]any
|
||||
payload map[string]any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
program: "foo + bar",
|
||||
name: "number",
|
||||
name: "number",
|
||||
params: map[string]any{
|
||||
"expression": "foo + bar",
|
||||
},
|
||||
payload: map[string]any{
|
||||
"foo": 1,
|
||||
"bar": 1,
|
||||
@@ -98,8 +99,10 @@ func TestGoodScriptExpr(t *testing.T) {
|
||||
expected: 2,
|
||||
},
|
||||
{
|
||||
program: "foo + bar",
|
||||
name: "string",
|
||||
name: "string",
|
||||
params: map[string]any{
|
||||
"expression": "foo + bar",
|
||||
},
|
||||
payload: map[string]any{
|
||||
"foo": "1",
|
||||
"bar": "1",
|
||||
@@ -110,14 +113,21 @@ func TestGoodScriptExpr(t *testing.T) {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
program, err := expr.Compile(test.program)
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed to compile program: %s", err)
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
exprProcessor := &processor.ScriptExpr{Program: program}
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
got, err := exprProcessor.Process(t.Context(), test.payload)
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed: %s", err)
|
||||
@@ -133,14 +143,16 @@ func TestGoodScriptExpr(t *testing.T) {
|
||||
|
||||
func TestBadScriptExpr(t *testing.T) {
|
||||
tests := []struct {
|
||||
program string
|
||||
name string
|
||||
params map[string]any
|
||||
payload map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "accessing missing field",
|
||||
program: "foo + bar",
|
||||
name: "accessing missing field",
|
||||
params: map[string]any{
|
||||
"expression": "foo + bar",
|
||||
},
|
||||
payload: map[string]any{
|
||||
"foo": 1,
|
||||
},
|
||||
@@ -150,14 +162,17 @@ func TestBadScriptExpr(t *testing.T) {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
program, err := expr.Compile(test.program)
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed to compile program: %s", err)
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
exprProcessor := &processor.ScriptExpr{Program: program}
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
got, err := exprProcessor.Process(t.Context(), test.payload)
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr expected to fail but succeeded, got: %v", got)
|
||||
|
||||
Reference in New Issue
Block a user