Skip to content

Commit

Permalink
refactoed and updated random float transformer and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 21, 2023
1 parent 919394f commit b0c6460
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 46 deletions.
2 changes: 0 additions & 2 deletions worker/internal/benthos/transformers/random_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ func init() {

spec := bloblang.NewPluginSpec()

// register the function

err := bloblang.RegisterFunctionV2("randombooltransformer", spec, func(args *bloblang.ParsedParams) (bloblang.Function, error) {

return func() (any, error) {
Expand Down
4 changes: 2 additions & 2 deletions worker/internal/benthos/transformers/random_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestProcessRandomBool(t *testing.T) {
func Test_ProcessRandomBool(t *testing.T) {

res, err := GenerateRandomBool()

Expand All @@ -16,7 +16,7 @@ func TestProcessRandomBool(t *testing.T) {

}

func TestRandomBoolTransformer(t *testing.T) {
func Test_RandomBoolTransformer(t *testing.T) {
mapping := `root = randombooltransformer()`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random bool transformer")
Expand Down
87 changes: 60 additions & 27 deletions worker/internal/benthos/transformers/random_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,59 @@ import (
func init() {

spec := bloblang.NewPluginSpec().
Param(bloblang.NewBoolParam("preserve_length")).
Param(bloblang.NewInt64Param("digits_before_decimal")).
Param(bloblang.NewInt64Param("digits_after_decimal"))
Param(bloblang.NewFloat64Param("value").Optional()).
Param(bloblang.NewBoolParam("preserve_length").Optional()).
Param(bloblang.NewInt64Param("digits_before_decimal").Optional()).
Param(bloblang.NewInt64Param("digits_after_decimal").Optional())

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

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

digitsBeforeDecimal, err := args.GetInt64("digits_before_decimal")
var value float64
if valuePtr != nil {
value = *valuePtr
}

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

Check warning on line 37 in worker/internal/benthos/transformers/random_float.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L36-L37

Added lines #L36 - L37 were not covered by tests

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

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

digitsAfterDecimal, err := args.GetInt64("digits_after_decimal")
var digitsBeforeDecimal int64
if digitsBeforeDecimalPtr != nil {
digitsBeforeDecimal = *digitsBeforeDecimalPtr
}

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

return bloblang.Float64Method(func(i float64) (any, error) {
if preserveLength {
res, err := GenerateRandomFloatPreserveLength(i, preserveLength)
return res, err
} else {
res, err := GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal)
return res, err
}
}), nil
var digitsAfterDecimal int64
if digitsAfterDecimalPtr != nil {
digitsAfterDecimal = *digitsAfterDecimalPtr
}

return func() (any, error) {
res, err := GenerateRandomFloat(value, preserveLength, digitsAfterDecimal, digitsBeforeDecimal)
return res, err

}, nil
})

if err != nil {
Expand All @@ -52,24 +74,39 @@ func init() {

}

func GenerateRandomFloatPreserveLength(i float64, preserveLength bool) (float64, error) {
func GenerateRandomFloat(value float64, preserveLength bool, digitsAfterDecimal, digitsBeforeDecimal int64) (float64, error) {

var returnValue float64
if value != 0 {
if preserveLength {
fLen := GetFloatLength(value)
res, err := GenerateRandomFloatWithDefinedLength(int64(fLen.DigitsBeforeDecimalLength), int64(fLen.DigitsAfterDecimalLength))
return res, err
} else {
res, err := GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal)
return res, err
}

Check warning on line 87 in worker/internal/benthos/transformers/random_float.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L85-L87

Added lines #L85 - L87 were not covered by tests
} else {
res, err := GenerateRandomFloatWithRandomLength()
return res, err
}
}

fLen := GetFloatLength(i)
func GenerateRandomFloatWithRandomLength() (float64, error) {

bd, err := transformer_utils.GenerateRandomInt(int64(fLen.DigitsBeforeDecimalLength))
var returnValue float64

bd, err := transformer_utils.GenerateRandomInt(int64(3))
if err != nil {
return 0, fmt.Errorf("unable to generate a random before digits integer")
}

ad, err := transformer_utils.GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength))
ad, err := transformer_utils.GenerateRandomInt(int64(3))

for {
if !transformer_utils.IsLastDigitZero(ad) {
break // Exit the loop when i is greater than or equal to 5
break
}
ad, err = transformer_utils.GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength))
ad, err = transformer_utils.GenerateRandomInt(int64(3))

Check warning on line 109 in worker/internal/benthos/transformers/random_float.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L109

Added line #L109 was not covered by tests

if err != nil {
return 0, fmt.Errorf("unable to generate a random int64 to convert to a float")
Expand Down Expand Up @@ -120,10 +157,6 @@ func GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecima
return 0, fmt.Errorf("unable to generate a random after digits integer")
}

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

combinedStr := fmt.Sprintf("%d.%d", bd, ad)

result, err := strconv.ParseFloat(combinedStr, 64)
Expand Down
47 changes: 39 additions & 8 deletions worker/internal/benthos/transformers/random_float_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package neosync_transformers

import (
"fmt"
"testing"

"github.com/benthosdev/benthos/v4/public/bloblang"
transformer_utils "github.com/nucleuscloud/neosync/worker/internal/benthos/transformers/utils"
"github.com/stretchr/testify/assert"
)

func TestProcessRandomFloatPreserveLength(t *testing.T) {
func Test_GenerateRandomFloat(t *testing.T) {

val := float64(6754.3543)
expectedLength := 8

res, err := GenerateRandomFloatPreserveLength(val, true)
res, err := GenerateRandomFloat(val, true, 3, 4)

actual := GetFloatLength(res).DigitsBeforeDecimalLength + GetFloatLength(res).DigitsAfterDecimalLength

assert.NoError(t, err)
assert.Equal(t, expectedLength, actual, "The output float needs to be the same length as the input Float")

}

func TestProcessRandomFloatPreserveLengthFalse(t *testing.T) {
func Test_GenerateRandomFloatPreserveLengthFalse(t *testing.T) {

expectedLength := 6

Expand All @@ -32,16 +33,46 @@ func TestProcessRandomFloatPreserveLengthFalse(t *testing.T) {
assert.Equal(t, actual, expectedLength, "The length of the output float needs to match the digits before + the digits after")
}

func TestRandomFloatTransformer(t *testing.T) {
mapping := `root = this.randomfloattransformer(true, 2,3)`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random float transformer")
func Test_GenerateRandomFloatWithRandomLength(t *testing.T) {

res, err := GenerateRandomFloatWithRandomLength()

actual := GetFloatLength(res).DigitsAfterDecimalLength + GetFloatLength(res).DigitsBeforeDecimalLength
assert.NoError(t, err)
assert.Equal(t, 6, actual, "The length of the output float needs to match the digits before + the digits after")
}

func TestRandomFloatTransformerWithValue(t *testing.T) {
testVal := float64(397.34)
mapping := fmt.Sprintf(`root = randomfloattransformer(%f, true, 2,3)`, testVal)
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the random float transformer")

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

assert.Equal(t, GetFloatLength(testVal), GetFloatLength(res.(float64))) // Generated Float must be the same length as the input Float"
assert.IsType(t, res, testVal)
}

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

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

actual := GetFloatLength(res.(float64)).DigitsAfterDecimalLength + GetFloatLength(res.(float64)).DigitsBeforeDecimalLength
assert.IsType(t, res, float64(1))
assert.Equal(t, 6, actual, "The length of the output float needs to match the digits before + the digits after")
}

func Test_GetFloatLength(t *testing.T) {
val := float64(3.14)
res := GetFloatLength(val)

assert.Equal(t, int64(1), transformer_utils.GetIntLength(int64(res.DigitsBeforeDecimalLength)))
assert.Equal(t, int64(1), transformer_utils.GetIntLength(int64(res.DigitsAfterDecimalLength)))

}
15 changes: 8 additions & 7 deletions worker/internal/benthos/transformers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,21 @@ func IntSliceToStringSlice(ints []int64) []string {
return str
}

// generates a random integer of length l that is passed in as a int64 param
func GenerateRandomInt(digits int64) (int64, error) {
if digits <= 0 {
// generates a random integer of length l that is passed in as a int64 param i.e. an l of 3 will generate
// an int64 of 3 digits such as 123 or 789.
func GenerateRandomInt(l int64) (int64, error) {
if l <= 0 {
return 0, fmt.Errorf("count is zero or not a positive integer")
}

// int64 only supports 18 digits, so if the count => 19, this will error out
if digits >= 19 {
if l >= 19 {
return 0, fmt.Errorf("count has to be less than 18 digits since int64 only supports up to 18 digits")
}

// Calculate the min and max values for count
minValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(digits-1), nil)
maxValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(digits), nil)
minValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(l-1), nil)
maxValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(l), nil)

// Generate a random integer within the specified range
randInt, err := rand.Int(rand.Reader, maxValue)
Expand All @@ -105,7 +106,7 @@ func GenerateRandomInt(digits int64) (int64, error) {
*/

if FirstDigitIsNine(randInt.Int64()) && GetIntLength(randInt.Int64()) == digits {
if FirstDigitIsNine(randInt.Int64()) && GetIntLength(randInt.Int64()) == l {
return randInt.Int64(), nil
} else {
randInt.Add(randInt, minValue)
Expand Down

0 comments on commit b0c6460

Please sign in to comment.