From 260b4c11338d74e48159ec2206729ec03a97c995 Mon Sep 17 00:00:00 2001 From: Shaun Remekie Date: Sat, 7 Oct 2023 22:51:48 +0100 Subject: [PATCH] added config tests --- processor/starlarktransform/config_test.go | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 processor/starlarktransform/config_test.go diff --git a/processor/starlarktransform/config_test.go b/processor/starlarktransform/config_test.go new file mode 100644 index 000000000000..067c0d2282e2 --- /dev/null +++ b/processor/starlarktransform/config_test.go @@ -0,0 +1,66 @@ +package starlarktransform + +import ( + "errors" + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestConfig(t *testing.T) { + testscript := "def transform(event):\n json.decode(event)\n return event" + mockHTTPServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, testscript) + })) + + testcases := []struct { + name string + config Config + expected string + expectedErr error + validationErr error + }{ + { + name: "with file path", + config: Config{ + Script: "./testdata/test.star", + }, + expected: testscript, + }, + { + name: "with http url", + config: Config{ + Script: mockHTTPServer.URL, + }, + expected: testscript, + }, + { + name: "empty script and code", + config: Config{}, + validationErr: errors.New("a value must be given for altest one, [code] or [script]"), + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + err := tc.config.validate() + require.Equal(t, tc.validationErr, err) + if err != nil { + return + } + + code, err := tc.config.GetCode() + require.Equal(t, tc.expectedErr, err) + if err != nil { + return + } + + assert.Equal(t, tc.expected, code) + }) + } + +}