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