-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconf_test.go
112 lines (105 loc) · 1.99 KB
/
conf_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
package goworld
import (
"testing"
)
var (
testConfig string = `
{
"server": {
"port": 4000,
"protocols": [
{
"name": "list",
"enabled": true
},
{
"name": "search",
"enabled": false
},
{
"name": "hotel",
"enabled": true,
"params": [
{
"name": "name",
"type": "string"
}
],
"results": [
{
"name": "name",
"type": "string"
},
{
"name": "address1",
"type": "string"
}
]
}
]
},
"workers": [
{
"port": 4002,
"host": "localhost",
"name": "w1"
},
{
"port": 4001,
"host": "localhost",
"name": "w2"
}
]
}
`
)
func unm() (*Config, error) {
b := []byte(testConfig)
return unmarshal(b)
}
func TestUnmarshal(t *testing.T) {
c, err := unm()
if err != nil || c == nil {
t.Errorf("Error parsing config: %v\n", err)
}
}
func TestReadConfig(t *testing.T) {
c, _ := unm()
if c.Server.Port != 4000 {
t.Errorf("Wrong number of server port \n")
}
if len(c.Workers) != 2 {
t.Errorf("Wring number of workers")
}
}
func TestGetProtocol(t *testing.T) {
c, _ := unm()
if c.GetProtocolDef("list") == nil {
t.Errorf("Error getting protocol list")
}
}
func TestReadProtocol(t *testing.T) {
c, _ := unm()
if len(c.Server.Protocols) != 3 {
t.Errorf("Wrong number of protocols")
}
for _, prot := range c.Server.Protocols {
switch prot.Name {
case "list":
if !prot.Enabled {
t.Errorf("Wrong value for protocol '%s' field Enabled\n", prot.Name)
}
case "search":
if prot.Enabled {
t.Errorf("Wrong value for protocol '%s' field Enabled\n", prot.Name)
}
case "hotel":
if len(prot.Params) != 1 {
t.Errorf("Wrong no of params for protocol '%s' \n", prot.Name)
}
if len(prot.Results) != 2 {
t.Errorf("Wrong no of result fields for protocol '%s' \n", prot.Name)
}
}
}
}