Skip to content

Commit

Permalink
updated int phone number and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 20, 2023
1 parent dc61ecd commit dc1df38
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
49 changes: 37 additions & 12 deletions worker/internal/benthos/transformers/int_phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,34 @@ import (
func init() {

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

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

preserveLength, err := args.GetBool("preserve_length")
numberPtr, err := args.GetOptionalInt64("number")
if err != nil {
return nil, err
}
var number int64
if numberPtr != nil {
number = *numberPtr
}

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

Check warning on line 33 in worker/internal/benthos/transformers/int_phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/int_phone.go#L32-L33

Added lines #L32 - L33 were not covered by tests
var preserveLength bool
if preserveLengthPtr != nil {
preserveLength = *preserveLengthPtr
}

return bloblang.Int64Method(func(s int64) (any, error) {
res, err := GenerateIntPhoneNumber(s, preserveLength)
return func() (any, error) {
res, err := GenerateIntPhoneNumber(number, preserveLength)
return res, err
}), nil
}, nil
})

if err != nil {
Expand All @@ -37,14 +51,25 @@ func init() {
// generates a random phone number and returns it as an int64
func GenerateIntPhoneNumber(number int64, preserveLength bool) (int64, error) {

if preserveLength {
if number != 0 {
if preserveLength {

res, err := GenerateIntPhoneNumberPreserveLength(number)
if err != nil {
return 0, fmt.Errorf("unable to convert phone number string to int64")
}
return res, err
res, err := GenerateIntPhoneNumberPreserveLength(number)
if err != nil {
return 0, fmt.Errorf("unable to convert phone number string to int64")
}

Check warning on line 60 in worker/internal/benthos/transformers/int_phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/int_phone.go#L59-L60

Added lines #L59 - L60 were not covered by tests
return res, err

} else {

Check warning on line 63 in worker/internal/benthos/transformers/int_phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/int_phone.go#L63

Added line #L63 was not covered by tests

res, err := GenerateRandomTenDigitIntPhoneNumber()
if err != nil {
return 0, fmt.Errorf("unable to convert phone number string to int64")
}

Check warning on line 68 in worker/internal/benthos/transformers/int_phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/int_phone.go#L65-L68

Added lines #L65 - L68 were not covered by tests

return res, err

Check warning on line 70 in worker/internal/benthos/transformers/int_phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/int_phone.go#L70

Added line #L70 was not covered by tests

}
} else {

res, err := GenerateRandomTenDigitIntPhoneNumber()
Expand Down
42 changes: 36 additions & 6 deletions worker/internal/benthos/transformers/int_phone_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package neosync_transformers

import (
"fmt"
"strconv"
"strings"
"testing"
Expand All @@ -9,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGenerateIntPhoneNumberPreserveLengthTrue(t *testing.T) {
func Test_GenerateIntPhoneNumberPreserveLengthTrue(t *testing.T) {

pn := int64(618384928322)
expectedLength := 12
Expand All @@ -33,14 +34,13 @@ func TestGenerateIntPhoneNumberPreserveLengthFalse(t *testing.T) {

}

func TestIntPhoneNumberTransformer(t *testing.T) {
mapping := `root = this.intphonetransformer(true)`
func Test_IntPhoneNumberTransformerWithValue(t *testing.T) {
testVal := int64(6183849282)
mapping := fmt.Sprintf(`root = intphonetransformer(%d,true)`, testVal)
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the int64 phone transformer")

testVal := int64(6183849282)

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

numStr := strconv.FormatInt(testVal, 10)
Expand All @@ -49,3 +49,33 @@ func TestIntPhoneNumberTransformer(t *testing.T) {
assert.Equal(t, len(resStr), len(numStr), "Generated phone number must be the same length as the input phone number")
assert.IsType(t, res, testVal)
}

func Test_IntPhoneNumberTransformerWithNoValue(t *testing.T) {
mapping := `root = intphonetransformer()`

ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the int64 phone transformer")

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

resStr := strconv.FormatInt(res.(int64), 10)

assert.Equal(t, len(resStr), 10, "Generated phone number must be the same length as the input phone number")
assert.IsType(t, res, int64(2), "Generated phone number must an int64 type")
}

func Test_IntPhoneNumberTransformerWithZeroValue(t *testing.T) {
mapping := `root = intphonetransformer(0)`

ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the int64 phone transformer")

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

resStr := strconv.FormatInt(res.(int64), 10)

assert.Equal(t, len(resStr), 10, "Generated phone number must be the same length as the input phone number")
assert.IsType(t, res, int64(2), "Generated phone number must an int64 type")
}

0 comments on commit dc1df38

Please sign in to comment.