-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjson_test.go
224 lines (216 loc) · 4.4 KB
/
json_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package utils
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)
type TestStruct struct {
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Email string `json:"email,omitempty"`
Country string `json:"country,omitempty"`
}
func TestStructToMap(t *testing.T) {
testCases := []struct {
name string
input interface{}
expectedMap map[string]any
expectError bool
}{
{
name: "Struct with string fields",
input: TestStruct{
Name: "John Doe",
Age: 30,
Email: "john.doe@example.com",
Country: "USA",
},
expectedMap: map[string]any{
"name": "John Doe",
"age": float64(30),
"email": "john.doe@example.com",
"country": "USA",
},
expectError: false,
},
{
name: "Empty struct",
input: TestStruct{},
expectedMap: map[string]any{},
expectError: false,
},
{
name: "Non-serializable input",
input: make(chan int),
expectedMap: nil,
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resultMap, err := ToJSONMap(tc.input)
if tc.expectError {
if err == nil {
t.Errorf("Expected an error, but got nil")
}
} else {
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
if diff := cmp.Diff(resultMap, tc.expectedMap); diff != "" {
t.Errorf("Result map differs from expected: %v", diff)
}
})
}
}
func Test_ExtractChangedPaths(t *testing.T) {
type args struct {
data string
paths []string
}
tests := []struct {
name string
args args
}{
{
name: "single child on all levels",
args: args{
data: `
{
"address": {
"city": {
"name": "Kathmandu"
}
}
}`,
paths: []string{"address.city.name"},
},
},
{
name: "multiple children on some level",
args: args{
data: `
{
"address": {
"city": {
"name": "Imadol",
"district": "Lalitpur"
}
}
}`,
paths: []string{"address.city"},
},
},
{
name: "multiple children on some level",
args: args{
data: `
{
"address": {
"city": {
"name": "Imadol",
"district": "Lalitpur",
"block": {
"section": "b",
"name": "Sagarmatha Tol"
}
}
}
}`,
paths: []string{"address.city", "address.city.block"},
},
},
{
name: "multiple top level children",
args: args{
data: `
{
"metadata": {
"annotations": {
"control-plane.alpha.kubernetes.io/leader": "{\"holderIdentity\":\"ip-172-16-56-162.eu-west-2.compute.internal\",\"leaseDurationSeconds\":30,\"acquireTime\":\"2023-03-07T10:13:03Z\",\"renewTime\":\"2023-03-16T05:10:21Z\",\"leaderTransitions\":25}"
},
"resourceVersion": "483339358"
}
}`,
paths: []string{
"metadata.resourceVersion",
"metadata.annotations.control-plane.alpha.kubernetes.io/leader",
},
},
},
{
name: "a child with an array",
args: args{
data: `
{
"status": {
"conditions": [
{
"type": "Ready",
"reason": "ChartPullFailed",
"status": "False",
"message": "no chart version found for mysql-8.8.8",
"lastTransitionTime": "2023-03-16T04:47:24.000Z"
}
]
},
"metadata": {
"resourceVersion": "483324452"
}
}`,
paths: []string{
"status.conditions",
"metadata.resourceVersion",
},
},
},
{
name: "deeply nested",
args: args{
data: `
{
"a": {
"b": {
"c": {
"d": {
"e": {
"f": 1,
"g": 2
}
}
},
"h": 3,
"i": {
"j": {
"k": 4
}
}
}
},
"metadata": {
"resourceVersion": "483324452"
}
}
`,
paths: []string{
"a.b.c.d.e",
"a.b.h",
"a.b.i.j.k",
"metadata.resourceVersion",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var m map[string]any
if err := json.Unmarshal([]byte(tt.args.data), &m); err != nil {
t.Fatalf("Failed to unmarshal data: %v", err)
}
paths := ExtractLeafNodesAndCommonParents(m)
assert.ElementsMatch(t, tt.args.paths, paths)
})
}
}