-
Notifications
You must be signed in to change notification settings - Fork 1
/
flatten_test.go
46 lines (40 loc) · 1.33 KB
/
flatten_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
package unflatten
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestFlattenConfig(t *testing.T) {
assert := require.New(t)
var expectedJSON = `{
"Connections.Accepted": 4,
"Connections.Open": 2,
"Memory.Alloc": 682208,
"Memory.Frees": 2567,
"Memory.Lookups": 281,
"Memory.Mallocs": 3326,
"Memory.Sys": 54417,
"Memory.TotalAlloc": 10324,
"Peers.IPv6.Completed": 0,
"Peers.IPv6.Current": 0,
"Peers.IPv6.Joined": 0,
"Peers.IPv6.Left": 0,
"Peers.IPv6.Reaped": 0,
"Peers.IPv6.Seeds.Current": 0,
"Peers.IPv6.Seeds.Joined": 0,
"Peers.IPv6.Seeds.Left": 0,
"Peers.IPv6.Seeds.Reaped": 0,
"ResponseTime.P50": 0.045775,
"ResponseTime.P90": 0.074299,
"ResponseTime.P95": 0.096207
}`
var fromJSON = `{"Connections":{"Accepted":4,"Open":2},"Memory":{"Alloc":682208,"Frees":2567,"Lookups":281,"Mallocs":3326,"Sys":54417,"TotalAlloc":10324},"Peers":{"IPv6":{"Completed":0,"Current":0,"Joined":0,"Left":0,"Reaped":0,"Seeds":{"Current":0,"Joined":0,"Left":0,"Reaped":0}}},"ResponseTime":{"P50":0.045775,"P90":0.074299,"P95":0.096207}}`
config := make(map[string]interface{})
err := json.Unmarshal([]byte(fromJSON), &config)
assert.NoError(err)
tree := Flatten(config, JoinWithDot)
payload, _ := json.MarshalIndent(tree, "", "")
if string(payload) != expectedJSON {
t.Errorf("expected %s got %s", expectedJSON, string(payload))
}
}