Skip to content

Commit

Permalink
updated random int transformer and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 21, 2023
1 parent b0c6460 commit 1382ec2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 deletions.
76 changes: 54 additions & 22 deletions worker/internal/benthos/transformers/random_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,46 @@ const defaultIntLength = 4
func init() {

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

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

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

intLength, err := args.GetInt64("int_length")
var value int64
if valuePtr != nil {
value = *valuePtr
}
preserveLengthPtr, err := args.GetOptionalBool("preserve_length")
if err != nil {
return nil, err
}

Check warning on line 35 in worker/internal/benthos/transformers/random_int.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_int.go#L34-L35

Added lines #L34 - L35 were not covered by tests

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

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

return bloblang.Int64Method(func(i int64) (any, error) {
res, err := GenerateRandomInt(i, preserveLength, intLength)
var intLength int64
if intLengthPtr != nil {
intLength = *intLengthPtr
}

return func() (any, error) {
res, err := GenerateRandomInt(value, preserveLength, intLength)
return res, err
}), nil
}, nil
})

if err != nil {
Expand All @@ -41,34 +61,46 @@ func init() {

}

// main transformer logic goes here
func GenerateRandomInt(i int64, preserveLength bool, intLength int64) (int64, error) {
func GenerateRandomInt(value int64, preserveLength bool, intLength int64) (int64, error) {
var returnValue int64

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

if preserveLength {
if value != 0 {

val, err := transformer_utils.GenerateRandomInt(transformer_utils.GetIntLength(i))
if preserveLength {

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

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

Check warning on line 79 in worker/internal/benthos/transformers/random_int.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_int.go#L78-L79

Added lines #L78 - L79 were not covered by tests

} else if intLength > 0 {
returnValue = val

val, err := transformer_utils.GenerateRandomInt(intLength)
} else if intLength > 0 {

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

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

Check warning on line 89 in worker/internal/benthos/transformers/random_int.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_int.go#L88-L89

Added lines #L88 - L89 were not covered by tests

returnValue = val

} else {

val, err := transformer_utils.GenerateRandomInt(defaultIntLength)

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

Check warning on line 99 in worker/internal/benthos/transformers/random_int.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_int.go#L98-L99

Added lines #L98 - L99 were not covered by tests

returnValue = val

}
} else {

val, err := transformer_utils.GenerateRandomInt(defaultIntLength)
Expand Down
21 changes: 17 additions & 4 deletions worker/internal/benthos/transformers/random_int_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 @@ -53,16 +54,28 @@ func TestGenerateandomIntPreserveLengthFalseIntLength(t *testing.T) {

}

func TestRandomIntTransformer(t *testing.T) {
mapping := `root = this.randominttransformer(false, 6)`
func TestRandomIntTransformerWithValue(t *testing.T) {
testVal := int64(397283)
mapping := fmt.Sprintf(`root = randominttransformer(%d, false, 6)`, testVal)
fmt.Println("mapping", mapping)
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random int transformer")

testVal := int64(397283)

res, err := ex.Query(testVal)
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"
assert.IsType(t, res, testVal)
}

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

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

assert.Equal(t, int64(4), transformer_utils.GetIntLength(res.(int64))) // Generated int must be the same length as the input int"
assert.IsType(t, res, int64(2))
}
6 changes: 3 additions & 3 deletions worker/pkg/workflows/datasync/activities/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func computeMutationFunction(col *mgmtv1alpha1.JobMapping) (string, error) {
pl := col.Transformer.Config.GetPhoneNumberConfig().PreserveLength
ef := col.Transformer.Config.GetPhoneNumberConfig().E164Format
ih := col.Transformer.Config.GetPhoneNumberConfig().IncludeHyphens
return fmt.Sprintf("this.%s.phonetransformer(%t, %t, %t)", col.Column, pl, ef, ih), nil
return fmt.Sprintf("phonetransformer(this.%s,%t, %t, %t)", col.Column, pl, ef, ih), nil

Check warning on line 587 in worker/pkg/workflows/datasync/activities/activities.go

View check run for this annotation

Codecov / codecov/patch

worker/pkg/workflows/datasync/activities/activities.go#L587

Added line #L587 was not covered by tests
case "int_phone_number":
pl := col.Transformer.Config.GetIntPhoneNumberConfig().PreserveLength
return fmt.Sprintf("intphonetransformer(this.%s, %t)", col.Column, pl), nil

Check warning on line 590 in worker/pkg/workflows/datasync/activities/activities.go

View check run for this annotation

Codecov / codecov/patch

worker/pkg/workflows/datasync/activities/activities.go#L590

Added line #L590 was not covered by tests
Expand All @@ -602,12 +602,12 @@ func computeMutationFunction(col *mgmtv1alpha1.JobMapping) (string, error) {
case "random_int":
pl := col.Transformer.Config.GetRandomIntConfig().PreserveLength
sl := col.Transformer.Config.GetRandomIntConfig().IntLength
return fmt.Sprintf(`this.%s.randominttransformer(%t, %d)`, col.Column, pl, sl), nil
return fmt.Sprintf(`randominttransformer(this.%s,%t, %d)`, col.Column, pl, sl), nil

Check warning on line 605 in worker/pkg/workflows/datasync/activities/activities.go

View check run for this annotation

Codecov / codecov/patch

worker/pkg/workflows/datasync/activities/activities.go#L605

Added line #L605 was not covered by tests
case "random_float":
pl := col.Transformer.Config.GetRandomFloatConfig().PreserveLength
bd := col.Transformer.Config.GetRandomFloatConfig().DigitsBeforeDecimal
ad := col.Transformer.Config.GetRandomFloatConfig().DigitsAfterDecimal
return fmt.Sprintf(`this.%s.randomfloattransformer(%t, %d, %d)`, col.Column, pl, bd, ad), nil
return fmt.Sprintf(`randomfloattransformer(this.%s, %t, %d, %d)`, col.Column, pl, bd, ad), nil

Check warning on line 610 in worker/pkg/workflows/datasync/activities/activities.go

View check run for this annotation

Codecov / codecov/patch

worker/pkg/workflows/datasync/activities/activities.go#L610

Added line #L610 was not covered by tests
case "gender":
ab := col.Transformer.Config.GetGenderConfig().Abbreviate
return fmt.Sprintf(`gendertransformer(%t)`, ab), nil
Expand Down

0 comments on commit 1382ec2

Please sign in to comment.