Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEOS-242: uncommenting test #469

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 59 additions & 60 deletions worker/internal/benthos/transformers/random_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
_ "github.com/benthosdev/benthos/v4/public/components/io"
)

const defaultLenBeforeDecimals = 2
const defaultLenAfterDecimals = 3

func init() {

spec := bloblang.NewPluginSpec().
Expand All @@ -38,8 +35,13 @@
}

return bloblang.Float64Method(func(i float64) (any, error) {
res, err := ProcessRandomFloat(i, preserveLength, digitsBeforeDecimal, digitsAfterDecimal)
return res, err
if preserveLength {
res, err := GenerateRandomFloatPreserveLength(i, preserveLength)
return res, err
} else {
res, err := GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal)
return res, err
}

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

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L42-L44

Added lines #L42 - L44 were not covered by tests
}), nil
})

Expand All @@ -49,90 +51,87 @@

}

// 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")

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L62 was not covered by tests
}

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))
ad, err := GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength))

for {
if !LastDigitAZero(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 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")

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L74 was not covered by tests
}
}

combinedStr := fmt.Sprintf("%d.%d", bd, ad)
if err != nil {
return 0, fmt.Errorf("unable to generate a random after digits integer")
}

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

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L79-L80

Added lines #L79 - L80 were not covered by tests

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")
}

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#L86-L87

Added lines #L86 - L87 were not covered by tests

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

ad, err := GenerateRandomInt(int64(defaultLenAfterDecimals))
return returnValue, nil
}

// 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) {
break // Exit the loop when i is greater than or equal to 5
}
ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength))
func GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal int64) (float64, error) {

if err != nil {
return 0, fmt.Errorf("unable to generate a random int64 to convert to a float")
}
}
var returnValue float64

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

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

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L100-L101

Added lines #L100 - L101 were not covered by tests

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)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L111 was not covered by tests

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")

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L114 was not covered by tests
}
}

combinedStr := fmt.Sprintf("%d.%d", bd, ad)
if err != nil {
return 0, fmt.Errorf("unable to generate a random after digits integer")
}

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

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L119-L120

Added lines #L119 - L120 were not covered by tests

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")
}

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

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/random_float.go#L123-L124

Added lines #L123 - L124 were not covered by tests

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")

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L130 was not covered by tests
}

returnValue = result

return returnValue, nil
}

Expand All @@ -153,7 +152,7 @@
}
}

func LastDigitAZero(n int64) bool {
func isLastDigitAZero(n int64) bool {
// Convert the int64 to a string
str := strconv.FormatInt(n, 10)

Expand Down
18 changes: 8 additions & 10 deletions worker/internal/benthos/transformers/random_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,18 +21,16 @@ func TestProcessRandomFloatPreserveLength(t *testing.T) {

}

//nolint
// func TestProcessRandomFloatPreserveLengthFalse(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)
// 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 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)`
Expand Down