From cbfe79901f800877a7590fde75a74befd1ffa3a5 Mon Sep 17 00:00:00 2001 From: Evis Drenova Date: Tue, 31 Oct 2023 17:14:08 -0700 Subject: [PATCH 1/4] uncommenting test --- .../benthos/transformers/random_float.go | 6 +++--- .../benthos/transformers/random_float_test.go | 17 ++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/worker/internal/benthos/transformers/random_float.go b/worker/internal/benthos/transformers/random_float.go index 7e6c838d3e..51baf18a71 100644 --- a/worker/internal/benthos/transformers/random_float.go +++ b/worker/internal/benthos/transformers/random_float.go @@ -70,7 +70,7 @@ func ProcessRandomFloat(i float64, preserveLength bool, digitsBeforeDecimal, dig ad, err := GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) for { - if !LastDigitAZero(ad) { + if !isLastDigitAZero(ad) { break // Exit the loop when i is greater than or equal to 5 } ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) @@ -104,7 +104,7 @@ func ProcessRandomFloat(i float64, preserveLength bool, digitsBeforeDecimal, dig // generate a new number if it ends in a zero so that the trailing zero doesn't get stripped and return // a value that is shorter than what the user asks for. This happens in the when we convert the string to a float64 for { - if !LastDigitAZero(ad) { + if !isLastDigitAZero(ad) { break // Exit the loop when i is greater than or equal to 5 } ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) @@ -153,7 +153,7 @@ func GetFloatLength(i float64) *FloatLength { } } -func LastDigitAZero(n int64) bool { +func isLastDigitAZero(n int64) bool { // Convert the int64 to a string str := strconv.FormatInt(n, 10) diff --git a/worker/internal/benthos/transformers/random_float_test.go b/worker/internal/benthos/transformers/random_float_test.go index 1769f10ac9..04742da8e4 100644 --- a/worker/internal/benthos/transformers/random_float_test.go +++ b/worker/internal/benthos/transformers/random_float_test.go @@ -21,18 +21,17 @@ func TestProcessRandomFloatPreserveLength(t *testing.T) { } -//nolint -// func TestProcessRandomFloatPreserveLengthFalse(t *testing.T) { +func TestProcessRandomFloatPreserveLengthFalse(t *testing.T) { -// val := float64(6754.3543) -// expectedLength := 5 + val := float64(6754.3543) + expectedLength := 5 -// res, err := ProcessRandomFloat(val, false, int64(3), int64(3)) + res, err := ProcessRandomFloat(val, false, int64(3), int64(3)) -// actual := GetFloatLength(res).DigitsAfterDecimalLength + GetFloatLength(res).DigitsBeforeDecimalLength -// assert.NoError(t, err) -// assert.Equal(t, actual, expectedLength, "The output Float needs to be the same length as the input Float") -// } + actual := GetFloatLength(res).DigitsAfterDecimalLength + GetFloatLength(res).DigitsBeforeDecimalLength + assert.NoError(t, err) + assert.Equal(t, actual, expectedLength, "The output Float needs to be the same length as the input Float") +} func TestRandomFloatTransformer(t *testing.T) { mapping := `root = this.randomfloattransformer(true, 2,3)` From f8a18c652dbcf57fcb302e45e1b81f30055820e6 Mon Sep 17 00:00:00 2001 From: Evis Drenova Date: Tue, 31 Oct 2023 18:15:09 -0700 Subject: [PATCH 2/4] fixed flaky test --- .../benthos/transformers/random_float.go | 116 +++++++++--------- .../benthos/transformers/random_float_test.go | 7 +- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/worker/internal/benthos/transformers/random_float.go b/worker/internal/benthos/transformers/random_float.go index 51baf18a71..bfa8d8c992 100644 --- a/worker/internal/benthos/transformers/random_float.go +++ b/worker/internal/benthos/transformers/random_float.go @@ -38,8 +38,15 @@ func init() { } return bloblang.Float64Method(func(i float64) (any, error) { - res, err := ProcessRandomFloat(i, preserveLength, digitsBeforeDecimal, digitsAfterDecimal) - return res, err + // res, err := ProcessRandomFloat(i, preserveLength, digitsBeforeDecimal, digitsAfterDecimal) + + if preserveLength { + res, err := GenerateRandomFloatPreserveLength(i, preserveLength) + return res, err + } else { + res, err := GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal) + return res, err + } }), nil }) @@ -49,90 +56,87 @@ func init() { } -// main transformer logic goes here -func ProcessRandomFloat(i float64, preserveLength bool, digitsBeforeDecimal, digitsAfterDecimal int64) (float64, error) { +func GenerateRandomFloatPreserveLength(i float64, preserveLength bool) (float64, error) { var returnValue float64 fLen := GetFloatLength(i) - if digitsBeforeDecimal < 0 || digitsAfterDecimal < 0 { - return 0.0, fmt.Errorf("digitsBefore and digitsAfter must be non-negative") + bd, err := GenerateRandomInt(int64(fLen.DigitsBeforeDecimalLength)) + if err != nil { + return 0, fmt.Errorf("unable to generate a random before digits integer") } - if preserveLength { - - bd, err := GenerateRandomInt(int64(fLen.DigitsBeforeDecimalLength)) - if err != nil { - return 0, fmt.Errorf("unable to generate a random before digits integer") - } - - ad, err := GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) - - for { - if !isLastDigitAZero(ad) { - break // Exit the loop when i is greater than or equal to 5 - } - ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) + ad, err := GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) - if err != nil { - return 0, fmt.Errorf("unable to generate a random int64 to convert to a float") - } + for { + if !isLastDigitAZero(ad) { + break // Exit the loop when i is greater than or equal to 5 } + ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) if err != nil { - return 0, fmt.Errorf("unable to generate a random after digits integer") + return 0, fmt.Errorf("unable to generate a random int64 to convert to a float") } + } - combinedStr := fmt.Sprintf("%d.%d", bd, ad) + if err != nil { + return 0, fmt.Errorf("unable to generate a random after digits integer") + } - result, err := strconv.ParseFloat(combinedStr, 64) - if err != nil { - return 0, fmt.Errorf("unable to convert string to float") - } + combinedStr := fmt.Sprintf("%d.%d", bd, ad) - returnValue = result - } else { + result, err := strconv.ParseFloat(combinedStr, 64) + if err != nil { + return 0, fmt.Errorf("unable to convert string to float") + } - bd, err := GenerateRandomInt(int64(defaultLenBeforeDecimals)) - if err != nil { - return 0, fmt.Errorf("unable to generate a random before digits integer") - } + returnValue = result + + return returnValue, nil +} - ad, err := GenerateRandomInt(int64(defaultLenAfterDecimals)) +func GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal int64) (float64, error) { - // generate a new number if it ends in a zero so that the trailing zero doesn't get stripped and return - // a value that is shorter than what the user asks for. This happens in the when we convert the string to a float64 - for { - if !isLastDigitAZero(ad) { - break // Exit the loop when i is greater than or equal to 5 - } - ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) + var returnValue float64 - if err != nil { - return 0, fmt.Errorf("unable to generate a random int64 to convert to a float") - } - } + bd, err := GenerateRandomInt(digitsBeforeDecimal) + if err != nil { + return 0, fmt.Errorf("unable to generate a random before digits integer") + } - if err != nil { - return 0, fmt.Errorf("unable to generate a random after digits integer") + ad, err := GenerateRandomInt(digitsAfterDecimal) + + // generate a new number if it ends in a zero so that the trailing zero doesn't get stripped and return + // a value that is shorter than what the user asks for. This happens in when we convert the string to a float64 + for { + if !isLastDigitAZero(ad) { + break // Exit the loop when i is greater than or equal to 5 } + ad, err = GenerateRandomInt(digitsAfterDecimal) if err != nil { - return 0, fmt.Errorf("unable to generate a random string with length") + return 0, fmt.Errorf("unable to generate a random int64 to convert to a float") } + } - combinedStr := fmt.Sprintf("%d.%d", bd, ad) + if err != nil { + return 0, fmt.Errorf("unable to generate a random after digits integer") + } - result, err := strconv.ParseFloat(combinedStr, 64) - if err != nil { - return 0, fmt.Errorf("unable to convert string to float") - } + if err != nil { + return 0, fmt.Errorf("unable to generate a random string with length") + } - returnValue = result + combinedStr := fmt.Sprintf("%d.%d", bd, ad) + result, err := strconv.ParseFloat(combinedStr, 64) + if err != nil { + return 0, fmt.Errorf("unable to convert string to float") } + returnValue = result + return returnValue, nil } diff --git a/worker/internal/benthos/transformers/random_float_test.go b/worker/internal/benthos/transformers/random_float_test.go index 04742da8e4..878a05b0b5 100644 --- a/worker/internal/benthos/transformers/random_float_test.go +++ b/worker/internal/benthos/transformers/random_float_test.go @@ -12,7 +12,7 @@ func TestProcessRandomFloatPreserveLength(t *testing.T) { val := float64(6754.3543) expectedLength := 8 - res, err := ProcessRandomFloat(val, true, int64(3), int64(3)) + res, err := GenerateRandomFloatPreserveLength(val, true) actual := GetFloatLength(res).DigitsBeforeDecimalLength + GetFloatLength(res).DigitsAfterDecimalLength @@ -23,10 +23,9 @@ func TestProcessRandomFloatPreserveLength(t *testing.T) { func TestProcessRandomFloatPreserveLengthFalse(t *testing.T) { - val := float64(6754.3543) - expectedLength := 5 + expectedLength := 6 - res, err := ProcessRandomFloat(val, false, int64(3), int64(3)) + res, err := GenerateRandomFloatWithDefinedLength(int64(3), int64(3)) actual := GetFloatLength(res).DigitsAfterDecimalLength + GetFloatLength(res).DigitsBeforeDecimalLength assert.NoError(t, err) From 05b1bbfcef391ada0ea29bb0f7c2bf30845263d7 Mon Sep 17 00:00:00 2001 From: Evis Drenova Date: Tue, 31 Oct 2023 18:17:08 -0700 Subject: [PATCH 3/4] clean up linting --- worker/internal/benthos/transformers/random_float.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/worker/internal/benthos/transformers/random_float.go b/worker/internal/benthos/transformers/random_float.go index bfa8d8c992..47b9b2791a 100644 --- a/worker/internal/benthos/transformers/random_float.go +++ b/worker/internal/benthos/transformers/random_float.go @@ -9,9 +9,6 @@ import ( _ "github.com/benthosdev/benthos/v4/public/components/io" ) -const defaultLenBeforeDecimals = 2 -const defaultLenAfterDecimals = 3 - func init() { spec := bloblang.NewPluginSpec(). @@ -38,8 +35,6 @@ func init() { } return bloblang.Float64Method(func(i float64) (any, error) { - // res, err := ProcessRandomFloat(i, preserveLength, digitsBeforeDecimal, digitsAfterDecimal) - if preserveLength { res, err := GenerateRandomFloatPreserveLength(i, preserveLength) return res, err From b278bd60f2d95b332e89fe92512dd50948e79b0d Mon Sep 17 00:00:00 2001 From: Evis Drenova Date: Tue, 31 Oct 2023 18:18:25 -0700 Subject: [PATCH 4/4] comments --- worker/internal/benthos/transformers/random_float_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worker/internal/benthos/transformers/random_float_test.go b/worker/internal/benthos/transformers/random_float_test.go index 878a05b0b5..09f3aa7926 100644 --- a/worker/internal/benthos/transformers/random_float_test.go +++ b/worker/internal/benthos/transformers/random_float_test.go @@ -29,7 +29,7 @@ func TestProcessRandomFloatPreserveLengthFalse(t *testing.T) { actual := GetFloatLength(res).DigitsAfterDecimalLength + GetFloatLength(res).DigitsBeforeDecimalLength assert.NoError(t, err) - assert.Equal(t, actual, expectedLength, "The output Float needs to be the same length as the input Float") + 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) {