Add tests for config error rate type

This commit is contained in:
9seconds
2021-03-11 21:30:43 +03:00
parent d48e82be7c
commit 5c4536c591
3 changed files with 130 additions and 11 deletions
+3 -3
View File
@@ -22,9 +22,9 @@ type Config struct {
AllowSkewness TypeDuration `json:"allow-skewness"` AllowSkewness TypeDuration `json:"allow-skewness"`
} `json:"time"` } `json:"time"`
AntiReplay struct { AntiReplay struct {
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
MaxSize TypeBytes `json:"max-size"` MaxSize TypeBytes `json:"max-size"`
ErrorRate TypeFloat `json:"error-rate"` ErrorRate TypeErrorRate `json:"error-rate"`
} `json:"anti-replay"` } `json:"anti-replay"`
} `json:"probes"` } `json:"probes"`
Network struct { Network struct {
@@ -5,18 +5,18 @@ import (
"strconv" "strconv"
) )
type TypeFloat struct { type TypeErrorRate struct {
value float64 value float64
} }
func (c *TypeFloat) UnmarshalJSON(data []byte) error { func (c *TypeErrorRate) UnmarshalJSON(data []byte) error {
value, err := strconv.ParseFloat(string(data), 64) value, err := strconv.ParseFloat(string(data), 64)
if err != nil { if err != nil {
return fmt.Errorf("incorrect float value: %w", err) return fmt.Errorf("incorrect float value: %w", err)
} }
if value < 0 { if value <= 0 || value >= 100 {
return fmt.Errorf("%f should be positive", value) return fmt.Errorf("%f should be 0 < x < 100", value)
} }
c.value = value c.value = value
@@ -24,16 +24,16 @@ func (c *TypeFloat) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func (c *TypeFloat) MarshalText() ([]byte, error) { func (c *TypeErrorRate) MarshalText() ([]byte, error) {
return []byte(c.String()), nil return []byte(c.String()), nil
} }
func (c TypeFloat) String() string { func (c TypeErrorRate) String() string {
return strconv.FormatFloat(c.value, 'f', -1, 64) return strconv.FormatFloat(c.value, 'f', -1, 64)
} }
func (c TypeFloat) Value(defaultValue float64) float64 { func (c TypeErrorRate) Value(defaultValue float64) float64 {
if c.value < 0.00001 { if c.value < 1e-8 {
return defaultValue return defaultValue
} }
+119
View File
@@ -0,0 +1,119 @@
package config_test
import (
"encoding/json"
"strconv"
"testing"
"github.com/9seconds/mtg/v2/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type typeErrorRateTestStruct struct {
Value config.TypeErrorRate `json:"value"`
}
type TypeErrorRateTestSuite struct {
suite.Suite
}
func (suite *TypeErrorRateTestSuite) TestUnmarshalFail() {
testData := []float64{
1000,
-100,
-0.0001,
}
for _, v := range testData {
data, err := json.Marshal(map[string]float64{
"value": v,
})
suite.NoError(err)
suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
assert.Error(t, json.Unmarshal(data, &typeErrorRateTestStruct{}))
})
}
}
func (suite *TypeErrorRateTestSuite) TestUnmarshalOk() {
testData := []float64{
1,
55.5,
0.0001,
1e-6,
}
for _, v := range testData {
value := v
data, err := json.Marshal(map[string]float64{
"value": v,
})
suite.NoError(err)
suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
testStruct := &typeErrorRateTestStruct{}
assert.NoError(t, json.Unmarshal(data, testStruct))
assert.InEpsilon(t, value, testStruct.Value.Value(0), 1e-10)
})
}
}
func (suite *TypeErrorRateTestSuite) TestMarshalOk() {
testData := []float64{
1,
55.5,
0.0001,
1e-6,
}
for _, v := range testData {
value := v
data, err := json.Marshal(map[string]float64{
"value": v,
})
suite.NoError(err)
suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
testStruct := &typeErrorRateTestStruct{}
assert.NoError(t, json.Unmarshal(data, testStruct))
parsed, err := strconv.ParseFloat(testStruct.Value.String(), 64)
assert.NoError(t, err)
assert.InEpsilon(t, value, parsed, 1e-10)
marshalled, err := testStruct.Value.MarshalText()
assert.NoError(t, err)
parsed, err = strconv.ParseFloat(string(marshalled), 64)
assert.NoError(t, err)
assert.InEpsilon(t, value, parsed, 1e-10)
})
}
}
func (suite *TypeErrorRateTestSuite) TestValue() {
testStruct := &typeErrorRateTestStruct{}
suite.InEpsilon(1, testStruct.Value.Value(1), 1e-10)
suite.InEpsilon(2, testStruct.Value.Value(2), 1e-10)
data, err := json.Marshal(map[string]float64{
"value": 1,
})
suite.NoError(err)
suite.NoError(json.Unmarshal(data, testStruct))
suite.InEpsilon(1, testStruct.Value.Value(2), 1e-10)
suite.InEpsilon(1, testStruct.Value.Value(3), 1e-10)
}
func TestTypeErrorRate(t *testing.T) {
t.Parallel()
suite.Run(t, &TypeErrorRateTestSuite{})
}