From 70f64e83c70144ee2d8fbe07683e0c272d836c89 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sun, 22 Mar 2026 21:00:11 -0500 Subject: [PATCH] add tests to check getting modules from context --- internal/processor/test/db-query_test.go | 67 ++++++++++++++++++++--- internal/processor/test/kv-get_test.go | 58 +++++++++++++++++--- internal/processor/test/kv-set_test.go | 66 +++++++++++++++++++--- internal/processor/test/processor_test.go | 7 +-- 4 files changed, 172 insertions(+), 26 deletions(-) diff --git a/internal/processor/test/db-query_test.go b/internal/processor/test/db-query_test.go index 7bf4c10..4503217 100644 --- a/internal/processor/test/db-query_test.go +++ b/internal/processor/test/db-query_test.go @@ -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) @@ -114,10 +125,11 @@ func TestGoodDbQuery(t *testing.T) { func TestBadDbQuery(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", @@ -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) diff --git a/internal/processor/test/kv-get_test.go b/internal/processor/test/kv-get_test.go index 15a9ca9..88591e2 100644 --- a/internal/processor/test/kv-get_test.go +++ b/internal/processor/test/kv-get_test.go @@ -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) diff --git a/internal/processor/test/kv-set_test.go b/internal/processor/test/kv-set_test.go index f47b68b..b422731 100644 --- a/internal/processor/test/kv-set_test.go +++ b/internal/processor/test/kv-set_test.go @@ -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) @@ -94,10 +105,11 @@ func TestGoodKvSet(t *testing.T) { func TestBadKvSet(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", @@ -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) diff --git a/internal/processor/test/processor_test.go b/internal/processor/test/processor_test.go index 978ef4c..4ade298 100644 --- a/internal/processor/test/processor_test.go +++ b/internal/processor/test/processor_test.go @@ -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 }