Skip to content

Commit

Permalink
fix: fixed NoiseInt transformer. Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
wwoytenko committed Oct 13, 2024
1 parent ccb47c0 commit 8883889
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
16 changes: 8 additions & 8 deletions internal/db/postgres/transformers/noise_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ func NewNoiseIntTransformer(ctx context.Context, driver *toolkit.Driver, paramet
}
}

limiter, limitsWarnings, err := validateIntTypeAndSetNoiseInt64Limiter(intSize, minValueThreshold, maxValueThreshold)
if err != nil {
return nil, nil, err
}
if limitsWarnings.IsFatal() {
return nil, limitsWarnings, nil
}

if err := minRatioParam.Scan(&minRatio); err != nil {
return nil, nil, fmt.Errorf("unable to scan \"min_ratio\" param: %w", err)
}
Expand All @@ -146,14 +154,6 @@ func NewNoiseIntTransformer(ctx context.Context, driver *toolkit.Driver, paramet
return nil, nil, fmt.Errorf("unable to scan \"max_ratio\" param: %w", err)
}

limiter, limitsWarnings, err := validateIntTypeAndSetNoiseInt64Limiter(intSize, maxValueThreshold, minValueThreshold)
if err != nil {
return nil, nil, err
}
if limitsWarnings.IsFatal() {
return nil, limitsWarnings, nil
}

t, err := transformers.NewNoiseInt64Transformer(limiter, minRatio, maxRatio)
if err != nil {
return nil, nil, fmt.Errorf("error initializing common int transformer: %w", err)
Expand Down
58 changes: 43 additions & 15 deletions internal/db/postgres/transformers/noise_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package transformers

import (
"context"
"fmt"
"testing"

"github.com/rs/zerolog/log"
Expand All @@ -38,43 +37,72 @@ func TestNoiseIntTransformer_Transform(t *testing.T) {
// Positive cases
tests := []struct {
name string
ratio float64
columnName string
params map[string]toolkit.ParamsValue
originalValue string
result result
}{
{
name: "int2",
columnName: "id2",
ratio: 0.9,
name: "int2",
columnName: "id2",
params: map[string]toolkit.ParamsValue{
"min_ratio": toolkit.ParamsValue("0.2"),
"max_ratio": toolkit.ParamsValue("0.9"),
},
result: result{min: 12, max: 234},
originalValue: "123",
},
{
name: "int4",
columnName: "id4",
ratio: 0.9,
name: "int4",
columnName: "id4",
params: map[string]toolkit.ParamsValue{
"min_ratio": toolkit.ParamsValue("0.2"),
"max_ratio": toolkit.ParamsValue("0.9"),
},
result: result{min: 12, max: 234},
originalValue: "123",
},
{
name: "int8",
columnName: "id8",
ratio: 0.9,
name: "int8",
columnName: "id8",
params: map[string]toolkit.ParamsValue{
"min_ratio": toolkit.ParamsValue("0.2"),
"max_ratio": toolkit.ParamsValue("0.9"),
},
result: result{min: 12, max: 234},
originalValue: "123",
},
{
name: "int8",
columnName: "id8",
params: map[string]toolkit.ParamsValue{
"min_ratio": toolkit.ParamsValue("0.2"),
"max_ratio": toolkit.ParamsValue("0.9"),
},
result: result{min: 12, max: 234},
originalValue: "123",
},
{
name: "int8",
columnName: "id8",
params: map[string]toolkit.ParamsValue{
"min_ratio": toolkit.ParamsValue("0.2"),
"max_ratio": toolkit.ParamsValue("0.9"),
"min": toolkit.ParamsValue("0"),
"max": toolkit.ParamsValue("110"),
},
result: result{min: 0, max: 110},
originalValue: "123",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.params["column"] = toolkit.ParamsValue(tt.columnName)
driver, record := getDriverAndRecord(tt.columnName, tt.originalValue)
transformerCtx, warnings, err := NoiseIntTransformerDefinition.Instance(
context.Background(),
driver, map[string]toolkit.ParamsValue{
"column": toolkit.ParamsValue(tt.columnName),
"min_ratio": toolkit.ParamsValue(fmt.Sprintf("%f", tt.ratio)),
},
driver, tt.params,
nil,
)
require.NoError(t, err)
Expand Down

0 comments on commit 8883889

Please sign in to comment.