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"
@@ -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)
@@ -103,10 +114,11 @@ func TestGoodKvGet(t *testing.T) {
func TestBadKvGet(t *testing.T) {
tests := []struct {
name string
params map[string]any
payload any
errorString string
name string
params map[string]any
payload any
wrappedPayloadCtx context.Context
errorString string
}{
{
name: "no module param",
@@ -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)