diff --git a/internal/module/test/module_test.go b/internal/module/test/module_test.go index 5ea4664..2eee6ab 100644 --- a/internal/module/test/module_test.go +++ b/internal/module/test/module_test.go @@ -1,32 +1,14 @@ package module_test import ( - "context" "testing" "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/module" + "github.com/jwetzell/showbridge-go/internal/test" ) -type TestModule struct { -} - -func (m *TestModule) Start(ctx context.Context) error { - <-ctx.Done() - return nil -} - -func (m *TestModule) Stop() {} - -func (m *TestModule) Type() string { - return "module.test" -} - -func (m *TestModule) Id() string { - return "test" -} - func TestModuleBadRegistrationNoType(t *testing.T) { defer func() { if r := recover(); r == nil { @@ -37,7 +19,7 @@ func TestModuleBadRegistrationNoType(t *testing.T) { module.RegisterModule(module.ModuleRegistration{ Type: "", New: func(config config.ModuleConfig) (common.Module, error) { - return &TestModule{}, nil + return &test.TestModule{}, nil }, }) } @@ -65,14 +47,14 @@ func TestModuleBadRegistrationExistingType(t *testing.T) { module.RegisterModule(module.ModuleRegistration{ Type: "module.test", New: func(config config.ModuleConfig) (common.Module, error) { - return &TestModule{}, nil + return &test.TestModule{}, nil }, }) module.RegisterModule(module.ModuleRegistration{ Type: "module.test", New: func(config config.ModuleConfig) (common.Module, error) { - return &TestModule{}, nil + return &test.TestModule{}, nil }, }) } diff --git a/internal/processor/test/db-query_test.go b/internal/processor/test/db-query_test.go index b61c9bb..87a0e32 100644 --- a/internal/processor/test/db-query_test.go +++ b/internal/processor/test/db-query_test.go @@ -8,6 +8,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" _ "modernc.org/sqlite" ) @@ -35,10 +36,10 @@ func TestDbQueryFromRegistry(t *testing.T) { payload := "hello" expected := map[string]any{"sqlite_version()": "3.51.3"} - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + "test": test.NewTestDBModule("test"), }, ), payload)) if err != nil { @@ -52,7 +53,7 @@ func TestDbQueryFromRegistry(t *testing.T) { func TestGoodDbQuery(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any @@ -98,8 +99,8 @@ func TestGoodDbQuery(t *testing.T) { expected: nil, }, } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["db.query"] if !ok { t.Fatalf("db.query processor not registered") @@ -107,26 +108,26 @@ func TestGoodDbQuery(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "db.query", - Params: test.params, + Params: testCase.params, }) if err != nil { t.Fatalf("db.query failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + "test": test.NewTestDBModule("test"), }, - ), test.payload)) + ), testCase.payload)) if err != nil { t.Fatalf("db.query processing failed: %s", err) } - if !reflect.DeepEqual(got.Payload, test.expected) { - t.Fatalf("db.query got payload: %+v, expected %+v", got.Payload, test.expected) + if !reflect.DeepEqual(got.Payload, testCase.expected) { + t.Fatalf("db.query got payload: %+v, expected %+v", got.Payload, testCase.expected) } }) } @@ -142,89 +143,89 @@ func TestBadDbQuery(t *testing.T) { }{ { name: "no module param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "query": "SELECT sqlite_version();", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "db.query module error: not found", }, { name: "non string module", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": 1, "query": "SELECT sqlite_version();", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "db.query module error: not a string", }, { name: "no query param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "db.query query error: not found", }, { name: "non string query", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "query": 1, }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "db.query query error: not a string", }, { name: "query template syntax error", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "query": "select * from {{", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "template: query:1: unclosed action", }, { name: "query template error", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "query": "select * from {{.Data}}", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "template: query:1:16: executing \"query\" at <.Data>: can't evaluate field Data in type common.WrappedPayload", }, { name: "query error", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "query": "select * from asdf;", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "db.query error executing query: SQL logic error: no such table: asdf (1)", }, { name: "no modules in context", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "query": "select * from test;", @@ -234,33 +235,33 @@ func TestBadDbQuery(t *testing.T) { }, { name: "module not found in context", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "query": "select * from test;", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}), + wrappedPayloadCtx: test.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"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "query": "select * from test;", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}), + wrappedPayloadCtx: test.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"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "query": "select * from test;", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestKVModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestKVModule("test"), }), errorString: "db.query module with id test is not a DatabaseModule", }, diff --git a/internal/processor/test/filter-expr_test.go b/internal/processor/test/filter-expr_test.go index f76036d..7512893 100644 --- a/internal/processor/test/filter-expr_test.go +++ b/internal/processor/test/filter-expr_test.go @@ -6,6 +6,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) func TestFilterExprFromRegistry(t *testing.T) { @@ -30,7 +31,7 @@ func TestFilterExprFromRegistry(t *testing.T) { } func TestGoodFilterExpr(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any @@ -41,7 +42,7 @@ func TestGoodFilterExpr(t *testing.T) { params: map[string]any{ "expression": "Payload.Int > 0", }, - payload: TestStruct{ + payload: test.TestStruct{ Int: 1, }, match: true, @@ -51,7 +52,7 @@ func TestGoodFilterExpr(t *testing.T) { params: map[string]any{ "expression": "Payload.String == 'hello'", }, - payload: TestStruct{ + payload: test.TestStruct{ String: "hello", }, match: true, @@ -61,15 +62,15 @@ func TestGoodFilterExpr(t *testing.T) { params: map[string]any{ "expression": "Payload.Int > 0", }, - payload: TestStruct{ + payload: test.TestStruct{ Int: 0, }, match: false, }, } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["filter.expr"] if !ok { t.Fatalf("filter.expr processor not registered") @@ -77,22 +78,22 @@ func TestGoodFilterExpr(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "filter.expr", - Params: test.params, + Params: testCase.params, }) if err != nil { t.Fatalf("filter.expr failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), testCase.payload)) if err != nil { t.Fatalf("filter.expr processing failed: %s", err) } //TODO(jwetzell): work out better way to compare the any/any - if got.End != !test.match { - t.Fatalf("filter.expr did fitler properly %+v (%T), expected %+v (%T)", got, got, test.match, test.match) + if got.End != !testCase.match { + t.Fatalf("filter.expr did fitler properly %+v (%T), expected %+v (%T)", got, got, testCase.match, testCase.match) } }) } @@ -110,7 +111,7 @@ func TestBadFilterExpr(t *testing.T) { params: map[string]any{ // no expression parameter }, - payload: TestStruct{}, + payload: test.TestStruct{}, errorString: "filter.expr expression error: not found", }, { @@ -118,7 +119,7 @@ func TestBadFilterExpr(t *testing.T) { params: map[string]any{ "expression": 12345, }, - payload: TestStruct{}, + payload: test.TestStruct{}, errorString: "filter.expr expression error: not a string", }, { @@ -126,7 +127,7 @@ func TestBadFilterExpr(t *testing.T) { params: map[string]any{ "expression": "foo +", }, - payload: TestStruct{}, + payload: test.TestStruct{}, errorString: "unexpected token EOF (1:5)\n | foo +\n | ....^", }, { diff --git a/internal/processor/test/kv-get_test.go b/internal/processor/test/kv-get_test.go index 9aae238..1d57c6b 100644 --- a/internal/processor/test/kv-get_test.go +++ b/internal/processor/test/kv-get_test.go @@ -8,6 +8,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) func TestKvGetFromRegistry(t *testing.T) { @@ -34,10 +35,10 @@ func TestKvGetFromRegistry(t *testing.T) { payload := "hello" expected := "test" - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( t.Context(), map[string]common.Module{ - "test": NewTestKVModule("test"), + "test": test.NewTestKVModule("test"), }, ), payload)) if err != nil { @@ -51,7 +52,7 @@ func TestKvGetFromRegistry(t *testing.T) { func TestGoodKvGet(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any @@ -78,8 +79,8 @@ func TestGoodKvGet(t *testing.T) { expected: "test", }, } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["kv.get"] if !ok { t.Fatalf("kv.get processor not registered") @@ -87,26 +88,26 @@ func TestGoodKvGet(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "kv.get", - Params: test.params, + Params: testCase.params, }) if err != nil { t.Fatalf("kv.get failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( t.Context(), map[string]common.Module{ - "test": NewTestKVModule("test"), + "test": test.NewTestKVModule("test"), }, - ), test.payload)) + ), testCase.payload)) if err != nil { t.Fatalf("kv.get processing failed: %s", err) } - if !reflect.DeepEqual(got.Payload, test.expected) { - t.Fatalf("kv.get got payload: %+v, expected %+v", got.Payload, test.expected) + if !reflect.DeepEqual(got.Payload, testCase.expected) { + t.Fatalf("kv.get got payload: %+v, expected %+v", got.Payload, testCase.expected) } }) } @@ -122,53 +123,53 @@ func TestBadKvGet(t *testing.T) { }{ { name: "no module param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "key": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestKVModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestKVModule("test"), }), errorString: "kv.get module error: not found", }, { name: "non string module", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": 1, "key": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestKVModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestKVModule("test"), }), errorString: "kv.get module error: not a string", }, { name: "no key param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestKVModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestKVModule("test"), }), errorString: "kv.get key error: not found", }, { name: "non string key", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": 1, }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestKVModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestKVModule("test"), }), errorString: "kv.get key error: not a string", }, { name: "no modules in context", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", @@ -178,23 +179,23 @@ func TestBadKvGet(t *testing.T) { }, { name: "module not found in context", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}), + wrappedPayloadCtx: test.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"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "kv.get module with id test is not a KeyValueModule", }, diff --git a/internal/processor/test/kv-set_test.go b/internal/processor/test/kv-set_test.go index 2d42d1d..62677ea 100644 --- a/internal/processor/test/kv-set_test.go +++ b/internal/processor/test/kv-set_test.go @@ -8,6 +8,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) func TestKvSetFromRegistry(t *testing.T) { @@ -35,10 +36,10 @@ func TestKvSetFromRegistry(t *testing.T) { payload := "" expected := "" - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + "test": &test.TestKVModule{}, }, ), payload)) if err != nil { @@ -52,7 +53,7 @@ func TestKvSetFromRegistry(t *testing.T) { func TestGoodKvSet(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any @@ -69,8 +70,8 @@ func TestGoodKvSet(t *testing.T) { expected: "", }, } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["kv.set"] if !ok { t.Fatalf("kv.set processor not registered") @@ -78,33 +79,33 @@ func TestGoodKvSet(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "kv.set", - Params: test.params, + Params: testCase.params, }) if err != nil { t.Fatalf("kv.set failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithModules( + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithModules( t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + "test": &test.TestKVModule{}, }, - ), test.payload)) + ), testCase.payload)) if err != nil { t.Fatalf("kv.set processing failed: %s", err) } - if !reflect.DeepEqual(got.Payload, test.expected) { - t.Fatalf("kv.set got payload: %+v, expected %+v", got.Payload, test.expected) + if !reflect.DeepEqual(got.Payload, testCase.expected) { + t.Fatalf("kv.set got payload: %+v, expected %+v", got.Payload, testCase.expected) } }) } } func TestBadKvSet(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any @@ -113,82 +114,82 @@ func TestBadKvSet(t *testing.T) { }{ { name: "no module param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "key": "test", "value": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": &test.TestKVModule{}, }), errorString: "kv.set module error: not found", }, { name: "non string module", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": 1, "key": "test", "value": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": &test.TestKVModule{}, }), errorString: "kv.set module error: not a string", }, { name: "no key param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "value": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": &test.TestKVModule{}, }), errorString: "kv.set key error: not found", }, { name: "non string key", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": 1, "value": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": &test.TestKVModule{}, }), errorString: "kv.set key error: not a string", }, { name: "no value param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": &test.TestKVModule{}, }), errorString: "kv.set value error: not found", }, { name: "non string value", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", "value": 1, }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": &test.TestKVModule{}, }), errorString: "kv.set value error: not a string", }, { name: "no modules in context", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", @@ -199,58 +200,58 @@ func TestBadKvSet(t *testing.T) { }, { name: "value template syntax error", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", "value": "{{", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": &test.TestKVModule{}, }), errorString: "template: template:1: unclosed action", }, { name: "value template execution error", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", "value": "{{.Data}}", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": &TestKVModule{}, + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": &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"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", "value": "hello", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{}), + wrappedPayloadCtx: test.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"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "module": "test", "key": "test", "value": "hello", }, - wrappedPayloadCtx: GetContextWithModules(t.Context(), map[string]common.Module{ - "test": NewTestDBModule("test"), + wrappedPayloadCtx: test.GetContextWithModules(t.Context(), map[string]common.Module{ + "test": test.NewTestDBModule("test"), }), errorString: "kv.set module with id test is not a KeyValueModule", }, } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["kv.set"] if !ok { @@ -259,24 +260,24 @@ func TestBadKvSet(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "kv.set", - Params: test.params, + Params: testCase.params, }) if err != nil { - if test.errorString != err.Error() { - t.Fatalf("kv.set got error '%s', expected '%s'", err.Error(), test.errorString) + if testCase.errorString != err.Error() { + t.Fatalf("kv.set got error '%s', expected '%s'", err.Error(), testCase.errorString) } return } - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload)) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload)) if err == nil { t.Fatalf("kv.set expected to fail but got payload: %+v", got) } - if err.Error() != test.errorString { - t.Fatalf("kv.set got error '%s', expected '%s'", err.Error(), test.errorString) + if err.Error() != testCase.errorString { + t.Fatalf("kv.set got error '%s', expected '%s'", err.Error(), testCase.errorString) } }) } diff --git a/internal/processor/test/processor_test.go b/internal/processor/test/processor_test.go index c392c3e..5a383cc 100644 --- a/internal/processor/test/processor_test.go +++ b/internal/processor/test/processor_test.go @@ -1,169 +1,13 @@ package processor_test import ( - "context" - "database/sql" "testing" - "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) -type TestStruct struct { - String string - Int int - Float float64 - Bool bool - Data any - IntSlice []int -} - -func (t TestStruct) GetString() string { - return t.String -} - -func (t TestStruct) GetInt() int { - return t.Int -} - -func (t TestStruct) GetFloat() float64 { - return t.Float -} - -func (t TestStruct) GetBool() bool { - return t.Bool -} - -func (t TestStruct) GetData() any { - return t.Data -} - -func (t TestStruct) GetIntSlice() []int { - return t.IntSlice -} - -func (t TestStruct) Void() {} - -func (t TestStruct) MultipleReturnValues() (string, int) { - return t.String, t.Int -} - -type TestProcessor struct { -} - -func (p *TestProcessor) Type() string { - return "test" -} -func (p *TestProcessor) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - return wrappedPayload, nil -} - -func NewTestKVModule(id string) *TestKVModule { - return &TestKVModule{ - id: id, - } -} - -type TestKVModule struct { - id string - kvData map[string]any -} - -func (m *TestKVModule) Start(ctx context.Context) error { - <-ctx.Done() - return nil -} - -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:") - - db.Exec(` - CREATE TABLE test ( - id INTEGER PRIMARY KEY, - value TEXT - ); - INSERT INTO test (id, value) VALUES (1, 'test-1'), (2, 'test-2'); - - `) - m.db = db - } - return m.db -} - -func (m *TestDBModule) Stop() {} - -func (m *TestDBModule) Type() string { - return "module.test.db" -} - -func (m *TestDBModule) Id() string { - return m.id -} - -func GetNewTestRouter() *TestRouter { - return &TestRouter{} -} - -type TestRouter struct { -} - -func (r *TestRouter) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []common.RouteIOError) { - return false, nil -} - -func (r *TestRouter) HandleOutput(ctx context.Context, destinationId string, payload any) error { - return nil -} - -func GetContextWithModules(ctx context.Context, modules map[string]common.Module) context.Context { - ctx = context.WithValue(ctx, common.ModulesContextKey, modules) - return ctx -} - -func GetContextWithRouter(ctx context.Context) context.Context { - ctx = context.WithValue(ctx, common.RouterContextKey, GetNewTestRouter()) - return ctx -} - func TestProcessorBadRegistrationNoType(t *testing.T) { defer func() { if r := recover(); r == nil { @@ -174,7 +18,7 @@ func TestProcessorBadRegistrationNoType(t *testing.T) { processor.RegisterProcessor(processor.ProcessorRegistration{ Type: "", New: func(config config.ProcessorConfig) (processor.Processor, error) { - return &TestProcessor{}, nil + return &test.TestProcessor{}, nil }, }) } @@ -202,7 +46,7 @@ func TestProcessorBadRegistrationExistingType(t *testing.T) { processor.RegisterProcessor(processor.ProcessorRegistration{ Type: "string.create", New: func(config config.ProcessorConfig) (processor.Processor, error) { - return &TestProcessor{}, nil + return &test.TestProcessor{}, nil }, }) } diff --git a/internal/processor/test/router-input_test.go b/internal/processor/test/router-input_test.go index bee6e1d..21ff410 100644 --- a/internal/processor/test/router-input_test.go +++ b/internal/processor/test/router-input_test.go @@ -8,6 +8,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) func TestRouterOutputFromRegistry(t *testing.T) { @@ -34,7 +35,7 @@ func TestRouterOutputFromRegistry(t *testing.T) { payload := "test" expected := "test" - got, err := processorInstance.Process(GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload)) + got, err := processorInstance.Process(test.GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("router.output processing failed: %s", err) } @@ -46,15 +47,15 @@ func TestRouterOutputFromRegistry(t *testing.T) { func TestGoodRouterOutput(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any expected any }{} - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["router.output"] if !ok { @@ -63,27 +64,27 @@ func TestGoodRouterOutput(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "router.output", - Params: test.params, + Params: testCase.params, }) if err != nil { t.Fatalf("router.output failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithRouter(t.Context()), test.payload)) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithRouter(t.Context()), testCase.payload)) if err != nil { t.Fatalf("router.output processing failed: %s", err) } - if !reflect.DeepEqual(got.Payload, test.expected) { - t.Fatalf("router.output got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, test.expected, test.expected) + if !reflect.DeepEqual(got.Payload, testCase.expected) { + t.Fatalf("router.output got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, testCase.expected, testCase.expected) } }) } } func TestBadRouterOutput(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any @@ -95,7 +96,7 @@ func TestBadRouterOutput(t *testing.T) { name: "no module param", params: map[string]any{}, payload: "test", - processCtx: GetContextWithRouter(t.Context()), + processCtx: test.GetContextWithRouter(t.Context()), wrappedPayloadCtx: t.Context(), errorString: "router.output module error: not found", }, @@ -105,7 +106,7 @@ func TestBadRouterOutput(t *testing.T) { "module": 123, }, payload: "test", - processCtx: GetContextWithRouter(t.Context()), + processCtx: test.GetContextWithRouter(t.Context()), wrappedPayloadCtx: t.Context(), errorString: "router.output module error: not a string", }, @@ -121,8 +122,8 @@ func TestBadRouterOutput(t *testing.T) { }, } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["router.output"] if !ok { @@ -131,24 +132,24 @@ func TestBadRouterOutput(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "router.output", - Params: test.params, + Params: testCase.params, }) if err != nil { - if test.errorString != err.Error() { - t.Fatalf("router.output got error '%s', expected '%s'", err.Error(), test.errorString) + if testCase.errorString != err.Error() { + t.Fatalf("router.output got error '%s', expected '%s'", err.Error(), testCase.errorString) } return } - got, err := processorInstance.Process(test.processCtx, common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload)) + got, err := processorInstance.Process(testCase.processCtx, common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload)) if err == nil { t.Fatalf("router.output expected to fail but succeeded, got: %v", got) } - if err.Error() != test.errorString { - t.Fatalf("router.output got error '%s', expected '%s'", err.Error(), test.errorString) + if err.Error() != testCase.errorString { + t.Fatalf("router.output got error '%s', expected '%s'", err.Error(), testCase.errorString) } }) } diff --git a/internal/processor/test/router-output_test.go b/internal/processor/test/router-output_test.go index f2bb95b..13eacb0 100644 --- a/internal/processor/test/router-output_test.go +++ b/internal/processor/test/router-output_test.go @@ -8,6 +8,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) func TestRouterInputFromRegistry(t *testing.T) { @@ -34,7 +35,7 @@ func TestRouterInputFromRegistry(t *testing.T) { payload := "test" expected := "test" - got, err := processorInstance.Process(GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload)) + got, err := processorInstance.Process(test.GetContextWithRouter(t.Context()), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("router.input processing failed: %s", err) } @@ -46,15 +47,15 @@ func TestRouterInputFromRegistry(t *testing.T) { func TestGoodRouterInput(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any expected any }{} - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["router.input"] if !ok { @@ -63,27 +64,27 @@ func TestGoodRouterInput(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "router.input", - Params: test.params, + Params: testCase.params, }) if err != nil { t.Fatalf("router.input failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(GetContextWithRouter(t.Context()), test.payload)) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(test.GetContextWithRouter(t.Context()), testCase.payload)) if err != nil { t.Fatalf("router.input processing failed: %s", err) } - if !reflect.DeepEqual(got.Payload, test.expected) { - t.Fatalf("router.input got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, test.expected, test.expected) + if !reflect.DeepEqual(got.Payload, testCase.expected) { + t.Fatalf("router.input got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, testCase.expected, testCase.expected) } }) } } func TestBadRouterInput(t *testing.T) { - tests := []struct { + testCases := []struct { name string params map[string]any payload any @@ -95,7 +96,7 @@ func TestBadRouterInput(t *testing.T) { name: "no source param", params: map[string]any{}, payload: "test", - processCtx: GetContextWithRouter(t.Context()), + processCtx: test.GetContextWithRouter(t.Context()), wrappedPayloadCtx: t.Context(), errorString: "router.input source error: not found", }, @@ -105,7 +106,7 @@ func TestBadRouterInput(t *testing.T) { "source": 123, }, payload: "test", - processCtx: GetContextWithRouter(t.Context()), + processCtx: test.GetContextWithRouter(t.Context()), wrappedPayloadCtx: t.Context(), errorString: "router.input source error: not a string", }, @@ -121,8 +122,8 @@ func TestBadRouterInput(t *testing.T) { }, } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { registration, ok := processor.ProcessorRegistry["router.input"] if !ok { @@ -131,24 +132,24 @@ func TestBadRouterInput(t *testing.T) { processorInstance, err := registration.New(config.ProcessorConfig{ Type: "router.input", - Params: test.params, + Params: testCase.params, }) if err != nil { - if test.errorString != err.Error() { - t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), test.errorString) + if testCase.errorString != err.Error() { + t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), testCase.errorString) } return } - got, err := processorInstance.Process(test.processCtx, common.GetWrappedPayload(test.wrappedPayloadCtx, test.payload)) + got, err := processorInstance.Process(testCase.processCtx, common.GetWrappedPayload(testCase.wrappedPayloadCtx, testCase.payload)) if err == nil { t.Fatalf("router.input expected to fail but succeeded, got: %v", got) } - if err.Error() != test.errorString { - t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), test.errorString) + if err.Error() != testCase.errorString { + t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), testCase.errorString) } }) } diff --git a/internal/processor/test/string-create_test.go b/internal/processor/test/string-create_test.go index 9fbf824..213a6ad 100644 --- a/internal/processor/test/string-create_test.go +++ b/internal/processor/test/string-create_test.go @@ -6,6 +6,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) func TestStringCreateFromRegistry(t *testing.T) { @@ -70,13 +71,13 @@ func TestGoodStringCreate(t *testing.T) { { name: "struct payload - field", params: map[string]any{"template": "{{.Payload.Data}}"}, - payload: TestStruct{Data: "test"}, + payload: test.TestStruct{Data: "test"}, expected: "test", }, { name: "struct payload - method", params: map[string]any{"template": "{{.Payload.GetData}}"}, - payload: TestStruct{Data: "test"}, + payload: test.TestStruct{Data: "test"}, expected: "test", }, } diff --git a/internal/processor/test/struct-field-get_test.go b/internal/processor/test/struct-field-get_test.go index e26586a..6104285 100644 --- a/internal/processor/test/struct-field-get_test.go +++ b/internal/processor/test/struct-field-get_test.go @@ -7,6 +7,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) func TestStructFieldGetFromRegistry(t *testing.T) { @@ -29,7 +30,7 @@ func TestStructFieldGetFromRegistry(t *testing.T) { t.Fatalf("struct.field.get processor has wrong type: %s", processorInstance.Type()) } - payload := TestStruct{Data: "hello"} + payload := test.TestStruct{Data: "hello"} expected := "hello" got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) @@ -53,31 +54,31 @@ func TestGoodStructFieldGet(t *testing.T) { { name: "string field", params: map[string]any{"name": "String"}, - payload: TestStruct{String: "hello"}, + payload: test.TestStruct{String: "hello"}, expected: "hello", }, { name: "int field", params: map[string]any{"name": "Int"}, - payload: TestStruct{Int: 42}, + payload: test.TestStruct{Int: 42}, expected: 42, }, { name: "float field", params: map[string]any{"name": "Float"}, - payload: TestStruct{Float: 3.14}, + payload: test.TestStruct{Float: 3.14}, expected: 3.14, }, { name: "bool field", params: map[string]any{"name": "Bool"}, - payload: TestStruct{Bool: true}, + payload: test.TestStruct{Bool: true}, expected: true, }, { name: "pointer to struct payload", params: map[string]any{"name": "Data"}, - payload: &TestStruct{Data: "hello"}, + payload: &test.TestStruct{Data: "hello"}, expected: "hello", }, { @@ -85,7 +86,7 @@ func TestGoodStructFieldGet(t *testing.T) { params: map[string]any{ "name": "IntSlice", }, - payload: TestStruct{IntSlice: []int{1, 2, 3}}, + payload: test.TestStruct{IntSlice: []int{1, 2, 3}}, expected: []int{1, 2, 3}, }, } @@ -128,13 +129,13 @@ func TestBadStructFieldGet(t *testing.T) { }{ { name: "no name param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{}, errorString: "struct.field.get name error: not found", }, { name: "non string name", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "name": 1, }, @@ -142,7 +143,7 @@ func TestBadStructFieldGet(t *testing.T) { }, { name: "missing field", - payload: TestStruct{String: "hello"}, + payload: test.TestStruct{String: "hello"}, params: map[string]any{ "name": "NonExistentField", }, diff --git a/internal/processor/test/struct-method-get_test.go b/internal/processor/test/struct-method-get_test.go index b2f7fd7..3bf6728 100644 --- a/internal/processor/test/struct-method-get_test.go +++ b/internal/processor/test/struct-method-get_test.go @@ -7,6 +7,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" + "github.com/jwetzell/showbridge-go/internal/test" ) func TestStructMethodGetFromRegistry(t *testing.T) { @@ -29,7 +30,7 @@ func TestStructMethodGetFromRegistry(t *testing.T) { t.Fatalf("struct.method.get processor has wrong type: %s", processorInstance.Type()) } - payload := TestStruct{Data: "hello"} + payload := test.TestStruct{Data: "hello"} expected := "hello" got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) @@ -53,25 +54,25 @@ func TestGoodStructMethodGet(t *testing.T) { { name: "string field", params: map[string]any{"name": "GetString"}, - payload: TestStruct{String: "hello"}, + payload: test.TestStruct{String: "hello"}, expected: "hello", }, { name: "int field", params: map[string]any{"name": "GetInt"}, - payload: TestStruct{Int: 42}, + payload: test.TestStruct{Int: 42}, expected: 42, }, { name: "float field", params: map[string]any{"name": "GetFloat"}, - payload: TestStruct{Float: 3.14}, + payload: test.TestStruct{Float: 3.14}, expected: 3.14, }, { name: "bool field", params: map[string]any{"name": "GetBool"}, - payload: TestStruct{Bool: true}, + payload: test.TestStruct{Bool: true}, expected: true, }, { @@ -79,7 +80,7 @@ func TestGoodStructMethodGet(t *testing.T) { params: map[string]any{ "name": "GetData", }, - payload: TestStruct{Data: []string{"hello"}}, + payload: test.TestStruct{Data: []string{"hello"}}, expected: []string{"hello"}, }, { @@ -87,7 +88,7 @@ func TestGoodStructMethodGet(t *testing.T) { params: map[string]any{ "name": "Void", }, - payload: TestStruct{}, + payload: test.TestStruct{}, expected: nil, }, { @@ -95,7 +96,7 @@ func TestGoodStructMethodGet(t *testing.T) { params: map[string]any{ "name": "GetData", }, - payload: &TestStruct{Data: "hello"}, + payload: &test.TestStruct{Data: "hello"}, expected: "hello", }, { @@ -103,7 +104,7 @@ func TestGoodStructMethodGet(t *testing.T) { params: map[string]any{ "name": "GetIntSlice", }, - payload: TestStruct{IntSlice: []int{1, 2, 3}}, + payload: test.TestStruct{IntSlice: []int{1, 2, 3}}, expected: []int{1, 2, 3}, }, { @@ -111,7 +112,7 @@ func TestGoodStructMethodGet(t *testing.T) { params: map[string]any{ "name": "MultipleReturnValues", }, - payload: TestStruct{String: "hello", Int: 42}, + payload: test.TestStruct{String: "hello", Int: 42}, expected: []any{"hello", 42}, }, } @@ -153,13 +154,13 @@ func TestBadStructMethodGet(t *testing.T) { }{ { name: "no name param", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{}, errorString: "struct.method.get name error: not found", }, { name: "non string name", - payload: TestStruct{Data: "hello"}, + payload: test.TestStruct{Data: "hello"}, params: map[string]any{ "name": 1, }, @@ -167,7 +168,7 @@ func TestBadStructMethodGet(t *testing.T) { }, { name: "missing method", - payload: TestStruct{String: "hello"}, + payload: test.TestStruct{String: "hello"}, params: map[string]any{ "name": "NonExistentMethod", }, diff --git a/internal/test/context.go b/internal/test/context.go new file mode 100644 index 0000000..adbb469 --- /dev/null +++ b/internal/test/context.go @@ -0,0 +1,27 @@ +package test + +import ( + "context" + + "github.com/jwetzell/showbridge-go/internal/common" +) + +func GetContextWithModules(ctx context.Context, modules map[string]common.Module) context.Context { + ctx = context.WithValue(ctx, common.ModulesContextKey, modules) + return ctx +} + +func GetContextWithRouter(ctx context.Context) context.Context { + ctx = context.WithValue(ctx, common.RouterContextKey, GetNewTestRouter()) + return ctx +} + +func GetContextWithSender(ctx context.Context, sender any) context.Context { + ctx = context.WithValue(ctx, common.SenderContextKey, sender) + return ctx +} + +func GetContextWithSource(ctx context.Context, source string) context.Context { + ctx = context.WithValue(ctx, common.SourceContextKey, source) + return ctx +} diff --git a/internal/test/module.go b/internal/test/module.go new file mode 100644 index 0000000..8c63727 --- /dev/null +++ b/internal/test/module.go @@ -0,0 +1,106 @@ +package test + +import ( + "context" + "database/sql" + + _ "modernc.org/sqlite" +) + +type TestModule struct { +} + +func (m *TestModule) Start(ctx context.Context) error { + <-ctx.Done() + return nil +} + +func (m *TestModule) Stop() {} + +func (m *TestModule) Type() string { + return "test.plain" +} + +func (m *TestModule) Id() string { + return "test" +} + +func NewTestKVModule(id string) *TestKVModule { + return &TestKVModule{ + id: id, + } +} + +type TestKVModule struct { + id string + kvData map[string]any +} + +func (m *TestKVModule) Start(ctx context.Context) error { + <-ctx.Done() + return nil +} + +func (m *TestKVModule) Stop() {} + +func (m *TestKVModule) Type() string { + return "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:") + + db.Exec(` + CREATE TABLE test ( + id INTEGER PRIMARY KEY, + value TEXT + ); + INSERT INTO test (id, value) VALUES (1, 'test-1'), (2, 'test-2'); + + `) + m.db = db + } + return m.db +} + +func (m *TestDBModule) Stop() {} + +func (m *TestDBModule) Type() string { + return "test.db" +} + +func (m *TestDBModule) Id() string { + return m.id +} diff --git a/internal/test/processor.go b/internal/test/processor.go new file mode 100644 index 0000000..69aa146 --- /dev/null +++ b/internal/test/processor.go @@ -0,0 +1,17 @@ +package test + +import ( + "context" + + "github.com/jwetzell/showbridge-go/internal/common" +) + +type TestProcessor struct { +} + +func (p *TestProcessor) Type() string { + return "test" +} +func (p *TestProcessor) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + return wrappedPayload, nil +} diff --git a/internal/test/router.go b/internal/test/router.go new file mode 100644 index 0000000..71df630 --- /dev/null +++ b/internal/test/router.go @@ -0,0 +1,22 @@ +package test + +import ( + "context" + + "github.com/jwetzell/showbridge-go/internal/common" +) + +type TestRouter struct { +} + +func (r *TestRouter) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []common.RouteIOError) { + return false, nil +} + +func (r *TestRouter) HandleOutput(ctx context.Context, destinationId string, payload any) error { + return nil +} + +func GetNewTestRouter() *TestRouter { + return &TestRouter{} +} diff --git a/internal/test/struct.go b/internal/test/struct.go new file mode 100644 index 0000000..6e9a3bd --- /dev/null +++ b/internal/test/struct.go @@ -0,0 +1,40 @@ +package test + +type TestStruct struct { + String string + Int int + Float float64 + Bool bool + Data any + IntSlice []int +} + +func (t TestStruct) GetString() string { + return t.String +} + +func (t TestStruct) GetInt() int { + return t.Int +} + +func (t TestStruct) GetFloat() float64 { + return t.Float +} + +func (t TestStruct) GetBool() bool { + return t.Bool +} + +func (t TestStruct) GetData() any { + return t.Data +} + +func (t TestStruct) GetIntSlice() []int { + return t.IntSlice +} + +func (t TestStruct) Void() {} + +func (t TestStruct) MultipleReturnValues() (string, int) { + return t.String, t.Int +}