diff --git a/internal/processor/int-random_test.go b/internal/processor/int-random_test.go new file mode 100644 index 0000000..b29c51f --- /dev/null +++ b/internal/processor/int-random_test.go @@ -0,0 +1,103 @@ +package processor_test + +import ( + "testing" + + "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/processor" +) + +func TestIntRandomFromRegistry(t *testing.T) { + registration, ok := processor.ProcessorRegistry["int.random"] + if !ok { + t.Fatalf("int.random processor not registered") + } + + processorInstance, err := registration.New(config.ProcessorConfig{ + Type: "int.random", + Params: map[string]any{ + "min": 1.0, + "max": 10.0, + }, + }) + + if err != nil { + t.Fatalf("failed to create int.random processor: %s", err) + } + + if processorInstance.Type() != "int.random" { + t.Fatalf("int.random processor has wrong type: %s", processorInstance.Type()) + } +} + +func TestIntRandomGoodConfig(t *testing.T) { + registration, ok := processor.ProcessorRegistry["int.random"] + if !ok { + t.Fatalf("int.random processor not registered") + } + + processorInstance, err := registration.New(config.ProcessorConfig{ + Type: "int.random", + Params: map[string]any{ + "min": 1.0, + "max": 10.0, + }, + }) + + if err != nil { + t.Fatalf("int.random should have created processor but got error: %s", err) + } + + payload := "12345" + + got, err := processorInstance.Process(t.Context(), payload) + if err != nil { + t.Fatalf("int.random processing failed: %s", err) + } + + gotInt, ok := got.(int) + if !ok { + t.Fatalf("int.random returned a %T payload: %s", got, got) + } + + if gotInt < 1 || gotInt > 10 { + t.Fatalf("int.random got %d, expected between %d and %d", gotInt, 1, 10) + } +} + +func TestGoodIntRandom(t *testing.T) { + tests := []struct { + processor processor.Processor + name string + payload any + min int + max int + }{ + { + name: "1-10", + payload: "12345", + min: 1, + max: 10, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + intRandom := processor.IntRandom{ + Min: test.min, + Max: test.max, + } + got, err := intRandom.Process(t.Context(), test.payload) + gotInt, ok := got.(int) + if !ok { + t.Fatalf("int.random returned a %T payload: %s", got, got) + } + if err != nil { + t.Fatalf("int.random failed: %s", err) + } + if gotInt < test.min || gotInt > test.max { + t.Fatalf("int.random got %d, expected between %d and %d", gotInt, test.min, test.max) + } + }) + } +} diff --git a/internal/processor/uint-random_test.go b/internal/processor/uint-random_test.go new file mode 100644 index 0000000..82ad8b6 --- /dev/null +++ b/internal/processor/uint-random_test.go @@ -0,0 +1,105 @@ +package processor_test + +import ( + "testing" + + "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/processor" +) + +func TestUintRandomFromRegistry(t *testing.T) { + registration, ok := processor.ProcessorRegistry["uint.random"] + if !ok { + t.Fatalf("uint.random processor not registered") + } + + processorInstance, err := registration.New(config.ProcessorConfig{ + Type: "uint.random", + Params: map[string]any{ + "min": 1.0, + "max": 10.0, + }, + }) + + if err != nil { + t.Fatalf("failed to create uint.random processor: %s", err) + } + + if processorInstance.Type() != "uint.random" { + t.Fatalf("uint.random processor has wrong type: %s", processorInstance.Type()) + } +} + +func TestUintRandomGoodConfig(t *testing.T) { + registration, ok := processor.ProcessorRegistry["uint.random"] + if !ok { + t.Fatalf("uint.random processor not registered") + } + + processorInstance, err := registration.New(config.ProcessorConfig{ + Type: "uint.random", + Params: map[string]any{ + "min": 1.0, + "max": 10.0, + }, + }) + + if err != nil { + t.Fatalf("uint.random should have created processor but got error: %s", err) + } + + payload := "12345" + + got, err := processorInstance.Process(t.Context(), payload) + if err != nil { + t.Fatalf("uint.random processing failed: %s", err) + } + + gotUint, ok := got.(uint) + if !ok { + t.Fatalf("uint.random returned a %T payload: %s", got, got) + } + + if gotUint < 1 || gotUint > 10 { + t.Fatalf("uint.random got %d, expected between %d and %d", gotUint, 1, 10) + } +} + +func TestGoodUintRandom(t *testing.T) { + + tests := []struct { + processor processor.Processor + name string + payload any + min uint + max uint + }{ + { + name: "1-10", + payload: "12345", + min: 1, + max: 10, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + uintRandom := processor.UintRandom{ + Min: test.min, + Max: test.max, + } + got, err := uintRandom.Process(t.Context(), test.payload) + + gotUint, ok := got.(uint) + if !ok { + t.Fatalf("uint.random returned a %T payload: %s", got, got) + } + if err != nil { + t.Fatalf("uint.random failed: %s", err) + } + if gotUint < test.min || gotUint > test.max { + t.Fatalf("uint.random got %d, expected between %d and %d", gotUint, test.min, test.max) + } + }) + } +} diff --git a/router_test.go b/router_test.go index 7125c6b..fd52b0f 100644 --- a/router_test.go +++ b/router_test.go @@ -72,6 +72,23 @@ func TestNewRouter(t *testing.T) { } } +func TestNewRouterNoModuleId(t *testing.T) { + routerConfig := config.Config{ + Modules: []config.ModuleConfig{ + { + Id: "", + Type: "mock.counter", + }, + }, + } + + _, moduleErrors, _ := showbridge.NewRouter(routerConfig) + + if moduleErrors == nil { + t.Fatalf("router should have returned 'unknown module' module errors") + } +} + func TestNewRouterUnknownModuleType(t *testing.T) { routerConfig := config.Config{ Modules: []config.ModuleConfig{