forked from google/jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_test.go
More file actions
226 lines (197 loc) · 6.21 KB
/
node_test.go
File metadata and controls
226 lines (197 loc) · 6.21 KB
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
225
226
package jsonapi
import (
"reflect"
"testing"
)
func TestHandleNodeErrors(t *testing.T) {
tests := []struct {
name string
hasNodeError bool
input *Node
expected *Node
}{
{
name: "has no errors",
hasNodeError: false,
input: &Node{Attributes: attributes{"foo": true, "bar": false}},
expected: &Node{Attributes: attributes{"foo": true, "bar": false}},
},
{
name: "has an error",
hasNodeError: true,
input: &Node{Attributes: attributes{"foo": true, "bar": &dominantFieldConflict{key: "bar", vals: []interface{}{true, false}}}},
expected: &Node{Attributes: attributes{"foo": true}},
},
{
name: "has a couple errors",
hasNodeError: true,
input: &Node{
Attributes: attributes{
"foo": true,
"bar": &dominantFieldConflict{key: "bar", vals: []interface{}{true, false}},
"bat": &dominantFieldConflict{key: "bat", vals: []interface{}{true, false}},
},
},
expected: &Node{Attributes: attributes{"foo": true}},
},
}
for _, scenario := range tests {
t.Logf("scenario: %s\n", scenario.name)
if scenario.hasNodeError && reflect.DeepEqual(scenario.input, scenario.expected) {
t.Error("expected input and expected to be different")
}
scenario.input.handleNodeErrors()
if !reflect.DeepEqual(scenario.input, scenario.expected) {
t.Errorf("Got\n%#v\nExpected\n%#v\n", scenario.input, scenario.expected)
}
}
}
func TestSetAttributes(t *testing.T) {
key := "foo"
attr := attributes{}
// set first val
attr.set(key, false)
// check presence of first val
val, ok := attr[key]
if !ok {
t.Errorf("expected attributes to have key: %s\n", key)
}
// assert first val is not an error
_, ok = val.(nodeError)
if ok {
t.Errorf("val stored for key (%s) should NOT be a nodeError\n", key)
}
// add second val; same key
attr.set(key, true)
// assert val converted to an error
_, ok = attr[key].(nodeError)
if !ok {
t.Errorf("val stored for key (%s) should be a nodeError\n", key)
}
// add third val; same key
attr.set(key, nil)
// assert val remains an error
_, ok = attr[key].(nodeError)
if !ok {
t.Errorf("val stored for key (%s) should be a nodeError\n", key)
}
}
func TestMergeNode(t *testing.T) {
parent := &Node{
Type: "Good",
ID: "99",
Attributes: map[string]interface{}{"fizz": "buzz"},
}
child := &Node{
Type: "Better",
ClientID: "1111",
Attributes: map[string]interface{}{"timbuk": 2},
}
expected := &Node{
Type: "Better",
ID: "99",
ClientID: "1111",
Attributes: map[string]interface{}{"fizz": "buzz", "timbuk": 2},
}
parent.merge(child)
if !reflect.DeepEqual(expected, parent) {
t.Errorf("Got %+v Expected %+v", parent, expected)
}
}
func TestCombinePeerNodesNoConflict(t *testing.T) {
brother := &Node{
Type: "brother",
ID: "99",
ClientID: "9999",
Attributes: map[string]interface{}{"fizz": "buzz"},
Relationships: map[string]interface{}{"father": "Joe"},
}
sister := &Node{
Type: "sister",
ID: "11",
ClientID: "1111",
Attributes: map[string]interface{}{"timbuk": 2},
Relationships: map[string]interface{}{"mother": "Mary"},
}
expected := &Node{
Type: "sister",
ID: "11",
ClientID: "1111",
Attributes: map[string]interface{}{"fizz": "buzz", "timbuk": 2},
Relationships: map[string]interface{}{"father": "Joe", "mother": "Mary"},
}
actual := combinePeerNodes([]*Node{brother, sister})
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Got %+v Expected %+v", actual, expected)
}
}
func TestCombinePeerNodesWithConflict(t *testing.T) {
brother := &Node{
Type: "brother",
ID: "99",
ClientID: "9999",
Attributes: map[string]interface{}{"timbuk": 2},
Relationships: map[string]interface{}{"father": "Joe"},
}
sister := &Node{
Type: "sister",
ID: "11",
ClientID: "1111",
Attributes: map[string]interface{}{"timbuk": 2},
Relationships: map[string]interface{}{"mother": "Mary"},
}
expected := &Node{
Type: "sister",
ID: "11",
ClientID: "1111",
Attributes: map[string]interface{}{"timbuk": &dominantFieldConflict{key: "timbuk", vals: []interface{}{2, 2}}},
Relationships: map[string]interface{}{"father": "Joe", "mother": "Mary"},
}
actual := combinePeerNodes([]*Node{brother, sister})
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Got %+v Expected %+v", actual, expected)
}
}
func TestDeepCopyNode(t *testing.T) {
source := &Node{
Type: "thing",
ID: "1",
ClientID: "2",
Attributes: attributes{"key": "val"},
Relationships: map[string]interface{}{"key": "val"},
Links: &Links{"key": "val"},
Meta: &Meta{"key": "val"},
}
badCopy := *source
if !reflect.DeepEqual(badCopy, *source) {
t.Errorf("Got %+v Expected %+v", badCopy, *source)
}
if reflect.ValueOf(badCopy.Attributes).Pointer() != reflect.ValueOf(source.Attributes).Pointer() {
t.Error("Expected map address to be the same")
}
if reflect.ValueOf(badCopy.Relationships).Pointer() != reflect.ValueOf(source.Relationships).Pointer() {
t.Error("Expected map address to be the same")
}
if reflect.ValueOf(badCopy.Links).Pointer() != reflect.ValueOf(source.Links).Pointer() {
t.Error("Expected map address to be the same")
}
if reflect.ValueOf(badCopy.Meta).Pointer() != reflect.ValueOf(source.Meta).Pointer() {
t.Error("Expected map address to be the same")
}
deepCopy := deepCopyNode(source)
if !reflect.DeepEqual(*deepCopy, *source) {
t.Errorf("Got %+v Expected %+v", *deepCopy, *source)
}
if reflect.ValueOf(deepCopy.Attributes).Pointer() == reflect.ValueOf(source.Attributes).Pointer() {
t.Error("Expected map address to be different")
}
if reflect.ValueOf(deepCopy.Relationships).Pointer() == reflect.ValueOf(source.Relationships).Pointer() {
t.Error("Expected map address to be different")
}
if reflect.ValueOf(deepCopy.Links).Pointer() == reflect.ValueOf(source.Links).Pointer() {
t.Error("Expected map address to be different")
}
if reflect.ValueOf(deepCopy.Meta).Pointer() == reflect.ValueOf(source.Meta).Pointer() {
t.Error("Expected map address to be different")
}
}