add tests to check getting modules from context

This commit is contained in:
Joel Wetzell
2026-03-22 21:00:11 -05:00
parent 13e5d4d85a
commit 70f64e83c7
4 changed files with 172 additions and 26 deletions

View File

@@ -1,6 +1,7 @@
package processor_test package processor_test
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@@ -34,7 +35,12 @@ 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(GetTestContext(t.Context()), payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
},
), payload))
if err != nil { if err != nil {
t.Fatalf("db.query processing failed: %s", err) t.Fatalf("db.query processing failed: %s", err)
} }
@@ -99,7 +105,12 @@ func TestGoodDbQuery(t *testing.T) {
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(GetTestContext(t.Context()), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
},
), test.payload))
if err != nil { if err != nil {
t.Fatalf("db.query processing failed: %s", err) t.Fatalf("db.query processing failed: %s", err)
@@ -114,10 +125,11 @@ func TestGoodDbQuery(t *testing.T) {
func TestBadDbQuery(t *testing.T) { func TestBadDbQuery(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
errorString string wrappedPayloadCtx context.Context
errorString string
}{ }{
{ {
name: "no module param", name: "no module param",
@@ -125,6 +137,9 @@ func TestBadDbQuery(t *testing.T) {
params: map[string]any{ params: map[string]any{
"query": "SELECT sqlite_version();", "query": "SELECT sqlite_version();",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "db.query module error: not found", errorString: "db.query module error: not found",
}, },
{ {
@@ -134,6 +149,9 @@ func TestBadDbQuery(t *testing.T) {
"module": 1, "module": 1,
"query": "SELECT sqlite_version();", "query": "SELECT sqlite_version();",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "db.query module error: not a string", errorString: "db.query module error: not a string",
}, },
{ {
@@ -142,6 +160,9 @@ func TestBadDbQuery(t *testing.T) {
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "db.query query error: not found", errorString: "db.query query error: not found",
}, },
{ {
@@ -151,6 +172,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": 1, "query": 1,
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "db.query query error: not a string", errorString: "db.query query error: not a string",
}, },
{ {
@@ -160,6 +184,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from {{", "query": "select * from {{",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "template: query:1: unclosed action", errorString: "template: query:1: unclosed action",
}, },
{ {
@@ -169,6 +196,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from {{.Data}}", "query": "select * from {{.Data}}",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
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",
}, },
{ {
@@ -178,8 +208,31 @@ func TestBadDbQuery(t *testing.T) {
"module": "test", "module": "test",
"query": "select * from asdf;", "query": "select * from asdf;",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
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",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"query": "select * from test;",
},
wrappedPayloadCtx: t.Context(),
errorString: "db.query wrapped payload has no modules",
},
{
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",
},
} }
for _, test := range tests { for _, test := range tests {
@@ -202,7 +255,7 @@ func TestBadDbQuery(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetTestContext(t.Context()), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload))
if err == nil { if err == nil {
t.Fatalf("db.query expected to fail but got payload: %+v", got) t.Fatalf("db.query expected to fail but got payload: %+v", got)

View File

@@ -1,6 +1,7 @@
package processor_test package processor_test
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@@ -33,7 +34,12 @@ func TestKvGetFromRegistry(t *testing.T) {
payload := "hello" payload := "hello"
expected := "test" expected := "test"
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetTestContext(t.Context()), payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
},
), payload))
if err != nil { if err != nil {
t.Fatalf("kv.get processing failed: %s", err) t.Fatalf("kv.get processing failed: %s", err)
} }
@@ -88,7 +94,12 @@ func TestGoodKvGet(t *testing.T) {
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(GetTestContext(t.Context()), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
},
), test.payload))
if err != nil { if err != nil {
t.Fatalf("kv.get processing failed: %s", err) t.Fatalf("kv.get processing failed: %s", err)
@@ -103,10 +114,11 @@ func TestGoodKvGet(t *testing.T) {
func TestBadKvGet(t *testing.T) { func TestBadKvGet(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
errorString string wrappedPayloadCtx context.Context
errorString string
}{ }{
{ {
name: "no module param", name: "no module param",
@@ -114,6 +126,9 @@ func TestBadKvGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.get module error: not found", errorString: "kv.get module error: not found",
}, },
{ {
@@ -123,6 +138,9 @@ func TestBadKvGet(t *testing.T) {
"module": 1, "module": 1,
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.get module error: not a string", errorString: "kv.get module error: not a string",
}, },
{ {
@@ -131,6 +149,9 @@ func TestBadKvGet(t *testing.T) {
params: map[string]any{ params: map[string]any{
"module": "test", "module": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.get key error: not found", errorString: "kv.get key error: not found",
}, },
{ {
@@ -140,8 +161,31 @@ func TestBadKvGet(t *testing.T) {
"module": "test", "module": "test",
"key": 1, "key": 1,
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.get key error: not a string", errorString: "kv.get key error: not a string",
}, },
{
name: "no modules in context",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"key": "test",
},
wrappedPayloadCtx: t.Context(),
errorString: "kv.get wrapped payload has no modules",
},
{
name: "module not found in context",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"key": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}),
errorString: "kv.get unable to find module with id: test",
},
} }
for _, test := range tests { for _, test := range tests {
@@ -164,7 +208,7 @@ func TestBadKvGet(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetTestContext(t.Context()), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload))
if err == nil { if err == nil {
t.Fatalf("kv.get expected to fail but got payload: %+v", got) t.Fatalf("kv.get expected to fail but got payload: %+v", got)

View File

@@ -1,6 +1,7 @@
package processor_test package processor_test
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@@ -34,7 +35,12 @@ func TestKvSetFromRegistry(t *testing.T) {
payload := "" payload := ""
expected := "" expected := ""
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetTestContext(t.Context()), payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
},
), payload))
if err != nil { if err != nil {
t.Fatalf("kv.set processing failed: %s", err) t.Fatalf("kv.set processing failed: %s", err)
} }
@@ -79,7 +85,12 @@ func TestGoodKvSet(t *testing.T) {
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(GetTestContext(t.Context()), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules(
t.Context(),
map[string]common.Module{
"test": &TestModule{},
},
), test.payload))
if err != nil { if err != nil {
t.Fatalf("kv.set processing failed: %s", err) t.Fatalf("kv.set processing failed: %s", err)
@@ -94,10 +105,11 @@ func TestGoodKvSet(t *testing.T) {
func TestBadKvSet(t *testing.T) { func TestBadKvSet(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
params map[string]any params map[string]any
payload any payload any
errorString string wrappedPayloadCtx context.Context
errorString string
}{ }{
{ {
name: "no module param", name: "no module param",
@@ -106,6 +118,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set module error: not found", errorString: "kv.set module error: not found",
}, },
{ {
@@ -116,6 +131,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set module error: not a string", errorString: "kv.set module error: not a string",
}, },
{ {
@@ -125,6 +143,9 @@ func TestBadKvSet(t *testing.T) {
"module": "test", "module": "test",
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set key error: not found", errorString: "kv.set key error: not found",
}, },
{ {
@@ -135,6 +156,9 @@ func TestBadKvSet(t *testing.T) {
"key": 1, "key": 1,
"value": "test", "value": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set key error: not a string", errorString: "kv.set key error: not a string",
}, },
{ {
@@ -144,6 +168,9 @@ func TestBadKvSet(t *testing.T) {
"module": "test", "module": "test",
"key": "test", "key": "test",
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set value error: not found", errorString: "kv.set value error: not found",
}, },
{ {
@@ -154,8 +181,33 @@ func TestBadKvSet(t *testing.T) {
"key": "test", "key": "test",
"value": 1, "value": 1,
}, },
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set value error: not a string", errorString: "kv.set value error: not a string",
}, },
{
name: "no modules in context",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"key": "test",
"value": "hello",
},
wrappedPayloadCtx: t.Context(),
errorString: "kv.set wrapped payload has no modules",
},
{
name: "module not found in context",
payload: TestStruct{Data: "hello"},
params: map[string]any{
"module": "test",
"key": "test",
"value": "hello",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}),
errorString: "kv.set unable to find module with id: test",
},
} }
for _, test := range tests { for _, test := range tests {
@@ -178,7 +230,7 @@ func TestBadKvSet(t *testing.T) {
return return
} }
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetTestContext(t.Context()), test.payload)) got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.wrappedPayloadCtx, test.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)

View File

@@ -108,11 +108,8 @@ func (m *TestModule) Set(key string, value any) error {
return nil return nil
} }
func GetTestContext(ctx context.Context) context.Context { func GetContextWithModules(ctx context.Context, modules map[string]common.Module) context.Context {
testModule := &TestModule{} ctx = context.WithValue(ctx, common.ModulesContextKey, modules)
ctx = context.WithValue(ctx, common.ModulesContextKey, map[string]common.Module{
"test": testModule,
})
return ctx return ctx
} }