|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+package config_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "encoding/json"
|
|
|
5
|
+ "strconv"
|
|
|
6
|
+ "testing"
|
|
|
7
|
+
|
|
|
8
|
+ "github.com/9seconds/mtg/v2/config"
|
|
|
9
|
+ "github.com/stretchr/testify/assert"
|
|
|
10
|
+ "github.com/stretchr/testify/suite"
|
|
|
11
|
+)
|
|
|
12
|
+
|
|
|
13
|
+type typeErrorRateTestStruct struct {
|
|
|
14
|
+ Value config.TypeErrorRate `json:"value"`
|
|
|
15
|
+}
|
|
|
16
|
+
|
|
|
17
|
+type TypeErrorRateTestSuite struct {
|
|
|
18
|
+ suite.Suite
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
|
21
|
+func (suite *TypeErrorRateTestSuite) TestUnmarshalFail() {
|
|
|
22
|
+ testData := []float64{
|
|
|
23
|
+ 1000,
|
|
|
24
|
+ -100,
|
|
|
25
|
+ -0.0001,
|
|
|
26
|
+ }
|
|
|
27
|
+
|
|
|
28
|
+ for _, v := range testData {
|
|
|
29
|
+ data, err := json.Marshal(map[string]float64{
|
|
|
30
|
+ "value": v,
|
|
|
31
|
+ })
|
|
|
32
|
+ suite.NoError(err)
|
|
|
33
|
+
|
|
|
34
|
+ suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
|
|
|
35
|
+ assert.Error(t, json.Unmarshal(data, &typeErrorRateTestStruct{}))
|
|
|
36
|
+ })
|
|
|
37
|
+ }
|
|
|
38
|
+}
|
|
|
39
|
+
|
|
|
40
|
+func (suite *TypeErrorRateTestSuite) TestUnmarshalOk() {
|
|
|
41
|
+ testData := []float64{
|
|
|
42
|
+ 1,
|
|
|
43
|
+ 55.5,
|
|
|
44
|
+ 0.0001,
|
|
|
45
|
+ 1e-6,
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ for _, v := range testData {
|
|
|
49
|
+ value := v
|
|
|
50
|
+
|
|
|
51
|
+ data, err := json.Marshal(map[string]float64{
|
|
|
52
|
+ "value": v,
|
|
|
53
|
+ })
|
|
|
54
|
+ suite.NoError(err)
|
|
|
55
|
+
|
|
|
56
|
+ suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
|
|
|
57
|
+ testStruct := &typeErrorRateTestStruct{}
|
|
|
58
|
+
|
|
|
59
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
60
|
+ assert.InEpsilon(t, value, testStruct.Value.Value(0), 1e-10)
|
|
|
61
|
+ })
|
|
|
62
|
+ }
|
|
|
63
|
+}
|
|
|
64
|
+
|
|
|
65
|
+func (suite *TypeErrorRateTestSuite) TestMarshalOk() {
|
|
|
66
|
+ testData := []float64{
|
|
|
67
|
+ 1,
|
|
|
68
|
+ 55.5,
|
|
|
69
|
+ 0.0001,
|
|
|
70
|
+ 1e-6,
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ for _, v := range testData {
|
|
|
74
|
+ value := v
|
|
|
75
|
+
|
|
|
76
|
+ data, err := json.Marshal(map[string]float64{
|
|
|
77
|
+ "value": v,
|
|
|
78
|
+ })
|
|
|
79
|
+ suite.NoError(err)
|
|
|
80
|
+
|
|
|
81
|
+ suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
|
|
|
82
|
+ testStruct := &typeErrorRateTestStruct{}
|
|
|
83
|
+
|
|
|
84
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
85
|
+
|
|
|
86
|
+ parsed, err := strconv.ParseFloat(testStruct.Value.String(), 64)
|
|
|
87
|
+ assert.NoError(t, err)
|
|
|
88
|
+ assert.InEpsilon(t, value, parsed, 1e-10)
|
|
|
89
|
+
|
|
|
90
|
+ marshalled, err := testStruct.Value.MarshalText()
|
|
|
91
|
+ assert.NoError(t, err)
|
|
|
92
|
+
|
|
|
93
|
+ parsed, err = strconv.ParseFloat(string(marshalled), 64)
|
|
|
94
|
+ assert.NoError(t, err)
|
|
|
95
|
+ assert.InEpsilon(t, value, parsed, 1e-10)
|
|
|
96
|
+ })
|
|
|
97
|
+ }
|
|
|
98
|
+}
|
|
|
99
|
+
|
|
|
100
|
+func (suite *TypeErrorRateTestSuite) TestValue() {
|
|
|
101
|
+ testStruct := &typeErrorRateTestStruct{}
|
|
|
102
|
+
|
|
|
103
|
+ suite.InEpsilon(1, testStruct.Value.Value(1), 1e-10)
|
|
|
104
|
+ suite.InEpsilon(2, testStruct.Value.Value(2), 1e-10)
|
|
|
105
|
+
|
|
|
106
|
+ data, err := json.Marshal(map[string]float64{
|
|
|
107
|
+ "value": 1,
|
|
|
108
|
+ })
|
|
|
109
|
+ suite.NoError(err)
|
|
|
110
|
+ suite.NoError(json.Unmarshal(data, testStruct))
|
|
|
111
|
+
|
|
|
112
|
+ suite.InEpsilon(1, testStruct.Value.Value(2), 1e-10)
|
|
|
113
|
+ suite.InEpsilon(1, testStruct.Value.Value(3), 1e-10)
|
|
|
114
|
+}
|
|
|
115
|
+
|
|
|
116
|
+func TestTypeErrorRate(t *testing.T) {
|
|
|
117
|
+ t.Parallel()
|
|
|
118
|
+ suite.Run(t, &TypeErrorRateTestSuite{})
|
|
|
119
|
+}
|