diff --git a/worker/internal/benthos/transformers/random_bool_test.go b/worker/internal/benthos/transformers/random_bool_test.go index 06d15e2271..27a832c6d4 100644 --- a/worker/internal/benthos/transformers/random_bool_test.go +++ b/worker/internal/benthos/transformers/random_bool_test.go @@ -9,12 +9,10 @@ import ( func TestProcessRandomBool(t *testing.T) { - testVal := false // check bool gen - res, err := GenerateRandomBool() assert.NoError(t, err) - assert.IsType(t, res, testVal) + assert.IsType(t, res, false) } diff --git a/worker/internal/benthos/transformers/random_float.go b/worker/internal/benthos/transformers/random_float.go index 16c0e3e866..5a94d550e8 100644 --- a/worker/internal/benthos/transformers/random_float.go +++ b/worker/internal/benthos/transformers/random_float.go @@ -66,7 +66,7 @@ func GenerateRandomFloatPreserveLength(i float64, preserveLength bool) (float64, ad, err := transformer_utils.GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) for { - if !isLastDigitAZero(ad) { + if !transformer_utils.IsLastDigitZero(ad) { break // Exit the loop when i is greater than or equal to 5 } ad, err = transformer_utils.GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) @@ -106,7 +106,7 @@ func GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecima // 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) { + if !transformer_utils.IsLastDigitZero(ad) { break // Exit the loop when i is greater than or equal to 5 } ad, err = transformer_utils.GenerateRandomInt(digitsAfterDecimal) @@ -152,15 +152,3 @@ func GetFloatLength(i float64) *FloatLength { DigitsAfterDecimalLength: len(parsed[1]), } } - -func isLastDigitAZero(n int64) bool { - // Convert the int64 to a string - str := strconv.FormatInt(n, 10) - - // Check if the string is empty or if the last character is '0' - if len(str) > 0 && str[len(str)-1] == '0' { - return true - } - - return false -} diff --git a/worker/internal/benthos/transformers/utils/utils.go b/worker/internal/benthos/transformers/utils/utils.go index a98a36e93c..0e20cbe1c4 100644 --- a/worker/internal/benthos/transformers/utils/utils.go +++ b/worker/internal/benthos/transformers/utils/utils.go @@ -135,3 +135,15 @@ func GetIntLength(i int64) int64 { return length } + +func IsLastDigitZero(n int64) bool { + // Convert the int64 to a string + str := strconv.FormatInt(n, 10) + + // Check if the string is empty or if the last character is '0' + if len(str) > 0 && str[len(str)-1] == '0' { + return true + } + + return false +} diff --git a/worker/internal/benthos/transformers/utils/utils_test.go b/worker/internal/benthos/transformers/utils/utils_test.go index 0e0c3acd34..a6669e345f 100644 --- a/worker/internal/benthos/transformers/utils/utils_test.go +++ b/worker/internal/benthos/transformers/utils/utils_test.go @@ -128,3 +128,19 @@ func TestGetIntLegth(t *testing.T) { assert.Equal(t, int64(expected), val, "The calculated length should match the expected length.") } + +func TestIsLastDigitZeroTrue(t *testing.T) { + + value := int64(954670) + + res := IsLastDigitZero(value) + assert.Equal(t, res, true, "The last digit is zero.") +} + +func TestIsLastDigitZeroFalse(t *testing.T) { + + value := int64(23546789) + + res := IsLastDigitZero(value) + assert.Equal(t, res, false, "The last digit is not zero.") +}