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
import (
"context"
"reflect"
"testing"
@@ -34,7 +35,12 @@ func TestDbQueryFromRegistry(t *testing.T) {
payload := "hello"
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 {
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)
}
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 {
t.Fatalf("db.query processing failed: %s", err)
@@ -117,6 +128,7 @@ func TestBadDbQuery(t *testing.T) {
name string
params map[string]any
payload any
wrappedPayloadCtx context.Context
errorString string
}{
{
@@ -125,6 +137,9 @@ func TestBadDbQuery(t *testing.T) {
params: map[string]any{
"query": "SELECT sqlite_version();",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "db.query module error: not found",
},
{
@@ -134,6 +149,9 @@ func TestBadDbQuery(t *testing.T) {
"module": 1,
"query": "SELECT sqlite_version();",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "db.query module error: not a string",
},
{
@@ -142,6 +160,9 @@ func TestBadDbQuery(t *testing.T) {
params: map[string]any{
"module": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "db.query query error: not found",
},
{
@@ -151,6 +172,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test",
"query": 1,
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "db.query query error: not a string",
},
{
@@ -160,6 +184,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test",
"query": "select * from {{",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "template: query:1: unclosed action",
},
{
@@ -169,6 +196,9 @@ func TestBadDbQuery(t *testing.T) {
"module": "test",
"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",
},
{
@@ -178,8 +208,31 @@ func TestBadDbQuery(t *testing.T) {
"module": "test",
"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)",
},
{
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 {
@@ -202,7 +255,7 @@ func TestBadDbQuery(t *testing.T) {
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 {
t.Fatalf("db.query expected to fail but got payload: %+v", got)

View File

@@ -1,6 +1,7 @@
package processor_test
import (
"context"
"reflect"
"testing"
@@ -33,7 +34,12 @@ func TestKvGetFromRegistry(t *testing.T) {
payload := "hello"
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 {
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)
}
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 {
t.Fatalf("kv.get processing failed: %s", err)
@@ -106,6 +117,7 @@ func TestBadKvGet(t *testing.T) {
name string
params map[string]any
payload any
wrappedPayloadCtx context.Context
errorString string
}{
{
@@ -114,6 +126,9 @@ func TestBadKvGet(t *testing.T) {
params: map[string]any{
"key": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.get module error: not found",
},
{
@@ -123,6 +138,9 @@ func TestBadKvGet(t *testing.T) {
"module": 1,
"key": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.get module error: not a string",
},
{
@@ -131,6 +149,9 @@ func TestBadKvGet(t *testing.T) {
params: map[string]any{
"module": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.get key error: not found",
},
{
@@ -140,8 +161,31 @@ func TestBadKvGet(t *testing.T) {
"module": "test",
"key": 1,
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
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 {
@@ -164,7 +208,7 @@ func TestBadKvGet(t *testing.T) {
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 {
t.Fatalf("kv.get expected to fail but got payload: %+v", got)

View File

@@ -1,6 +1,7 @@
package processor_test
import (
"context"
"reflect"
"testing"
@@ -34,7 +35,12 @@ func TestKvSetFromRegistry(t *testing.T) {
payload := ""
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 {
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)
}
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 {
t.Fatalf("kv.set processing failed: %s", err)
@@ -97,6 +108,7 @@ func TestBadKvSet(t *testing.T) {
name string
params map[string]any
payload any
wrappedPayloadCtx context.Context
errorString string
}{
{
@@ -106,6 +118,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test",
"value": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set module error: not found",
},
{
@@ -116,6 +131,9 @@ func TestBadKvSet(t *testing.T) {
"key": "test",
"value": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set module error: not a string",
},
{
@@ -125,6 +143,9 @@ func TestBadKvSet(t *testing.T) {
"module": "test",
"value": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set key error: not found",
},
{
@@ -135,6 +156,9 @@ func TestBadKvSet(t *testing.T) {
"key": 1,
"value": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set key error: not a string",
},
{
@@ -144,6 +168,9 @@ func TestBadKvSet(t *testing.T) {
"module": "test",
"key": "test",
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
errorString: "kv.set value error: not found",
},
{
@@ -154,8 +181,33 @@ func TestBadKvSet(t *testing.T) {
"key": "test",
"value": 1,
},
wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{
"test": &TestModule{},
}),
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 {
@@ -178,7 +230,7 @@ func TestBadKvSet(t *testing.T) {
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 {
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
}
func GetTestContext(ctx context.Context) context.Context {
testModule := &TestModule{}
ctx = context.WithValue(ctx, common.ModulesContextKey, map[string]common.Module{
"test": testModule,
})
func GetContextWithModules(ctx context.Context, modules map[string]common.Module) context.Context {
ctx = context.WithValue(ctx, common.ModulesContextKey, modules)
return ctx
}