-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
110 lines (103 loc) · 3.38 KB
/
node_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
package map2struct
import (
"encoding/json"
"fmt"
"log"
"testing"
"reflect"
)
var randomRubbishJSON = `{
"_id": "617c120ab7a3fa5dd968346d",
"index": 0,
"guid": "f3a4d826-30d6-4811-ba08-cda75b221f2b",
"isActive": true,
"balance": "$1,241.70",
"picture": "http://placehold.it/32x32",
"age": 37,
"eyeColor": "brown",
"name": "Susie Noble",
"gender": "female",
"company": "ZILLA",
"email": "[email protected]",
"phone": "+1 (925) 507-2761",
"address": "266 Legion Street, Cressey, Illinois, 8982",
"about": "Aliquip ut veniam mollit duis non minim ad amet est ex id fugiat consequat. Amet labore nulla esse adipisicing velit fugiat aute fugiat in Lorem aliquip et exercitation. Proident laborum aliqua laborum esse Lorem minim. Velit do aute cillum laborum sit excepteur minim fugiat cillum laborum ex enim. Consequat minim nisi adipisicing eu deserunt esse id id anim esse consequat sunt cillum deserunt. Sit commodo aute adipisicing irure nulla in aliquip pariatur eiusmod consectetur velit. Ea anim nulla cillum eu fugiat.\r\n",
"registered": "2017-08-29T12:01:21 -01:00",
"latitude": -39.228822,
"longitude": 136.128045,
"tags": [
"consequat",
"magna",
"exercitation",
"eu",
"minim",
"duis",
"qui"
],
"friends": [
{
"id": 0,
"name": "Diane Martinez"
},
{
"id": 1,
"name": "Keller Schneider"
},
{
"id": 2,
"name": "Imogene Kemp"
}
],
"greeting": "Hello, Susie Noble! You have 7 unread messages.",
"favoriteFruit": "strawberry"
}`
func TestDecode(T *testing.T) {
testOutput := []string{
"Susie Noble,friends,0,Diane Martinez,consequat",
"Susie Noble,friends,1,Keller Schneider,consequat",
"Susie Noble,friends,2,Imogene Kemp,consequat",
"Susie Noble,friends,0,Diane Martinez,magna",
"Susie Noble,friends,1,Keller Schneider,magna",
"Susie Noble,friends,2,Imogene Kemp,magna",
"Susie Noble,friends,0,Diane Martinez,exercitation",
"Susie Noble,friends,1,Keller Schneider,exercitation",
"Susie Noble,friends,2,Imogene Kemp,exercitation",
"Susie Noble,friends,0,Diane Martinez,eu",
"Susie Noble,friends,1,Keller Schneider,eu",
"Susie Noble,friends,2,Imogene Kemp,eu",
"Susie Noble,friends,0,Diane Martinez,minim",
"Susie Noble,friends,1,Keller Schneider,minim",
"Susie Noble,friends,2,Imogene Kemp,minim",
"Susie Noble,friends,0,Diane Martinez,duis",
"Susie Noble,friends,1,Keller Schneider,duis",
"Susie Noble,friends,2,Imogene Kemp,duis",
"Susie Noble,friends,0,Diane Martinez,qui",
"Susie Noble,friends,1,Keller Schneider,qui",
"Susie Noble,friends,2,Imogene Kemp,qui",
}
exampleInput := make(map[string]interface{}) // using JSON as an example dataset - we really want map[string]interface{}
err := json.Unmarshal([]byte(randomRubbishJSON), &exampleInput)
if err != nil {
log.Println(err)
T.Fail()
}
root := Node{}
root.Ingest(exampleInput)
friends := root.Get("friends") // get a list of the friends structs
tags := root.Array("tags") // grab the tags array
actualOutput := []string{}
for tagindex := range tags {
for friendindex := range friends {
out := fmt.Sprintf("%s,%s,%s,%s,%s", root.Field("name"),
friends[friendindex].Name, friends[friendindex].Field("id"),
friends[friendindex].Field("name"), tags[tagindex])
actualOutput = append(actualOutput, out)
}
}
for _, v := range actualOutput {
log.Println(v)
}
if !reflect.DeepEqual(actualOutput, testOutput) {
T.Fail()
}
}