add tests for correct module type from processors context

This commit is contained in:
Joel Wetzell
2026-03-22 21:27:20 -05:00
parent 26dc976565
commit 9646c3f2d3
4 changed files with 149 additions and 45 deletions

View File

@@ -38,7 +38,7 @@ func TestDbQueryFromRegistry(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
},
), payload))
if err != nil {
@@ -108,7 +108,7 @@ func TestGoodDbQuery(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
},
), test.payload))
@@ -138,7 +138,7 @@ func TestBadDbQuery(t *testing.T) {
"query": "SELECT sqlite_version();",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
}),
errorString: "db.query module error: not found",
},
@@ -150,7 +150,7 @@ func TestBadDbQuery(t *testing.T) {
"query": "SELECT sqlite_version();",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
}),
errorString: "db.query module error: not a string",
},
@@ -161,7 +161,7 @@ func TestBadDbQuery(t *testing.T) {
"module": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
}),
errorString: "db.query query error: not found",
},
@@ -173,7 +173,7 @@ func TestBadDbQuery(t *testing.T) {
"query": 1,
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
}),
errorString: "db.query query error: not a string",
},
@@ -185,7 +185,7 @@ func TestBadDbQuery(t *testing.T) {
"query": "select * from {{",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
}),
errorString: "template: query:1: unclosed action",
},
@@ -197,7 +197,7 @@ func TestBadDbQuery(t *testing.T) {
"query": "select * from {{.Data}}",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
}),
errorString: "template: query:1:16: executing \"query\" at <.Data>: can't evaluate field Data in type common.WrappedPayload",
},
@@ -209,7 +209,7 @@ func TestBadDbQuery(t *testing.T) {
"query": "select * from asdf;",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestDBModule("test"),
}),
errorString: "db.query error executing query: SQL logic error: no such table: asdf (1)",
},
@@ -233,6 +233,28 @@ func TestBadDbQuery(t *testing.T) {
wrappedPayloadCtx: 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"},
params: map[string]any{
"module": "test",
"query": "select * from test;",
},
wrappedPayloadCtx: 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"},
params: map[string]any{
"module": "test",
"query": "select * from test;",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestKVModule("test"),
}),
errorString: "db.query module with id test is not a DatabaseModule",
},
}
for _, test := range tests {

View File

@@ -37,7 +37,7 @@ func TestKvGetFromRegistry(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
"test": NewTestKVModule("test"),
},
), payload))
if err != nil {
@@ -97,7 +97,7 @@ func TestGoodKvGet(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
"test": NewTestKVModule("test"),
},
), test.payload))
@@ -127,7 +127,7 @@ func TestBadKvGet(t *testing.T) {
"key": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestKVModule("test"),
}),
errorString: "kv.get module error: not found",
},
@@ -139,7 +139,7 @@ func TestBadKvGet(t *testing.T) {
"key": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestKVModule("test"),
}),
errorString: "kv.get module error: not a string",
},
@@ -150,7 +150,7 @@ func TestBadKvGet(t *testing.T) {
"module": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestKVModule("test"),
}),
errorString: "kv.get key error: not found",
},
@@ -162,7 +162,7 @@ func TestBadKvGet(t *testing.T) {
"key": 1,
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": NewTestKVModule("test"),
}),
errorString: "kv.get key error: not a string",
},
@@ -186,6 +186,18 @@ func TestBadKvGet(t *testing.T) {
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}),
errorString: "kv.get unable to find module with id: test",
},
{
name: "module not a kv module",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"key": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"),
}),
errorString: "kv.get module with id test is not a KeyValueModule",
},
}
for _, test := range tests {

View File

@@ -38,7 +38,7 @@ func TestKvSetFromRegistry(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
"test": &TestKVModule{},
},
), payload))
if err != nil {
@@ -88,7 +88,7 @@ func TestGoodKvSet(t *testing.T) {
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
"test": &TestKVModule{},
},
), test.payload))
@@ -119,7 +119,7 @@ func TestBadKvSet(t *testing.T) {
"value": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": &TestKVModule{},
}),
errorString: "kv.set module error: not found",
},
@@ -132,7 +132,7 @@ func TestBadKvSet(t *testing.T) {
"value": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": &TestKVModule{},
}),
errorString: "kv.set module error: not a string",
},
@@ -144,7 +144,7 @@ func TestBadKvSet(t *testing.T) {
"value": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": &TestKVModule{},
}),
errorString: "kv.set key error: not found",
},
@@ -157,7 +157,7 @@ func TestBadKvSet(t *testing.T) {
"value": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": &TestKVModule{},
}),
errorString: "kv.set key error: not a string",
},
@@ -169,7 +169,7 @@ func TestBadKvSet(t *testing.T) {
"key": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": &TestKVModule{},
}),
errorString: "kv.set value error: not found",
},
@@ -182,7 +182,7 @@ func TestBadKvSet(t *testing.T) {
"value": 1,
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
"test": &TestKVModule{},
}),
errorString: "kv.set value error: not a string",
},
@@ -197,6 +197,32 @@ func TestBadKvSet(t *testing.T) {
wrappedPayloadCtx: t.Context(),
errorString: "kv.set wrapped payload has no modules",
},
{
name: "value template syntax error",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"key": "test",
"value": "{{",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{},
}),
errorString: "template: template:1: unclosed action",
},
{
name: "value template execution error",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"key": "test",
"value": "{{.Data}}",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestKVModule{},
}),
errorString: "template: template:1:2: executing \"template\" at <.Data>: can't evaluate field Data in type common.WrappedPayload",
},
{
name: "module not found in context",
payload: TestStruct{Data: "hello"},
@@ -208,6 +234,19 @@ func TestBadKvSet(t *testing.T) {
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}),
errorString: "kv.set unable to find module with id: test",
},
{
name: "module not a kv module",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"key": "test",
"value": "hello",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": NewTestDBModule("test"),
}),
errorString: "kv.set module with id test is not a KeyValueModule",
},
}
for _, test := range tests {

View File

@@ -59,17 +59,60 @@ func (p *TestProcessor) Process(ctx context.Context, wrappedPayload common.Wrapp
return wrappedPayload, nil
}
type TestModule struct {
kvData map[string]any
db *sql.DB
func NewTestKVModule(id string) *TestKVModule {
return &TestKVModule{
id: id,
}
}
func (m *TestModule) Start(ctx context.Context) error {
type TestKVModule struct {
id string
kvData map[string]any
}
func (m *TestKVModule) Start(ctx context.Context) error {
<-ctx.Done()
return nil
}
func (m *TestModule) Database() *sql.DB {
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:")
@@ -86,26 +129,14 @@ func (m *TestModule) Database() *sql.DB {
return m.db
}
func (m *TestModule) Stop() {}
func (m *TestDBModule) Stop() {}
func (m *TestModule) Type() string {
return "module.test"
func (m *TestDBModule) Type() string {
return "module.test.db"
}
func (m *TestModule) Id() string {
return "test"
}
func (m *TestModule) Get(key string) (any, error) {
return key, nil
}
func (m *TestModule) Set(key string, value any) error {
if m.kvData == nil {
m.kvData = make(map[string]any)
}
m.kvData[key] = value
return nil
func (m *TestDBModule) Id() string {
return m.id
}
func GetContextWithModules(ctx context.Context, modules map[string]common.Module) context.Context {