From 7312bfec20eb0ad3f198d718082cd9a1214914b8 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sun, 8 Feb 2026 15:56:14 -0600 Subject: [PATCH] add error tests for random processors --- internal/processor/int-random_test.go | 72 ++++++++++++++++++++++++++ internal/processor/uint-random_test.go | 72 ++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/internal/processor/int-random_test.go b/internal/processor/int-random_test.go index b29c51f..521abd0 100644 --- a/internal/processor/int-random_test.go +++ b/internal/processor/int-random_test.go @@ -101,3 +101,75 @@ func TestGoodIntRandom(t *testing.T) { }) } } + +func TestBadIntRandom(t *testing.T) { + tests := []struct { + name string + params map[string]any + payload any + errorString string + }{ + { + name: "no min param", + payload: "hello", + params: map[string]any{"max": 10.0}, + errorString: "int.random requires a min parameter", + }, + { + name: "no max param", + payload: "hello", + params: map[string]any{"min": 1.0}, + errorString: "int.random requires a max parameter", + }, + { + name: "min param not a number", + payload: "hello", + params: map[string]any{"min": "1", "max": 10.0}, + errorString: "int.random min must be a number", + }, + { + name: "max param not a number", + payload: "hello", + params: map[string]any{"min": 1.0, "max": "10"}, + errorString: "int.random max must be a number", + }, + { + name: "max less than min", + payload: "hello", + params: map[string]any{"min": 1.0, "max": 0.0}, + errorString: "int.random max must be greater than min", + }, + } + + for _, test := range tests { + t.Run(test.name, func(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: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("int.random got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + got, err := processorInstance.Process(t.Context(), test.payload) + + if err == nil { + t.Fatalf("int.random expected to fail but got payload: %s", got) + } + + if err.Error() != test.errorString { + t.Fatalf("int.random got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/processor/uint-random_test.go b/internal/processor/uint-random_test.go index 82ad8b6..9270de8 100644 --- a/internal/processor/uint-random_test.go +++ b/internal/processor/uint-random_test.go @@ -103,3 +103,75 @@ func TestGoodUintRandom(t *testing.T) { }) } } + +func TestBadUintRandom(t *testing.T) { + tests := []struct { + name string + params map[string]any + payload any + errorString string + }{ + { + name: "no min param", + payload: "hello", + params: map[string]any{"max": 10.0}, + errorString: "uint.random requires a min parameter", + }, + { + name: "no max param", + payload: "hello", + params: map[string]any{"min": 1.0}, + errorString: "uint.random requires a max parameter", + }, + { + name: "min param not a number", + payload: "hello", + params: map[string]any{"min": "1", "max": 10.0}, + errorString: "uint.random min must be a number", + }, + { + name: "max param not a number", + payload: "hello", + params: map[string]any{"min": 1.0, "max": "10"}, + errorString: "uint.random max must be a number", + }, + { + name: "max less than min", + payload: "hello", + params: map[string]any{"min": 1.0, "max": 0.0}, + errorString: "uint.random max must be greater than min", + }, + } + + for _, test := range tests { + t.Run(test.name, func(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: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("uint.random got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + got, err := processorInstance.Process(t.Context(), test.payload) + + if err == nil { + t.Fatalf("uint.random expected to fail but got payload: %s", got) + } + + if err.Error() != test.errorString { + t.Fatalf("uint.random got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +}