move test implementations to a shared internal package

This commit is contained in:
Joel Wetzell
2026-03-22 22:39:29 -05:00
parent 71b6a6d4a8
commit 9843c116b2
16 changed files with 423 additions and 376 deletions

View File

@@ -1,32 +1,14 @@
package module_test package module_test
import ( import (
"context"
"testing" "testing"
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/module" "github.com/jwetzell/showbridge-go/internal/module"
"github.com/jwetzell/showbridge-go/internal/test"
) )
type TestModule struct {
}
func (m *TestModule) Start(ctx context.Context) error {
<-ctx.Done()
return nil
}
func (m *TestModule) Stop() {}
func (m *TestModule) Type() string {
return "module.test"
}
func (m *TestModule) Id() string {
return "test"
}
func TestModuleBadRegistrationNoType(t *testing.T) { func TestModuleBadRegistrationNoType(t *testing.T) {
defer func() { defer func() {
if r := recover(); r == nil { if r := recover(); r == nil {
@@ -37,7 +19,7 @@ func TestModuleBadRegistrationNoType(t *testing.T) {
module.RegisterModule(module.ModuleRegistration{ module.RegisterModule(module.ModuleRegistration{
Type: "", Type: "",
New: func(config config.ModuleConfig) (common.Module, error) { New: func(config config.ModuleConfig) (common.Module, error) {
return &TestModule{}, nil return &test.TestModule{}, nil
}, },
}) })
} }
@@ -65,14 +47,14 @@ func TestModuleBadRegistrationExistingType(t *testing.T) {
module.RegisterModule(module.ModuleRegistration{ module.RegisterModule(module.ModuleRegistration{
Type: "module.test", Type: "module.test",
New: func(config config.ModuleConfig) (common.Module, error) { New: func(config config.ModuleConfig) (common.Module, error) {
return &TestModule{}, nil return &test.TestModule{}, nil
}, },
}) })
module.RegisterModule(module.ModuleRegistration{ module.RegisterModule(module.ModuleRegistration{
Type: "module.test", Type: "module.test",
New: func(config config.ModuleConfig) (common.Module, error) { New: func(config config.ModuleConfig) (common.Module, error) {
return &TestModule{}, nil return &test.TestModule{}, nil
}, },
}) })
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
_ "modernc.org/sqlite" _ "modernc.org/sqlite"
) )
@@ -35,10 +36,10 @@ func TestDbQueryFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := map[string]any{"sqlite_version()": "3.51.3"} expected := map[string]any{"sqlite_version()": "3.51.3"}
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules(
t.Context(), t.Context(),
map[string]common.Module{ map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}, },
), payload)) ), payload))
if err != nil { if err != nil {
@@ -52,7 +53,7 @@ func TestDbQueryFromRegistry(t *testing.T) {
func TestGoodDbQuery(t *testing.T) { func TestGoodDbQuery(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
@@ -98,8 +99,8 @@ func TestGoodDbQuery(t *testing.T) {
expected: nil, expected: nil,
}, },
} }
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["db.query"] registration, ok := processor.ProcessorRegistry["db.query"]
if !ok { if !ok {
t.Fatalf("db.query processor not registered") t.Fatalf("db.query processor not registered")
@@ -107,26 +108,26 @@ func TestGoodDbQuery(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "db.query", Type: "db.query",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
t.Fatalf("db.query failed to create processor: %s", err) t.Fatalf("db.query failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules(
t.Context(), t.Context(),
map[string]common.Module{ map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}, },
), test.payload)) ), testCase.payload))
if err != nil { if err != nil {
t.Fatalf("db.query processing failed: %s", err) t.Fatalf("db.query processing failed: %s", err)
} }
if !reflect.DeepEqual(got.Payload, test.expected) { if !reflect.DeepEqual(got.Payload, testCase.expected) {
t.Fatalf("db.query got payload: %+v, expected %+v", got.Payload, test.expected) t.Fatalf("db.query got payload: %+v, expected %+v", got.Payload, testCase.expected)
} }
}) })
} }
@@ -142,89 +143,89 @@ func TestBadDbQuery(t *testing.T) {
}{ }{
{ {
name: "no module param", name: "no module param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"query": "SELECT sqlite_version();", "query": "SELECT sqlite_version();",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "db.query module error: not found", errorString: "db.query module error: not found",
}, },
{ {
name: "non string module", name: "non string module",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": 1, "module": 1,
"query": "SELECT sqlite_version();", "query": "SELECT sqlite_version();",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "db.query module error: not a string", errorString: "db.query module error: not a string",
}, },
{ {
name: "no query param", name: "no query param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "db.query query error: not found", errorString: "db.query query error: not found",
}, },
{ {
name: "non string query", name: "non string query",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"query": 1, "query": 1,
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "db.query query error: not a string", errorString: "db.query query error: not a string",
}, },
{ {
name: "query template syntax error", name: "query template syntax error",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"query": "select * from {{", "query": "select * from {{",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "template: query:1: unclosed action", errorString: "template: query:1: unclosed action",
}, },
{ {
name: "query template error", name: "query template error",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"query": "select * from {{.Data}}", "query": "select * from {{.Data}}",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "template: query:1:16: executing \"query\" at <.Data>: can't evaluate field Data in type common.WrappedPayload", errorString: "template: query:1:16: executing \"query\" at <.Data>: can't evaluate field Data in type common.WrappedPayload",
}, },
{ {
name: "query error", name: "query error",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"query": "select * from asdf;", "query": "select * from asdf;",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "db.query error executing query: SQL logic error: no such table: asdf (1)", errorString: "db.query error executing query: SQL logic error: no such table: asdf (1)",
}, },
{ {
name: "no modules in context", name: "no modules in context",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"query": "select * from test;", "query": "select * from test;",
@@ -234,33 +235,33 @@ func TestBadDbQuery(t *testing.T) {
}, },
{ {
name: "module not found in context", name: "module not found in context",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"query": "select * from test;", "query": "select * from test;",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}), wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{}),
errorString: "db.query unable to find module with id: test", errorString: "db.query unable to find module with id: test",
}, },
{ {
name: "module not found in context", name: "module not found in context",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"query": "select * from test;", "query": "select * from test;",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}), wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{}),
errorString: "db.query unable to find module with id: test", errorString: "db.query unable to find module with id: test",
}, },
{ {
name: "module not a DatabseModule", name: "module not a DatabseModule",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"query": "select * from test;", "query": "select * from test;",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), }),
errorString: "db.query module with id test is not a DatabaseModule", errorString: "db.query module with id test is not a DatabaseModule",
}, },

