|
| 1 | +package grpcdatasource |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/tidwall/gjson" |
| 8 | + |
| 9 | + "github.com/wundergraph/astjson" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewEntityIndexMap(t *testing.T) { |
| 13 | + t.Run("returns empty map when no representations match", func(t *testing.T) { |
| 14 | + reps := getRepresentations(gjson.Parse(`{"representations":[ |
| 15 | + {"__typename":"Storage","id":"1"} |
| 16 | + ]}`)) |
| 17 | + idx := newEntityIndexMap("Product", reps) |
| 18 | + assert.Equal(t, entityIndexMap{}, idx) |
| 19 | + }) |
| 20 | + |
| 21 | + t.Run("returns empty map when representations are nil", func(t *testing.T) { |
| 22 | + idx := newEntityIndexMap("Product", nil) |
| 23 | + assert.Equal(t, entityIndexMap{}, idx) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("ordered representations [Product, Product, Storage, Storage]", func(t *testing.T) { |
| 27 | + reps := getRepresentations(gjson.Parse(`{"representations":[ |
| 28 | + {"__typename":"Product","id":"1"}, |
| 29 | + {"__typename":"Product","id":"2"}, |
| 30 | + {"__typename":"Storage","id":"3"}, |
| 31 | + {"__typename":"Storage","id":"4"} |
| 32 | + ]}`)) |
| 33 | + |
| 34 | + productIdx := newEntityIndexMap("Product", reps) |
| 35 | + assert.Equal(t, entityIndexMap{0, 1}, productIdx) |
| 36 | + |
| 37 | + storageIdx := newEntityIndexMap("Storage", reps) |
| 38 | + assert.Equal(t, entityIndexMap{2, 3}, storageIdx) |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("unordered representations [Product, Storage, Product, Storage]", func(t *testing.T) { |
| 42 | + reps := getRepresentations(gjson.Parse(`{"representations":[ |
| 43 | + {"__typename":"Product","id":"1"}, |
| 44 | + {"__typename":"Storage","id":"2"}, |
| 45 | + {"__typename":"Product","id":"3"}, |
| 46 | + {"__typename":"Storage","id":"4"} |
| 47 | + ]}`)) |
| 48 | + |
| 49 | + productIdx := newEntityIndexMap("Product", reps) |
| 50 | + assert.Equal(t, entityIndexMap{0, 2}, productIdx) |
| 51 | + |
| 52 | + storageIdx := newEntityIndexMap("Storage", reps) |
| 53 | + assert.Equal(t, entityIndexMap{1, 3}, storageIdx) |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("interleaved representations across three types", func(t *testing.T) { |
| 57 | + reps := getRepresentations(gjson.Parse(`{"representations":[ |
| 58 | + {"__typename":"Product","id":"1"}, |
| 59 | + {"__typename":"Storage","id":"2"}, |
| 60 | + {"__typename":"Warehouse","id":"3"}, |
| 61 | + {"__typename":"Product","id":"4"}, |
| 62 | + {"__typename":"Warehouse","id":"5"}, |
| 63 | + {"__typename":"Storage","id":"6"} |
| 64 | + ]}`)) |
| 65 | + |
| 66 | + assert.Equal(t, entityIndexMap{0, 3}, newEntityIndexMap("Product", reps)) |
| 67 | + assert.Equal(t, entityIndexMap{1, 5}, newEntityIndexMap("Storage", reps)) |
| 68 | + assert.Equal(t, entityIndexMap{2, 4}, newEntityIndexMap("Warehouse", reps)) |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("single matching representation", func(t *testing.T) { |
| 72 | + reps := getRepresentations(gjson.Parse(`{"representations":[ |
| 73 | + {"__typename":"Storage","id":"1"}, |
| 74 | + {"__typename":"Product","id":"2"}, |
| 75 | + {"__typename":"Storage","id":"3"} |
| 76 | + ]}`)) |
| 77 | + |
| 78 | + assert.Equal(t, entityIndexMap{1}, newEntityIndexMap("Product", reps)) |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("preserves original positions for fully matching list", func(t *testing.T) { |
| 82 | + reps := getRepresentations(gjson.Parse(`{"representations":[ |
| 83 | + {"__typename":"Product","id":"1"}, |
| 84 | + {"__typename":"Product","id":"2"}, |
| 85 | + {"__typename":"Product","id":"3"} |
| 86 | + ]}`)) |
| 87 | + |
| 88 | + assert.Equal(t, entityIndexMap{0, 1, 2}, newEntityIndexMap("Product", reps)) |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("interface entity matches by typename string only", func(t *testing.T) { |
| 92 | + // Interface-entity representations carry the interface name as __typename |
| 93 | + // (e.g. "Resource"). The index map cares only about the typename string, |
| 94 | + // not whether it refers to an interface or a concrete type. |
| 95 | + reps := getRepresentations(gjson.Parse(`{"representations":[ |
| 96 | + {"__typename":"Resource","id":"1"}, |
| 97 | + {"__typename":"Product","id":"2"}, |
| 98 | + {"__typename":"Resource","id":"3"}, |
| 99 | + {"__typename":"Storage","id":"4"}, |
| 100 | + {"__typename":"Resource","id":"5"} |
| 101 | + ]}`)) |
| 102 | + |
| 103 | + assert.Equal(t, entityIndexMap{0, 2, 4}, newEntityIndexMap("Resource", reps)) |
| 104 | + // Concrete types in the same list are independent. |
| 105 | + assert.Equal(t, entityIndexMap{1}, newEntityIndexMap("Product", reps)) |
| 106 | + assert.Equal(t, entityIndexMap{3}, newEntityIndexMap("Storage", reps)) |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +func TestGetRepresentations(t *testing.T) { |
| 111 | + t.Run("returns nil when representations key missing", func(t *testing.T) { |
| 112 | + vars := gjson.Parse(`{"other":"value"}`) |
| 113 | + assert.Nil(t, getRepresentations(vars)) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("returns empty slice when representations is empty array", func(t *testing.T) { |
| 117 | + vars := gjson.Parse(`{"representations":[]}`) |
| 118 | + reps := getRepresentations(vars) |
| 119 | + assert.NotNil(t, reps) |
| 120 | + assert.Empty(t, reps) |
| 121 | + }) |
| 122 | + |
| 123 | + t.Run("returns representations when present", func(t *testing.T) { |
| 124 | + vars := gjson.Parse(`{"representations":[{"__typename":"Product","id":"1"},{"__typename":"Storage","id":"2"}]}`) |
| 125 | + reps := getRepresentations(vars) |
| 126 | + assert.Len(t, reps, 2) |
| 127 | + assert.Equal(t, "Product", reps[0].Get("__typename").String()) |
| 128 | + assert.Equal(t, "Storage", reps[1].Get("__typename").String()) |
| 129 | + }) |
| 130 | +} |
| 131 | +func TestValidateEntityResponse(t *testing.T) { |
| 132 | + reps := getRepresentations(gjson.Parse(`{"representations":[ |
| 133 | + {"__typename":"Product","id":"1"}, |
| 134 | + {"__typename":"Product","id":"2"} |
| 135 | + ]}`)) |
| 136 | + |
| 137 | + t.Run("returns error when data is nil", func(t *testing.T) { |
| 138 | + err := validateEntityResponse(nil, "Product", reps) |
| 139 | + assert.ErrorContains(t, err, "validateEntityResponse: subgraph response data is nil") |
| 140 | + }) |
| 141 | + |
| 142 | + t.Run("returns error when requested entity type is empty", func(t *testing.T) { |
| 143 | + data := astjson.MustParse(`{"_entities":[]}`) |
| 144 | + err := validateEntityResponse(data, "", reps) |
| 145 | + assert.ErrorContains(t, err, "validateEntityResponse: requested entity type is empty") |
| 146 | + }) |
| 147 | + |
| 148 | + t.Run("returns error when representations are empty", func(t *testing.T) { |
| 149 | + data := astjson.MustParse(`{"_entities":[]}`) |
| 150 | + err := validateEntityResponse(data, "Product", nil) |
| 151 | + assert.ErrorContains(t, err, "validateEntityResponse: no entity representations provided") |
| 152 | + }) |
| 153 | + |
| 154 | + t.Run("returns error when entity count mismatches representation count", func(t *testing.T) { |
| 155 | + data := astjson.MustParse(`{"_entities":[{"__typename":"Product","id":"1"}]}`) |
| 156 | + err := validateEntityResponse(data, "Product", reps) |
| 157 | + assert.ErrorContains(t, err, "entity type Product received 1 entities in the subgraph response, but 2 are expected") |
| 158 | + }) |
| 159 | + |
| 160 | + t.Run("returns nil when entity count matches representation count", func(t *testing.T) { |
| 161 | + data := astjson.MustParse(`{"_entities":[{"__typename":"Product","id":"1"},{"__typename":"Product","id":"2"}]}`) |
| 162 | + assert.NoError(t, validateEntityResponse(data, "Product", reps)) |
| 163 | + }) |
| 164 | + |
| 165 | + t.Run("counts only representations of the requested type", func(t *testing.T) { |
| 166 | + mixedReps := getRepresentations(gjson.Parse(`{"representations":[ |
| 167 | + {"__typename":"Product","id":"1"}, |
| 168 | + {"__typename":"Storage","id":"2"}, |
| 169 | + {"__typename":"Product","id":"3"} |
| 170 | + ]}`)) |
| 171 | + data := astjson.MustParse(`{"_entities":[{"__typename":"Product","id":"1"},{"__typename":"Product","id":"3"}]}`) |
| 172 | + assert.NoError(t, validateEntityResponse(data, "Product", mixedReps)) |
| 173 | + }) |
| 174 | + |
| 175 | + t.Run("returns error when _entities key is missing", func(t *testing.T) { |
| 176 | + data := astjson.MustParse(`{}`) |
| 177 | + err := validateEntityResponse(data, "Product", reps) |
| 178 | + assert.ErrorContains(t, err, "entity type Product received 0 entities in the subgraph response, but 2 are expected") |
| 179 | + }) |
| 180 | + |
| 181 | + t.Run("returns error when _entities path is not an array", func(t *testing.T) { |
| 182 | + data := astjson.MustParse(`{"_entities":"not an array"}`) |
| 183 | + err := validateEntityResponse(data, "Product", reps) |
| 184 | + assert.Error(t, err) |
| 185 | + }) |
| 186 | +} |
0 commit comments