-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathentity_test.go
More file actions
185 lines (155 loc) · 7.11 KB
/
entity_test.go
File metadata and controls
185 lines (155 loc) · 7.11 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
package grpcdatasource
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/wundergraph/astjson"
)
func TestNewEntityIndexMap(t *testing.T) {
t.Run("returns empty map when no representations match", func(t *testing.T) {
reps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Storage","id":"1"}
]}`))
idx := newEntityIndexMap("Product", reps)
assert.Equal(t, entityIndexMap{}, idx)
})
t.Run("returns empty map when representations are nil", func(t *testing.T) {
idx := newEntityIndexMap("Product", nil)
assert.Equal(t, entityIndexMap{}, idx)
})
t.Run("ordered representations [Product, Product, Storage, Storage]", func(t *testing.T) {
reps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Product","id":"1"},
{"__typename":"Product","id":"2"},
{"__typename":"Storage","id":"3"},
{"__typename":"Storage","id":"4"}
]}`))
productIdx := newEntityIndexMap("Product", reps)
assert.Equal(t, entityIndexMap{0, 1}, productIdx)
storageIdx := newEntityIndexMap("Storage", reps)
assert.Equal(t, entityIndexMap{2, 3}, storageIdx)
})
t.Run("unordered representations [Product, Storage, Product, Storage]", func(t *testing.T) {
reps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Product","id":"1"},
{"__typename":"Storage","id":"2"},
{"__typename":"Product","id":"3"},
{"__typename":"Storage","id":"4"}
]}`))
productIdx := newEntityIndexMap("Product", reps)
assert.Equal(t, entityIndexMap{0, 2}, productIdx)
storageIdx := newEntityIndexMap("Storage", reps)
assert.Equal(t, entityIndexMap{1, 3}, storageIdx)
})
t.Run("interleaved representations across three types", func(t *testing.T) {
reps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Product","id":"1"},
{"__typename":"Storage","id":"2"},
{"__typename":"Warehouse","id":"3"},
{"__typename":"Product","id":"4"},
{"__typename":"Warehouse","id":"5"},
{"__typename":"Storage","id":"6"}
]}`))
assert.Equal(t, entityIndexMap{0, 3}, newEntityIndexMap("Product", reps))
assert.Equal(t, entityIndexMap{1, 5}, newEntityIndexMap("Storage", reps))
assert.Equal(t, entityIndexMap{2, 4}, newEntityIndexMap("Warehouse", reps))
})
t.Run("single matching representation", func(t *testing.T) {
reps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Storage","id":"1"},
{"__typename":"Product","id":"2"},
{"__typename":"Storage","id":"3"}
]}`))
assert.Equal(t, entityIndexMap{1}, newEntityIndexMap("Product", reps))
})
t.Run("preserves original positions for fully matching list", func(t *testing.T) {
reps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Product","id":"1"},
{"__typename":"Product","id":"2"},
{"__typename":"Product","id":"3"}
]}`))
assert.Equal(t, entityIndexMap{0, 1, 2}, newEntityIndexMap("Product", reps))
})
t.Run("interface entity matches by typename string only", func(t *testing.T) {
// Interface-entity representations carry the interface name as __typename
// (e.g. "Resource"). The index map cares only about the typename string,
// not whether it refers to an interface or a concrete type.
reps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Resource","id":"1"},
{"__typename":"Product","id":"2"},
{"__typename":"Resource","id":"3"},
{"__typename":"Storage","id":"4"},
{"__typename":"Resource","id":"5"}
]}`))
assert.Equal(t, entityIndexMap{0, 2, 4}, newEntityIndexMap("Resource", reps))
// Concrete types in the same list are independent.
assert.Equal(t, entityIndexMap{1}, newEntityIndexMap("Product", reps))
assert.Equal(t, entityIndexMap{3}, newEntityIndexMap("Storage", reps))
})
}
func TestGetRepresentations(t *testing.T) {
t.Run("returns nil when representations key missing", func(t *testing.T) {
vars := astjson.MustParse(`{"other":"value"}`)
assert.Nil(t, getRepresentations(vars))
})
t.Run("returns empty slice when representations is empty array", func(t *testing.T) {
vars := astjson.MustParse(`{"representations":[]}`)
reps := getRepresentations(vars)
assert.NotNil(t, reps)
assert.Empty(t, reps)
})
t.Run("returns representations when present", func(t *testing.T) {
vars := astjson.MustParse(`{"representations":[{"__typename":"Product","id":"1"},{"__typename":"Storage","id":"2"}]}`)
reps := getRepresentations(vars)
assert.Len(t, reps, 2)
assert.Equal(t, "Product", string(reps[0].Get("__typename").GetStringBytes()))
assert.Equal(t, "Storage", string(reps[1].Get("__typename").GetStringBytes()))
})
}
func TestValidateEntityResponse(t *testing.T) {
reps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Product","id":"1"},
{"__typename":"Product","id":"2"}
]}`))
t.Run("returns error when data is nil", func(t *testing.T) {
err := validateEntityResponse(nil, "Product", reps)
assert.ErrorContains(t, err, "validateEntityResponse: subgraph response data is nil")
})
t.Run("returns error when requested entity type is empty", func(t *testing.T) {
data := astjson.MustParse(`{"_entities":[]}`)
err := validateEntityResponse(data, "", reps)
assert.ErrorContains(t, err, "validateEntityResponse: requested entity type is empty")
})
t.Run("returns error when representations are empty", func(t *testing.T) {
data := astjson.MustParse(`{"_entities":[]}`)
err := validateEntityResponse(data, "Product", nil)
assert.ErrorContains(t, err, "validateEntityResponse: no entity representations provided")
})
t.Run("returns error when entity count mismatches representation count", func(t *testing.T) {
data := astjson.MustParse(`{"_entities":[{"__typename":"Product","id":"1"}]}`)
err := validateEntityResponse(data, "Product", reps)
assert.ErrorContains(t, err, "entity type Product received 1 entities in the subgraph response, but 2 are expected")
})
t.Run("returns nil when entity count matches representation count", func(t *testing.T) {
data := astjson.MustParse(`{"_entities":[{"__typename":"Product","id":"1"},{"__typename":"Product","id":"2"}]}`)
assert.NoError(t, validateEntityResponse(data, "Product", reps))
})
t.Run("counts only representations of the requested type", func(t *testing.T) {
mixedReps := getRepresentations(astjson.MustParse(`{"representations":[
{"__typename":"Product","id":"1"},
{"__typename":"Storage","id":"2"},
{"__typename":"Product","id":"3"}
]}`))
data := astjson.MustParse(`{"_entities":[{"__typename":"Product","id":"1"},{"__typename":"Product","id":"3"}]}`)
assert.NoError(t, validateEntityResponse(data, "Product", mixedReps))
})
t.Run("returns error when _entities key is missing", func(t *testing.T) {
data := astjson.MustParse(`{}`)
err := validateEntityResponse(data, "Product", reps)
assert.ErrorContains(t, err, "entity type Product received 0 entities in the subgraph response, but 2 are expected")
})
t.Run("returns error when _entities path is not an array", func(t *testing.T) {
data := astjson.MustParse(`{"_entities":"not an array"}`)
err := validateEntityResponse(data, "Product", reps)
assert.Error(t, err)
})
}