mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
move test implementations to a shared internal package
This commit is contained in:
27
internal/test/context.go
Normal file
27
internal/test/context.go
Normal file
@@ -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
|
||||
}
|
||||
106
internal/test/module.go
Normal file
106
internal/test/module.go
Normal file
@@ -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
|
||||
}
|
||||
17
internal/test/processor.go
Normal file
17
internal/test/processor.go
Normal file
@@ -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
|
||||
}
|
||||
22
internal/test/router.go
Normal file
22
internal/test/router.go
Normal file
@@ -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{}
|
||||
}
|
||||
40
internal/test/struct.go
Normal file
40
internal/test/struct.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user