mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
move test implementations to a shared internal package
This commit is contained in:
@@ -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",
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user