View File

@@ -6,6 +6,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
func TestFilterExprFromRegistry(t *testing.T) { func TestFilterExprFromRegistry(t *testing.T) {
@@ -30,7 +31,7 @@ func TestFilterExprFromRegistry(t *testing.T) {
} }
func TestGoodFilterExpr(t *testing.T) { func TestGoodFilterExpr(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
@@ -41,7 +42,7 @@ func TestGoodFilterExpr(t *testing.T) {
params: map[string]any{ params: map[string]any{
"expression": "Payload.Int > 0", "expression": "Payload.Int > 0",
}, },
payload: TestStruct{ payload: test.TestStruct{
Int: 1, Int: 1,
}, },
match: true, match: true,
@@ -51,7 +52,7 @@ func TestGoodFilterExpr(t *testing.T) {
params: map[string]any{ params: map[string]any{
"expression": "Payload.String == 'hello'", "expression": "Payload.String == 'hello'",
}, },
payload: TestStruct{ payload: test.TestStruct{
String: "hello", String: "hello",
}, },
match: true, match: true,
@@ -61,15 +62,15 @@ func TestGoodFilterExpr(t *testing.T) {
params: map[string]any{ params: map[string]any{
"expression": "Payload.Int > 0", "expression": "Payload.Int > 0",
}, },
payload: TestStruct{ payload: test.TestStruct{
Int: 0, Int: 0,
}, },
match: false, match: false,
}, },
} }
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["filter.expr"] registration, ok := processor.ProcessorRegistry["filter.expr"]
if !ok { if !ok {
t.Fatalf("filter.expr processor not registered") t.Fatalf("filter.expr processor not registered")
@@ -77,22 +78,22 @@ func TestGoodFilterExpr(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "filter.expr", Type: "filter.expr",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
t.Fatalf("filter.expr failed to create processor: %s", err) t.Fatalf("filter.expr failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), testCase.payload))
if err != nil { if err != nil {
t.Fatalf("filter.expr processing failed: %s", err) t.Fatalf("filter.expr processing failed: %s", err)
} }
//TODO(jwetzell): work out better way to compare the any/any //TODO(jwetzell): work out better way to compare the any/any
if got.End != !test.match { if got.End != !testCase.match {
t.Fatalf("filter.expr did fitler properly %+v (%T), expected %+v (%T)", got, got, test.match, test.match) t.Fatalf("filter.expr did fitler properly %+v (%T), expected %+v (%T)", got, got, testCase.match, testCase.match)
} }
}) })
} }
@@ -110,7 +111,7 @@ func TestBadFilterExpr(t *testing.T) {
params: map[string]any{ params: map[string]any{
// no expression parameter // no expression parameter
}, },
payload: TestStruct{}, payload: test.TestStruct{},
errorString: "filter.expr expression error: not found", errorString: "filter.expr expression error: not found",
}, },
{ {
@@ -118,7 +119,7 @@ func TestBadFilterExpr(t *testing.T) {
params: map[string]any{ params: map[string]any{
"expression": 12345, "expression": 12345,
}, },
payload: TestStruct{}, payload: test.TestStruct{},
errorString: "filter.expr expression error: not a string", errorString: "filter.expr expression error: not a string",
}, },
{ {
@@ -126,7 +127,7 @@ func TestBadFilterExpr(t *testing.T) {
params: map[string]any{ params: map[string]any{
"expression": "foo +", "expression": "foo +",
}, },
payload: TestStruct{}, payload: test.TestStruct{},
errorString: "unexpected token EOF (1:5)\n | foo +\n | ....^", errorString: "unexpected token EOF (1:5)\n | foo +\n | ....^",
}, },
{ {

View File

@@ -8,6 +8,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
func TestKvGetFromRegistry(t *testing.T) { func TestKvGetFromRegistry(t *testing.T) {
@@ -34,10 +35,10 @@ func TestKvGetFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := "test" expected := "test"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules(
t.Context(), t.Context(),
map[string]common.Module{ map[string]common.Module{
"test": NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}, },
), payload)) ), payload))
if err != nil { if err != nil {
@@ -51,7 +52,7 @@ func TestKvGetFromRegistry(t *testing.T) {
func TestGoodKvGet(t *testing.T) { func TestGoodKvGet(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
@@ -78,8 +79,8 @@ func TestGoodKvGet(t *testing.T) {
expected: "test", expected: "test",
}, },
} }
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["kv.get"] registration, ok := processor.ProcessorRegistry["kv.get"]
if !ok { if !ok {
t.Fatalf("kv.get processor not registered") t.Fatalf("kv.get processor not registered")
@@ -87,26 +88,26 @@ func TestGoodKvGet(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "kv.get", Type: "kv.get",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
t.Fatalf("kv.get failed to create processor: %s", err) t.Fatalf("kv.get failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules(
t.Context(), t.Context(),
map[string]common.Module{ map[string]common.Module{
"test": NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}, },
), test.payload)) ), testCase.payload))
if err != nil { if err != nil {
t.Fatalf("kv.get processing failed: %s", err) t.Fatalf("kv.get processing failed: %s", err)
} }
if !reflect.DeepEqual(got.Payload, test.expected) { if !reflect.DeepEqual(got.Payload, testCase.expected) {
t.Fatalf("kv.get got payload: %+v, expected %+v", got.Payload, test.expected) t.Fatalf("kv.get got payload: %+v, expected %+v", got.Payload, testCase.expected)
} }
}) })
} }
@@ -122,53 +123,53 @@ func TestBadKvGet(t *testing.T) {
}{ }{
{ {
name: "no module param", name: "no module param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), }),
errorString: "kv.get module error: not found", errorString: "kv.get module error: not found",
}, },
{ {
name: "non string module", name: "non string module",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": 1, "module": 1,
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), }),
errorString: "kv.get module error: not a string", errorString: "kv.get module error: not a string",
}, },
{ {
name: "no key param", name: "no key param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), }),
errorString: "kv.get key error: not found", errorString: "kv.get key error: not found",
}, },
{ {
name: "non string key", name: "non string key",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": 1, "key": 1,
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestKVModule("test"), "test": test.NewTestKVModule("test"),
}), }),
errorString: "kv.get key error: not a string", errorString: "kv.get key error: not a string",
}, },
{ {
name: "no modules in context", name: "no modules in context",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
@@ -178,23 +179,23 @@ func TestBadKvGet(t *testing.T) {
}, },
{ {
name: "module not found in context", name: "module not found in context",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}), wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{}),
errorString: "kv.get unable to find module with id: test", errorString: "kv.get unable to find module with id: test",
}, },
{ {
name: "module not a kv module", name: "module not a kv module",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "kv.get module with id test is not a KeyValueModule", errorString: "kv.get module with id test is not a KeyValueModule",
}, },

