-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} | ||
|
||
} |