|
| 1 | +// Copyright 2026 Democratized Data Foundation |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package client |
| 12 | + |
| 13 | +import ( |
| 14 | + "encoding/json" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +// GQLResult does not use the default struct marshalling. It copies itself field by |
| 21 | +// field through a private mirror, so a field can exist on the type and still be missing |
| 22 | +// from the JSON. These tests catch that. |
| 23 | +// |
| 24 | +// It matters because the Go client never serializes anything. A half applied change |
| 25 | +// passes there and fails on every other client. |
| 26 | + |
| 27 | +func TestGQLResultMarshal_WithWarning_RoundTrips(t *testing.T) { |
| 28 | + input := GQLResult{ |
| 29 | + Data: map[string]any{"Users": []any{}}, |
| 30 | + Extensions: &GQLExtensions{ |
| 31 | + Warnings: []GQLWarning{ |
| 32 | + { |
| 33 | + Code: "test_warning", |
| 34 | + Message: "something worth knowing happened", |
| 35 | + Detail: map[string]any{"requested": 10, "returned": 6}, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + data, err := json.Marshal(input) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + var output GQLResult |
| 45 | + err = json.Unmarshal(data, &output) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + require.NotNil(t, output.Extensions) |
| 49 | + require.Len(t, output.Extensions.Warnings, 1) |
| 50 | + |
| 51 | + warning := output.Extensions.Warnings[0] |
| 52 | + require.Equal(t, "test_warning", warning.Code) |
| 53 | + require.Equal(t, "something worth knowing happened", warning.Message) |
| 54 | + |
| 55 | + // UnmarshalJSON calls dec.UseNumber, so numbers come back as json.Number, not |
| 56 | + // float64. Same as everything under `data`. |
| 57 | + require.Equal(t, json.Number("10"), warning.Detail["requested"]) |
| 58 | + require.Equal(t, json.Number("6"), warning.Detail["returned"]) |
| 59 | +} |
| 60 | + |
| 61 | +func TestGQLResultMarshal_WithMultipleWarnings_PreservesOrder(t *testing.T) { |
| 62 | + input := GQLResult{ |
| 63 | + Extensions: &GQLExtensions{ |
| 64 | + Warnings: []GQLWarning{ |
| 65 | + {Code: "first", Message: "one"}, |
| 66 | + {Code: "second", Message: "two"}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + data, err := json.Marshal(input) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + var output GQLResult |
| 75 | + err = json.Unmarshal(data, &output) |
| 76 | + require.NoError(t, err) |
| 77 | + |
| 78 | + require.Len(t, output.Extensions.Warnings, 2) |
| 79 | + require.Equal(t, "first", output.Extensions.Warnings[0].Code) |
| 80 | + require.Equal(t, "second", output.Extensions.Warnings[1].Code) |
| 81 | +} |
| 82 | + |
| 83 | +func TestGQLResultMarshal_WithoutExtensions_OmitsField(t *testing.T) { |
| 84 | + input := GQLResult{Data: map[string]any{"Users": []any{}}} |
| 85 | + |
| 86 | + data, err := json.Marshal(input) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + var raw map[string]json.RawMessage |
| 90 | + err = json.Unmarshal(data, &raw) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + require.NotContains(t, raw, "extensions") |
| 94 | +} |
| 95 | + |
| 96 | +func TestGQLResultMarshal_WithEmptyExtensions_OmitsField(t *testing.T) { |
| 97 | + // An empty value must not be sent as `"extensions":{}`. Otherwise every response |
| 98 | + // changes shape as soon as anything allocates an accumulator. |
| 99 | + input := GQLResult{ |
| 100 | + Data: map[string]any{"Users": []any{}}, |
| 101 | + Extensions: &GQLExtensions{}, |
| 102 | + } |
| 103 | + |
| 104 | + data, err := json.Marshal(input) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + var raw map[string]json.RawMessage |
| 108 | + err = json.Unmarshal(data, &raw) |
| 109 | + require.NoError(t, err) |
| 110 | + |
| 111 | + require.NotContains(t, raw, "extensions") |
| 112 | +} |
| 113 | + |
| 114 | +func TestGQLResultUnmarshal_WithUnknownExtensionKey_IsIgnored(t *testing.T) { |
| 115 | + // An older client must ignore an entry it does not know about instead of failing |
| 116 | + // the whole response. |
| 117 | + data := []byte(`{ |
| 118 | + "data": null, |
| 119 | + "extensions": { |
| 120 | + "warnings": [{"code": "known", "message": "hi", "unknownField": 1}], |
| 121 | + "unknownKey": {"anything": true} |
| 122 | + } |
| 123 | + }`) |
| 124 | + |
| 125 | + var output GQLResult |
| 126 | + err := json.Unmarshal(data, &output) |
| 127 | + require.NoError(t, err) |
| 128 | + |
| 129 | + require.NotNil(t, output.Extensions) |
| 130 | + require.Len(t, output.Extensions.Warnings, 1) |
| 131 | + require.Equal(t, "known", output.Extensions.Warnings[0].Code) |
| 132 | +} |
| 133 | + |
| 134 | +func TestGQLResultUnmarshal_WithoutExtensions_LeavesNil(t *testing.T) { |
| 135 | + var output GQLResult |
| 136 | + err := json.Unmarshal([]byte(`{"data": null}`), &output) |
| 137 | + require.NoError(t, err) |
| 138 | + |
| 139 | + require.Nil(t, output.Extensions) |
| 140 | + require.True(t, output.Extensions.IsEmpty()) |
| 141 | +} |
| 142 | + |
| 143 | +// The empty non nil slice is the case a field by field check gets wrong: the slice is |
| 144 | +// not the zero value, but it still encodes to nothing. |
| 145 | +func TestGQLExtensionsIsEmpty_WithEmptyWarningSlice_IsEmpty(t *testing.T) { |
| 146 | + extensions := &GQLExtensions{Warnings: []GQLWarning{}} |
| 147 | + |
| 148 | + require.True(t, extensions.IsEmpty()) |
| 149 | +} |
| 150 | + |
| 151 | +func TestGQLExtensionsIsEmpty_WithNilReceiver_IsEmpty(t *testing.T) { |
| 152 | + var extensions *GQLExtensions |
| 153 | + |
| 154 | + require.True(t, extensions.IsEmpty()) |
| 155 | +} |
| 156 | + |
| 157 | +func TestGQLExtensionsIsEmpty_WithAWarning_IsNotEmpty(t *testing.T) { |
| 158 | + extensions := &GQLExtensions{Warnings: []GQLWarning{{Code: "test_warning"}}} |
| 159 | + |
| 160 | + require.False(t, extensions.IsEmpty()) |
| 161 | +} |
| 162 | + |
| 163 | +// A peer may send an empty extensions object. Callers are told the field is nil when |
| 164 | +// there is nothing to report, so decoding must make that true rather than hand back a |
| 165 | +// non nil value with nothing in it. |
| 166 | +func TestGQLResultUnmarshal_WithEmptyExtensions_LeavesNil(t *testing.T) { |
| 167 | + var output GQLResult |
| 168 | + err := json.Unmarshal([]byte(`{"data": null, "extensions": {}}`), &output) |
| 169 | + require.NoError(t, err) |
| 170 | + |
| 171 | + require.Nil(t, output.Extensions) |
| 172 | +} |
| 173 | + |
| 174 | +func TestGQLResultUnmarshal_WithOnlyUnknownExtensionKeys_LeavesNil(t *testing.T) { |
| 175 | + var output GQLResult |
| 176 | + err := json.Unmarshal([]byte(`{"data": null, "extensions": {"unknownKey": 1}}`), &output) |
| 177 | + require.NoError(t, err) |
| 178 | + |
| 179 | + require.Nil(t, output.Extensions) |
| 180 | +} |
0 commit comments