View File

@@ -8,6 +8,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
func TestKvSetFromRegistry(t *testing.T) { func TestKvSetFromRegistry(t *testing.T) {
@@ -35,10 +36,10 @@ func TestKvSetFromRegistry(t *testing.T) {
payload := "" payload := ""
expected := "" expected := ""
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules(
t.Context(), t.Context(),
map[string]common.Module{ map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}, },
), payload)) ), payload))
if err != nil { if err != nil {
@@ -52,7 +53,7 @@ func TestKvSetFromRegistry(t *testing.T) {
func TestGoodKvSet(t *testing.T) { func TestGoodKvSet(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
@@ -69,8 +70,8 @@ func TestGoodKvSet(t *testing.T) {
expected: "", expected: "",
}, },
} }
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["kv.set"] registration, ok := processor.ProcessorRegistry["kv.set"]
if !ok { if !ok {
t.Fatalf("kv.set processor not registered") t.Fatalf("kv.set processor not registered")
@@ -78,33 +79,33 @@ func TestGoodKvSet(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "kv.set", Type: "kv.set",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
t.Fatalf("kv.set failed to create processor: %s", err) t.Fatalf("kv.set failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules(
t.Context(), t.Context(),
map[string]common.Module{ map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}, },
), test.payload)) ), testCase.payload))
if err != nil { if err != nil {
t.Fatalf("kv.set processing failed: %s", err) t.Fatalf("kv.set processing failed: %s", err)
} }
if !reflect.DeepEqual(got.Payload, test.expected) { if !reflect.DeepEqual(got.Payload, testCase.expected) {
t.Fatalf("kv.set got payload: %+v, expected %+v", got.Payload, test.expected) t.Fatalf("kv.set got payload: %+v, expected %+v", got.Payload, testCase.expected)
} }
}) })
} }
} }
func TestBadKvSet(t *testing.T) { func TestBadKvSet(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
@@ -113,82 +114,82 @@ func TestBadKvSet(t *testing.T) {
}{ }{
{ {
name: "no module param", name: "no module param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"key": "test", "key": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}), }),
errorString: "kv.set module error: not found", errorString: "kv.set module error: not found",
}, },
{ {
name: "non string module", name: "non string module",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": 1, "module": 1,
"key": "test", "key": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}), }),
errorString: "kv.set module error: not a string", errorString: "kv.set module error: not a string",
}, },
{ {
name: "no key param", name: "no key param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}), }),
errorString: "kv.set key error: not found", errorString: "kv.set key error: not found",
}, },
{ {
name: "non string key", name: "non string key",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": 1, "key": 1,
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}), }),
errorString: "kv.set key error: not a string", errorString: "kv.set key error: not a string",
}, },
{ {
name: "no value param", name: "no value param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}), }),
errorString: "kv.set value error: not found", errorString: "kv.set value error: not found",
}, },
{ {
name: "non string value", name: "non string value",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
"value": 1, "value": 1,
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}), }),
errorString: "kv.set value error: not a string", errorString: "kv.set value error: not a string",
}, },
{ {
name: "no modules in context", name: "no modules in context",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
@@ -199,58 +200,58 @@ func TestBadKvSet(t *testing.T) {
}, },
{ {
name: "value template syntax error", name: "value template syntax error",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
"value": "{{", "value": "{{",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}), }),
errorString: "template: template:1: unclosed action", errorString: "template: template:1: unclosed action",
}, },
{ {
name: "value template execution error", name: "value template execution error",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
"value": "{{.Data}}", "value": "{{.Data}}",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{}, "test": &test.TestKVModule{},
}), }),
errorString: "template: template:1:2: executing \"template\" at <.Data>: can't evaluate field Data in type common.WrappedPayload", errorString: "template: template:1:2: executing \"template\" at <.Data>: can't evaluate field Data in type common.WrappedPayload",
}, },
{ {
name: "module not found in context", name: "module not found in context",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
"value": "hello", "value": "hello",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}), wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{}),
errorString: "kv.set unable to find module with id: test", errorString: "kv.set unable to find module with id: test",
}, },
{ {
name: "module not a kv module", name: "module not a kv module",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
"key": "test", "key": "test",
"value": "hello", "value": "hello",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"), "test": test.NewTestDBModule("test"),
}), }),
errorString: "kv.set module with id test is not a KeyValueModule", errorString: "kv.set module with id test is not a KeyValueModule",
}, },
} }
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["kv.set"] registration, ok := processor.ProcessorRegistry["kv.set"]
if !ok { if !ok {
@@ -259,24 +260,24 @@ func TestBadKvSet(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "kv.set", Type: "kv.set",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
if test.errorString != err.Error() { if testCase.errorString != err.Error() {
t.Fatalf("kv.set got error '%s', expected '%s'", err.Error(), test.errorString) t.Fatalf("kv.set got error '%s', expected '%s'", err.Error(), testCase.errorString)
} }
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload))
if err == nil { if err == nil {
t.Fatalf("kv.set expected to fail but got payload: %+v", got) t.Fatalf("kv.set expected to fail but got payload: %+v", got)
} }
if err.Error() != test.errorString { if err.Error() != testCase.errorString {
t.Fatalf("kv.set got error '%s', expected '%s'", err.Error(), test.errorString) t.Fatalf("kv.set got error '%s', expected '%s'", err.Error(), testCase.errorString)
} }
}) })
} }

