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

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