Skip to content

Commit

Permalink
updated random string transformer and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 21, 2023
1 parent 1382ec2 commit 4f31fd7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 31 deletions.
4 changes: 2 additions & 2 deletions worker/internal/benthos/transformers/phone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Test_PhoneNumberTransformerWithValue(t *testing.T) {
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the phone transformer")

res, err := ex.Query(testVal)
res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Len(t, res.(string), len(testVal), "Generated phone number must be the same length as the input phone number")
Expand All @@ -119,7 +119,7 @@ func Test_PhoneNumberTransformerWithNoValue(t *testing.T) {

testVal := "6183849282"

res, err := ex.Query(testVal)
res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Len(t, res.(string), len(testVal), "Generated phone number must be the same length as the input phone number")
Expand Down
2 changes: 1 addition & 1 deletion worker/internal/benthos/transformers/random_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestRandomFloatTransformerWithValue(t *testing.T) {
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random float transformer")

res, err := ex.Query(testVal)
res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Equal(t, GetFloatLength(testVal), GetFloatLength(res.(float64))) // Generated Float must be the same length as the input Float"
Expand Down
2 changes: 1 addition & 1 deletion worker/internal/benthos/transformers/random_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestRandomIntTransformerWithValue(t *testing.T) {
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random int transformer")

res, err := ex.Query(testVal)
res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Equal(t, transformer_utils.GetIntLength(testVal), transformer_utils.GetIntLength(res.(int64))) // Generated int must be the same length as the input int"
Expand Down
76 changes: 55 additions & 21 deletions worker/internal/benthos/transformers/random_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,51 @@ const defaultStrLength = 10
func init() {

spec := bloblang.NewPluginSpec().
Param(bloblang.NewBoolParam("preserve_length")).
Param(bloblang.NewInt64Param("str_length"))
Param(bloblang.NewStringParam("value").Optional()).
Param(bloblang.NewBoolParam("preserve_length").Optional()).
Param(bloblang.NewInt64Param("str_length").Optional())

// register the plugin
err := bloblang.RegisterMethodV2("randomstringtransformer", spec, func(args *bloblang.ParsedParams) (bloblang.Method, error) {
err := bloblang.RegisterFunctionV2("randomstringtransformer", spec, func(args *bloblang.ParsedParams) (bloblang.Function, error) {

preserveLength, err := args.GetBool("preserve_length")
valuePtr, err := args.GetOptionalString("value")
if err != nil {
return nil, err
}

strLength, err := args.GetInt64("str_length")
var value string
if valuePtr != nil {
value = *valuePtr
}

preserveLengthPtr, err := args.GetOptionalBool("preserve_length")
if err != nil {
return nil, err
}

Check warning on line 36 in worker/internal/benthos/transformers/random_string.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_string.go#L35-L36

Added lines #L35 - L36 were not covered by tests

var preserveLength bool
if preserveLengthPtr != nil {
preserveLength = *preserveLengthPtr
}

strLengthPtr, err := args.GetOptionalInt64("str_length")
if err != nil {
return nil, err
}

var strLength int64
if strLengthPtr != nil {
strLength = *strLengthPtr
}

if err != nil {
return nil, fmt.Errorf("unable to convert the string case to a defined enum value")
}

return bloblang.StringMethod(func(s string) (any, error) {
res, err := GenerateRandomString(s, preserveLength, strLength)
return func() (any, error) {
res, err := GenerateRandomString(value, preserveLength, strLength)
return res, err
}), nil
}, nil
})

if err != nil {
Expand All @@ -46,33 +67,46 @@ func init() {
}

// main transformer logic goes here
func GenerateRandomString(s string, preserveLength bool, strLength int64) (string, error) {
func GenerateRandomString(value string, preserveLength bool, strLength int64) (string, error) {
var returnValue string

if preserveLength && strLength > 0 {
return "", fmt.Errorf("preserve length and int length params cannot both be true")
}

if preserveLength {
if value != "" {

val, err := transformer_utils.GenerateRandomStringWithLength(int64(len(s)))
if preserveLength {

if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
}
val, err := transformer_utils.GenerateRandomStringWithLength(int64(len(value)))

returnValue = val
if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
}

Check warning on line 85 in worker/internal/benthos/transformers/random_string.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_string.go#L84-L85

Added lines #L84 - L85 were not covered by tests

} else if strLength > 0 {
returnValue = val

val, err := transformer_utils.GenerateRandomStringWithLength(strLength)
} else if strLength > 0 {

if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
}
val, err := transformer_utils.GenerateRandomStringWithLength(strLength)

returnValue = val
if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
}

Check warning on line 95 in worker/internal/benthos/transformers/random_string.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_string.go#L94-L95

Added lines #L94 - L95 were not covered by tests

returnValue = val

} else {

val, err := transformer_utils.GenerateRandomStringWithLength(defaultStrLength)

if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
}

Check warning on line 105 in worker/internal/benthos/transformers/random_string.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_string.go#L104-L105

Added lines #L104 - L105 were not covered by tests

returnValue = val

}
} else {

val, err := transformer_utils.GenerateRandomStringWithLength(defaultStrLength)
Expand Down
22 changes: 17 additions & 5 deletions worker/internal/benthos/transformers/random_string_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package neosync_transformers

import (
"fmt"
"testing"

"github.com/benthosdev/benthos/v4/public/bloblang"
Expand Down Expand Up @@ -42,16 +43,27 @@ func TestRandomStringPreserveLengthFalseStrLength(t *testing.T) {

}

func TestRandomStringTransformer(t *testing.T) {
mapping := `root = this.randomstringtransformer(true, -1)`
func TestRandomStringTransformerWithValue(t *testing.T) {
testVal := "testte"
mapping := fmt.Sprintf(`root = randomstringtransformer(%q, false, 6)`, testVal)
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random string transformer")

testVal := "testte"

res, err := ex.Query(testVal)
res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Equal(t, len(testVal), len(res.(string)), "Generated string must be the same length as the input string")
assert.IsType(t, res, testVal)
}

func TestRandomStringTransformerWithNoValue(t *testing.T) {
mapping := `root = randomstringtransformer()`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random string transformer")

res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Equal(t, 10, len(res.(string)), "Generated string must be the same length as the input string")
assert.IsType(t, res, "")
}
2 changes: 1 addition & 1 deletion worker/internal/benthos/transformers/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestUUIDTransformer(t *testing.T) {
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the uuid transformer")

res, err := ex.Query("test") // input is ignored here
res, err := ex.Query(nil) // input is ignored here
assert.NoError(t, err)

assert.Len(t, res.(string), 36, "UUIDs with hyphens must have 36 characters")
Expand Down

0 comments on commit 4f31fd7

Please sign in to comment.