View File

@@ -1,169 +1,13 @@
package processor_test package processor_test
import ( import (
"context"
"database/sql"
"testing" "testing"
"github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
type TestStruct struct {
String string
Int int
Float float64
Bool bool
Data any
IntSlice []int
}
func (t TestStruct) GetString() string {
return t.String
}
func (t TestStruct) GetInt() int {
return t.Int
}
func (t TestStruct) GetFloat() float64 {
return t.Float
}
func (t TestStruct) GetBool() bool {
return t.Bool
}
func (t TestStruct) GetData() any {
return t.Data
}
func (t TestStruct) GetIntSlice() []int {
return t.IntSlice
}
func (t TestStruct) Void() {}
func (t TestStruct) MultipleReturnValues() (string, int) {
return t.String, t.Int
}
type TestProcessor struct {
}
func (p *TestProcessor) Type() string {
return "test"
}
func (p *TestProcessor) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
return wrappedPayload, nil
}
func NewTestKVModule(id string) *TestKVModule {
return &TestKVModule{
id: id,
}
}
type TestKVModule struct {
id string
kvData map[string]any
}
func (m *TestKVModule) Start(ctx context.Context) error {
<-ctx.Done()
return nil
}
func (m *TestKVModule) Stop() {}
func (m *TestKVModule) Type() string {
return "module.test.kv"
}
func (m *TestKVModule) Id() string {
return m.id
}
func (m *TestKVModule) Get(key string) (any, error) {
return key, nil
}
func (m *TestKVModule) Set(key string, value any) error {
if m.kvData == nil {
m.kvData = make(map[string]any)
}
m.kvData[key] = value
return nil
}
func NewTestDBModule(id string) *TestDBModule {
return &TestDBModule{
id: id,
}
}
type TestDBModule struct {
id string
db *sql.DB
}
func (m *TestDBModule) Start(ctx context.Context) error {
<-ctx.Done()
return nil
}
func (m *TestDBModule) Database() *sql.DB {
if m.db == nil {
db, _ := sql.Open("sqlite", ":memory:")
db.Exec(`
CREATE TABLE test (
id INTEGER PRIMARY KEY,
value TEXT
);
INSERT INTO test (id, value) VALUES (1, 'test-1'), (2, 'test-2');
`)
m.db = db
}
return m.db
}
func (m *TestDBModule) Stop() {}
func (m *TestDBModule) Type() string {
return "module.test.db"
}
func (m *TestDBModule) Id() string {
return m.id
}
func GetNewTestRouter() *TestRouter {
return &TestRouter{}
}
type TestRouter struct {
}
func (r *TestRouter) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []common.RouteIOError) {
return false, nil
}
func (r *TestRouter) HandleOutput(ctx context.Context, destinationId string, payload any) error {
return nil
}
func GetContextWithModules(ctx context.Context, modules map[string]common.Module) context.Context {
ctx = context.WithValue(ctx, common.ModulesContextKey, modules)
return ctx
}
func GetContextWithRouter(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, common.RouterContextKey, GetNewTestRouter())
return ctx
}
func TestProcessorBadRegistrationNoType(t *testing.T) { func TestProcessorBadRegistrationNoType(t *testing.T) {
defer func() { defer func() {
if r := recover(); r == nil { if r := recover(); r == nil {
@@ -174,7 +18,7 @@ func TestProcessorBadRegistrationNoType(t *testing.T) {
processor.RegisterProcessor(processor.ProcessorRegistration{ processor.RegisterProcessor(processor.ProcessorRegistration{
Type: "", Type: "",
New: func(config config.ProcessorConfig) (processor.Processor, error) { New: func(config config.ProcessorConfig) (processor.Processor, error) {
return &TestProcessor{}, nil return &test.TestProcessor{}, nil
}, },
}) })
} }
@@ -202,7 +46,7 @@ func TestProcessorBadRegistrationExistingType(t *testing.T) {
processor.RegisterProcessor(processor.ProcessorRegistration{ processor.RegisterProcessor(processor.ProcessorRegistration{
Type: "string.create", Type: "string.create",
New: func(config config.ProcessorConfig) (processor.Processor, error) { New: func(config config.ProcessorConfig) (processor.Processor, error) {
return &TestProcessor{}, nil return &test.TestProcessor{}, nil
}, },
}) })
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
func TestRouterOutputFromRegistry(t *testing.T) { func TestRouterOutputFromRegistry(t *testing.T) {
@@ -34,7 +35,7 @@ func TestRouterOutputFromRegistry(t *testing.T) {
payload := "test" payload := "test"
expected := "test" expected := "test"
got, err := processorInstance.Process(GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(test.GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload))
if err != nil { if err != nil {
t.Fatalf("router.output processing failed: %s", err) t.Fatalf("router.output processing failed: %s", err)
} }
@@ -46,15 +47,15 @@ func TestRouterOutputFromRegistry(t *testing.T) {
func TestGoodRouterOutput(t *testing.T) { func TestGoodRouterOutput(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
expected any expected any
}{} }{}
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["router.output"] registration, ok := processor.ProcessorRegistry["router.output"]
if !ok { if !ok {
@@ -63,27 +64,27 @@ func TestGoodRouterOutput(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "router.output", Type: "router.output",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
t.Fatalf("router.output failed to create processor: %s", err) t.Fatalf("router.output failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithRouter(t.Context()), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithRouter(t.Context()), testCase.payload))
if err != nil { if err != nil {
t.Fatalf("router.output processing failed: %s", err) t.Fatalf("router.output processing failed: %s", err)
} }
if !reflect.DeepEqual(got.Payload, test.expected) { if !reflect.DeepEqual(got.Payload, testCase.expected) {
t.Fatalf("router.output got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, test.expected, test.expected) t.Fatalf("router.output got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, testCase.expected, testCase.expected)
} }
}) })
} }
} }
func TestBadRouterOutput(t *testing.T) { func TestBadRouterOutput(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
@@ -95,7 +96,7 @@ func TestBadRouterOutput(t *testing.T) {
name: "no module param", name: "no module param",
params: map[string]any{}, params: map[string]any{},
payload: "test", payload: "test",
processCtx: GetContextWithRouter(t.Context()), processCtx: test.GetContextWithRouter(t.Context()),
wrappedPayloadCtx: t.Context(), wrappedPayloadCtx: t.Context(),
errorString: "router.output module error: not found", errorString: "router.output module error: not found",
}, },
@@ -105,7 +106,7 @@ func TestBadRouterOutput(t *testing.T) {
"module": 123, "module": 123,
}, },
payload: "test", payload: "test",
processCtx: GetContextWithRouter(t.Context()), processCtx: test.GetContextWithRouter(t.Context()),
wrappedPayloadCtx: t.Context(), wrappedPayloadCtx: t.Context(),
errorString: "router.output module error: not a string", errorString: "router.output module error: not a string",
}, },
@@ -121,8 +122,8 @@ func TestBadRouterOutput(t *testing.T) {
}, },
} }
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["router.output"] registration, ok := processor.ProcessorRegistry["router.output"]
if !ok { if !ok {
@@ -131,24 +132,24 @@ func TestBadRouterOutput(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "router.output", Type: "router.output",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
if test.errorString != err.Error() { if testCase.errorString != err.Error() {
t.Fatalf("router.output got error '%s', expected '%s'", err.Error(), test.errorString) t.Fatalf("router.output got error '%s', expected '%s'", err.Error(), testCase.errorString)
} }
return return
} }
got, err := processorInstance.Process(test.processCtx, common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload)) got, err := processorInstance.Process(testCase.processCtx, common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload))
if err == nil { if err == nil {
t.Fatalf("router.output expected to fail but succeeded, got: %v", got) t.Fatalf("router.output expected to fail but succeeded, got: %v", got)
} }
if err.Error() != test.errorString { if err.Error() != testCase.errorString {
t.Fatalf("router.output got error '%s', expected '%s'", err.Error(), test.errorString) t.Fatalf("router.output got error '%s', expected '%s'", err.Error(), testCase.errorString)
} }
}) })
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
func TestRouterInputFromRegistry(t *testing.T) { func TestRouterInputFromRegistry(t *testing.T) {
@@ -34,7 +35,7 @@ func TestRouterInputFromRegistry(t *testing.T) {
payload := "test" payload := "test"
expected := "test" expected := "test"
got, err := processorInstance.Process(GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(test.GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload))
if err != nil { if err != nil {
t.Fatalf("router.input processing failed: %s", err) t.Fatalf("router.input processing failed: %s", err)
} }
@@ -46,15 +47,15 @@ func TestRouterInputFromRegistry(t *testing.T) {
func TestGoodRouterInput(t *testing.T) { func TestGoodRouterInput(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
expected any expected any
}{} }{}
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["router.input"] registration, ok := processor.ProcessorRegistry["router.input"]
if !ok { if !ok {
@@ -63,27 +64,27 @@ func TestGoodRouterInput(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "router.input", Type: "router.input",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
t.Fatalf("router.input failed to create processor: %s", err) t.Fatalf("router.input failed to create processor: %s", err)
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithRouter(t.Context()), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithRouter(t.Context()), testCase.payload))
if err != nil { if err != nil {
t.Fatalf("router.input processing failed: %s", err) t.Fatalf("router.input processing failed: %s", err)
} }
if !reflect.DeepEqual(got.Payload, test.expected) { if !reflect.DeepEqual(got.Payload, testCase.expected) {
t.Fatalf("router.input got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, test.expected, test.expected) t.Fatalf("router.input got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, testCase.expected, testCase.expected)
} }
}) })
} }
} }
func TestBadRouterInput(t *testing.T) { func TestBadRouterInput(t *testing.T) {
tests := []struct { testCases := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
@@ -95,7 +96,7 @@ func TestBadRouterInput(t *testing.T) {
name: "no source param", name: "no source param",
params: map[string]any{}, params: map[string]any{},
payload: "test", payload: "test",
processCtx: GetContextWithRouter(t.Context()), processCtx: test.GetContextWithRouter(t.Context()),
wrappedPayloadCtx: t.Context(), wrappedPayloadCtx: t.Context(),
errorString: "router.input source error: not found", errorString: "router.input source error: not found",
}, },
@@ -105,7 +106,7 @@ func TestBadRouterInput(t *testing.T) {
"source": 123, "source": 123,
}, },
payload: "test", payload: "test",
processCtx: GetContextWithRouter(t.Context()), processCtx: test.GetContextWithRouter(t.Context()),
wrappedPayloadCtx: t.Context(), wrappedPayloadCtx: t.Context(),
errorString: "router.input source error: not a string", errorString: "router.input source error: not a string",
}, },
@@ -121,8 +122,8 @@ func TestBadRouterInput(t *testing.T) {
}, },
} }
for _, test := range tests { for _, testCase := range testCases {
t.Run(test.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["router.input"] registration, ok := processor.ProcessorRegistry["router.input"]
if !ok { if !ok {
@@ -131,24 +132,24 @@ func TestBadRouterInput(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "router.input", Type: "router.input",
Params: test.params, Params: testCase.params,
}) })
if err != nil { if err != nil {
if test.errorString != err.Error() { if testCase.errorString != err.Error() {
t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), test.errorString) t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), testCase.errorString)
} }
return return
} }
got, err := processorInstance.Process(test.processCtx, common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload)) got, err := processorInstance.Process(testCase.processCtx, common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload))
if err == nil { if err == nil {
t.Fatalf("router.input expected to fail but succeeded, got: %v", got) t.Fatalf("router.input expected to fail but succeeded, got: %v", got)
} }
if err.Error() != test.errorString { if err.Error() != testCase.errorString {
t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), test.errorString) t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), testCase.errorString)
} }
}) })
} }

