-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdriver_parsing_test.go
119 lines (115 loc) · 3.21 KB
/
driver_parsing_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package godynamo
import (
"github.com/btnguyen2k/consu/reddo"
"os"
"reflect"
"testing"
)
func Test_parseConnString_parseParamValue(t *testing.T) {
testName := "Test_parseConnString_parseParamValue"
type paramValueStruct struct {
pnames []string
enames []string
paramType reflect.Type
defaultValue interface{}
expectedValue interface{}
validator func(val interface{}) bool
}
_ = os.Setenv("ENV_TIMEOUT", "1234")
_ = os.Setenv("ENV_TIMEOUT_INVALID", "-123")
testCases := []struct {
name string
connStr string
expectedParams map[string]string
paramValues []paramValueStruct
}{
{
name: "endpoint",
connStr: "endpoint=http://localhost:8000",
expectedParams: map[string]string{
"ENDPOINT": "http://localhost:8000",
},
paramValues: []paramValueStruct{
{
pnames: []string{"EP", "ENDPOINT"},
paramType: reddo.TypeString,
expectedValue: "http://localhost:8000",
},
{
pnames: []string{"TIMEOUT"},
paramType: reddo.TypeInt,
defaultValue: int64(123),
expectedValue: int64(123),
},
{
pnames: []string{"TIMEOUT_ENV"},
enames: []string{"ENV_TIMEOUT"},
validator: func(val interface{}) bool {
return val.(int64) >= 0
},
paramType: reddo.TypeInt,
expectedValue: int64(1234),
},
{
pnames: []string{"TIMEOUT_DEFAULT"},
enames: []string{"ENV_TIMEOUT_INVALID"},
validator: func(val interface{}) bool {
return val.(int64) >= 0
},
paramType: reddo.TypeInt,
defaultValue: int64(12345),
expectedValue: int64(12345),
},
},
},
{
name: "empty",
connStr: "endpoint=http://localhost:8000;timeout",
expectedParams: map[string]string{
"ENDPOINT": "http://localhost:8000",
"TIMEOUT": "",
},
paramValues: []paramValueStruct{
{
pnames: []string{"T", "TIMEOUT"},
paramType: reddo.TypeInt,
defaultValue: int64(1234),
expectedValue: int64(1234),
},
},
},
{
name: "invalid_timeout_value",
connStr: "endpoint=http://localhost:8000;timeout=-1",
expectedParams: map[string]string{
"ENDPOINT": "http://localhost:8000",
"TIMEOUT": "-1",
},
paramValues: []paramValueStruct{
{
pnames: []string{"T", "TIMEOUT"},
paramType: reddo.TypeInt,
defaultValue: int64(1234),
expectedValue: int64(1234),
validator: func(val interface{}) bool {
return val.(int64) >= 0
},
},
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
params := parseConnString(testCase.connStr)
if !reflect.DeepEqual(params, testCase.expectedParams) {
t.Fatalf("%s failed: expected %#v received %#v", testName+"/"+testCase.name, testCase.expectedParams, params)
}
for _, paramValue := range testCase.paramValues {
val := parseParamValue(params, paramValue.paramType, paramValue.validator, paramValue.defaultValue, paramValue.pnames, paramValue.enames)
if !reflect.DeepEqual(val, paramValue.expectedValue) {
t.Fatalf("%s failed: <%s> expected %#v received %#v", testName+"/"+testCase.name, paramValue.pnames, paramValue.expectedValue, val)
}
}
})
}
}