View File

@@ -6,6 +6,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
func TestStringCreateFromRegistry(t *testing.T) { func TestStringCreateFromRegistry(t *testing.T) {
@@ -70,13 +71,13 @@ func TestGoodStringCreate(t *testing.T) {
{ {
name: "struct payload - field", name: "struct payload - field",
params: map[string]any{"template": "{{.Payload.Data}}"}, params: map[string]any{"template": "{{.Payload.Data}}"},
payload: TestStruct{Data: "test"}, payload: test.TestStruct{Data: "test"},
expected: "test", expected: "test",
}, },
{ {
name: "struct payload - method", name: "struct payload - method",
params: map[string]any{"template": "{{.Payload.GetData}}"}, params: map[string]any{"template": "{{.Payload.GetData}}"},
payload: TestStruct{Data: "test"}, payload: test.TestStruct{Data: "test"},
expected: "test", expected: "test",
}, },
} }

View File

@@ -7,6 +7,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
func TestStructFieldGetFromRegistry(t *testing.T) { func TestStructFieldGetFromRegistry(t *testing.T) {
@@ -29,7 +30,7 @@ func TestStructFieldGetFromRegistry(t *testing.T) {
t.Fatalf("struct.field.get processor has wrong type: %s", processorInstance.Type()) t.Fatalf("struct.field.get processor has wrong type: %s", processorInstance.Type())
} }
payload := TestStruct{Data: "hello"} payload := test.TestStruct{Data: "hello"}
expected := "hello" expected := "hello"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload))
@@ -53,31 +54,31 @@ func TestGoodStructFieldGet(t *testing.T) {
{ {
name: "string field", name: "string field",
params: map[string]any{"name": "String"}, params: map[string]any{"name": "String"},
payload: TestStruct{String: "hello"}, payload: test.TestStruct{String: "hello"},
expected: "hello", expected: "hello",
}, },
{ {
name: "int field", name: "int field",
params: map[string]any{"name": "Int"}, params: map[string]any{"name": "Int"},
payload: TestStruct{Int: 42}, payload: test.TestStruct{Int: 42},
expected: 42, expected: 42,
}, },
{ {
name: "float field", name: "float field",
params: map[string]any{"name": "Float"}, params: map[string]any{"name": "Float"},
payload: TestStruct{Float: 3.14}, payload: test.TestStruct{Float: 3.14},
expected: 3.14, expected: 3.14,
}, },
{ {
name: "bool field", name: "bool field",
params: map[string]any{"name": "Bool"}, params: map[string]any{"name": "Bool"},
payload: TestStruct{Bool: true}, payload: test.TestStruct{Bool: true},
expected: true, expected: true,
}, },
{ {
name: "pointer to struct payload", name: "pointer to struct payload",
params: map[string]any{"name": "Data"}, params: map[string]any{"name": "Data"},
payload: &TestStruct{Data: "hello"}, payload: &test.TestStruct{Data: "hello"},
expected: "hello", expected: "hello",
}, },
{ {
@@ -85,7 +86,7 @@ func TestGoodStructFieldGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"name": "IntSlice", "name": "IntSlice",
}, },
payload: TestStruct{IntSlice: []int{1, 2, 3}}, payload: test.TestStruct{IntSlice: []int{1, 2, 3}},
expected: []int{1, 2, 3}, expected: []int{1, 2, 3},
}, },
} }
@@ -128,13 +129,13 @@ func TestBadStructFieldGet(t *testing.T) {
}{ }{
{ {
name: "no name param", name: "no name param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{}, params: map[string]any{},
errorString: "struct.field.get name error: not found", errorString: "struct.field.get name error: not found",
}, },
{ {
name: "non string name", name: "non string name",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"name": 1, "name": 1,
}, },
@@ -142,7 +143,7 @@ func TestBadStructFieldGet(t *testing.T) {
}, },
{ {
name: "missing field", name: "missing field",
payload: TestStruct{String: "hello"}, payload: test.TestStruct{String: "hello"},
params: map[string]any{ params: map[string]any{
"name": "NonExistentField", "name": "NonExistentField",
}, },

View File

@@ -7,6 +7,7 @@ import (
"github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/common"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/processor"
"github.com/jwetzell/showbridge-go/internal/test"
) )
func TestStructMethodGetFromRegistry(t *testing.T) { func TestStructMethodGetFromRegistry(t *testing.T) {
@@ -29,7 +30,7 @@ func TestStructMethodGetFromRegistry(t *testing.T) {
t.Fatalf("struct.method.get processor has wrong type: %s", processorInstance.Type()) t.Fatalf("struct.method.get processor has wrong type: %s", processorInstance.Type())
} }
payload := TestStruct{Data: "hello"} payload := test.TestStruct{Data: "hello"}
expected := "hello" expected := "hello"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload))
@@ -53,25 +54,25 @@ func TestGoodStructMethodGet(t *testing.T) {
{ {
name: "string field", name: "string field",
params: map[string]any{"name": "GetString"}, params: map[string]any{"name": "GetString"},
payload: TestStruct{String: "hello"}, payload: test.TestStruct{String: "hello"},
expected: "hello", expected: "hello",
}, },
{ {
name: "int field", name: "int field",
params: map[string]any{"name": "GetInt"}, params: map[string]any{"name": "GetInt"},
payload: TestStruct{Int: 42}, payload: test.TestStruct{Int: 42},
expected: 42, expected: 42,
}, },
{ {
name: "float field", name: "float field",
params: map[string]any{"name": "GetFloat"}, params: map[string]any{"name": "GetFloat"},
payload: TestStruct{Float: 3.14}, payload: test.TestStruct{Float: 3.14},
expected: 3.14, expected: 3.14,
}, },
{ {
name: "bool field", name: "bool field",
params: map[string]any{"name": "GetBool"}, params: map[string]any{"name": "GetBool"},
payload: TestStruct{Bool: true}, payload: test.TestStruct{Bool: true},
expected: true, expected: true,
}, },
{ {
@@ -79,7 +80,7 @@ func TestGoodStructMethodGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"name": "GetData", "name": "GetData",
}, },
payload: TestStruct{Data: []string{"hello"}}, payload: test.TestStruct{Data: []string{"hello"}},
expected: []string{"hello"}, expected: []string{"hello"},
}, },
{ {
@@ -87,7 +88,7 @@ func TestGoodStructMethodGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"name": "Void", "name": "Void",
}, },
payload: TestStruct{}, payload: test.TestStruct{},
expected: nil, expected: nil,
}, },
{ {
@@ -95,7 +96,7 @@ func TestGoodStructMethodGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"name": "GetData", "name": "GetData",
}, },
payload: &TestStruct{Data: "hello"}, payload: &test.TestStruct{Data: "hello"},
expected: "hello", expected: "hello",
}, },
{ {
@@ -103,7 +104,7 @@ func TestGoodStructMethodGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"name": "GetIntSlice", "name": "GetIntSlice",
}, },
payload: TestStruct{IntSlice: []int{1, 2, 3}}, payload: test.TestStruct{IntSlice: []int{1, 2, 3}},
expected: []int{1, 2, 3}, expected: []int{1, 2, 3},
}, },
{ {
@@ -111,7 +112,7 @@ func TestGoodStructMethodGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"name": "MultipleReturnValues", "name": "MultipleReturnValues",
}, },
payload: TestStruct{String: "hello", Int: 42}, payload: test.TestStruct{String: "hello", Int: 42},
expected: []any{"hello", 42}, expected: []any{"hello", 42},
}, },
} }
@@ -153,13 +154,13 @@ func TestBadStructMethodGet(t *testing.T) {
}{ }{
{ {
name: "no name param", name: "no name param",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{}, params: map[string]any{},
errorString: "struct.method.get name error: not found", errorString: "struct.method.get name error: not found",
}, },
{ {
name: "non string name", name: "non string name",
payload: TestStruct{Data: "hello"}, payload: test.TestStruct{Data: "hello"},
params: map[string]any{ params: map[string]any{
"name": 1, "name": 1,
}, },
@@ -167,7 +168,7 @@ func TestBadStructMethodGet(t *testing.T) {
}, },
{ {
name: "missing method", name: "missing method",
payload: TestStruct{String: "hello"}, payload: test.TestStruct{String: "hello"},
params: map[string]any{ params: map[string]any{
"name": "NonExistentMethod", "name": "NonExistentMethod",
}, },

27
internal/test/context.go Normal file
View File

@@ -0,0 +1,27 @@
package test
import (
"context"
"github.com/jwetzell/showbridge-go/internal/common"
)
func GetContextWithModules(ctx context.Context, modules map[string]common.Module) context.Context {
ctx = context.WithValue(ctx, common.ModulesContextKey, modules)
return ctx
}
func GetContextWithRouter(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, common.RouterContextKey, GetNewTestRouter())
return ctx
}
func GetContextWithSender(ctx context.Context, sender any) context.Context {
ctx = context.WithValue(ctx, common.SenderContextKey, sender)
return ctx
}
func GetContextWithSource(ctx context.Context, source string) context.Context {
ctx = context.WithValue(ctx, common.SourceContextKey, source)
return ctx
}

106
internal/test/module.go Normal file
View File

@@ -0,0 +1,106 @@
package test
import (
"context"
"database/sql"
_ "modernc.org/sqlite"
)
type TestModule struct {
}
func (m *TestModule) Start(ctx context.Context) error {
<-ctx.Done()
return nil
}
func (m *TestModule) Stop() {}
func (m *TestModule) Type() string {
return "test.plain"
}
func (m *TestModule) Id() string {
return "test"
}
func NewTestKVModule(id string) *TestKVModule {
return &TestKVModule{
id: id,
}
}
type TestKVModule struct {
id string
kvData map[string]any
}
func (m *TestKVModule) Start(ctx context.Context) error {
<-ctx.Done()
return nil
}
func (m *TestKVModule) Stop() {}
func (m *TestKVModule) Type() string {
return "test.kv"
}
func (m *TestKVModule) Id() string {
return m.id
}
func (m *TestKVModule) Get(key string) (any, error) {
return key, nil
}
func (m *TestKVModule) Set(key string, value any) error {
if m.kvData == nil {
m.kvData = make(map[string]any)
}
m.kvData[key] = value
return nil
}
func NewTestDBModule(id string) *TestDBModule {
return &TestDBModule{
id: id,
}
}
type TestDBModule struct {
id string
db *sql.DB
}
func (m *TestDBModule) Start(ctx context.Context) error {
<-ctx.Done()
return nil
}
func (m *TestDBModule) Database() *sql.DB {
if m.db == nil {
db, _ := sql.Open("sqlite", ":memory:")
db.Exec(`
CREATE TABLE test (
id INTEGER PRIMARY KEY,
value TEXT
);
INSERT INTO test (id, value) VALUES (1, 'test-1'), (2, 'test-2');
`)
m.db = db
}
return m.db
}
func (m *TestDBModule) Stop() {}
func (m *TestDBModule) Type() string {
return "test.db"
}
func (m *TestDBModule) Id() string {
return m.id
}

View File

@@ -0,0 +1,17 @@
package test
import (
"context"
"github.com/jwetzell/showbridge-go/internal/common"
)
type TestProcessor struct {
}
func (p *TestProcessor) Type() string {
return "test"
}
func (p *TestProcessor) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
return wrappedPayload, nil
}

22
internal/test/router.go Normal file
View File

@@ -0,0 +1,22 @@
package test
import (
"context"
"github.com/jwetzell/showbridge-go/internal/common"
)
type TestRouter struct {
}
func (r *TestRouter) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []common.RouteIOError) {
return false, nil
}
func (r *TestRouter) HandleOutput(ctx context.Context, destinationId string, payload any) error {
return nil
}
func GetNewTestRouter() *TestRouter {
return &TestRouter{}
}

40
internal/test/struct.go Normal file
View File

@@ -0,0 +1,40 @@
package test
type TestStruct struct {
String string
Int int
Float float64
Bool bool
Data any
IntSlice []int
}
func (t TestStruct) GetString() string {
return t.String
}
func (t TestStruct) GetInt() int {
return t.Int
}
func (t TestStruct) GetFloat() float64 {
return t.Float
}
func (t TestStruct) GetBool() bool {
return t.Bool
}
func (t TestStruct) GetData() any {
return t.Data
}
func (t TestStruct) GetIntSlice() []int {
return t.IntSlice
}
func (t TestStruct) Void() {}
func (t TestStruct) MultipleReturnValues() (string, int) {
return t.String, t.Int
}