-
Notifications
You must be signed in to change notification settings - Fork 13
/
scalar_test.go
275 lines (257 loc) · 7.13 KB
/
scalar_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package opslevel_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/hasura/go-graphql-client"
ol "github.com/opslevel/opslevel-go/v2024"
"github.com/rocktavious/autopilot/v2023"
)
type IDTester struct {
Key1 ol.ID `json:"key1"`
Key2 ol.ID `json:"key2,omitempty"`
Key3 *ol.ID `json:"key3"`
Key4 *ol.ID `json:"key4,omitempty"`
}
func TestMarshalID(t *testing.T) {
// Arrange
id1 := ol.NewID()
id2 := ol.NewID("Z2lkOi8vMTIzNDU2Nzg5")
case1 := IDTester{}
case2 := IDTester{
Key1: *id1,
Key2: *id1,
Key3: id1,
Key4: id1,
}
case3 := IDTester{
Key1: *id2,
Key2: *id2,
Key3: id2,
Key4: id2,
}
// Act
buf1, err1 := json.Marshal(case1)
buf2, err2 := json.Marshal(case2)
buf3, err3 := json.Marshal(case3)
// Assert
autopilot.Ok(t, err1)
autopilot.Equals(t, `{"key1":"","key3":null}`, string(buf1))
autopilot.Ok(t, err2)
autopilot.Equals(t, `{"key1":"","key3":null,"key4":null}`, string(buf2))
autopilot.Ok(t, err3)
autopilot.Equals(t, `{"key1":"Z2lkOi8vMTIzNDU2Nzg5","key2":"Z2lkOi8vMTIzNDU2Nzg5","key3":"Z2lkOi8vMTIzNDU2Nzg5","key4":"Z2lkOi8vMTIzNDU2Nzg5"}`, string(buf3))
}
func TestConstructQueryID(t *testing.T) {
// Arrange
id := ol.ID("1234")
var q struct {
Account struct {
Output struct {
Id ol.ID `graphql:"id"`
} `graphql:"myQuery(id1: $id1 id2: $id2)"`
}
}
v := ol.PayloadVariables{
"id1": id,
"id2": &id,
}
// Act
query, err := graphql.ConstructQuery(q, v, ol.WithName("MyQuery"))
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, `query MyQuery($id1:ID!$id2:ID){account{myQuery(id1: $id1 id2: $id2){id}}}`, query)
}
func TestConstructMutationID(t *testing.T) {
// Arrange
id := ol.ID("1234")
var q struct {
Account struct {
Output struct {
Id ol.ID `graphql:"id"`
} `graphql:"myMutation(id1: $id1 id2: $id2)"`
}
}
v := ol.PayloadVariables{
"id1": id,
"id2": &id,
}
// Act
query, err := graphql.ConstructMutation(q, v, ol.WithName("MyMutation"))
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, `mutation MyMutation($id1:ID!$id2:ID){account{myMutation(id1: $id1 id2: $id2){id}}}`, query)
}
func TestMarshalIdentifiers(t *testing.T) {
type TestCase struct {
Name string
Identifier *ol.IdentifierInput
OutputBuffer string
}
testCases := []TestCase{
{
Name: "empty identifier",
Identifier: ol.NewIdentifier(),
OutputBuffer: `null`,
},
{
Name: "identifier with empty arg",
Identifier: ol.NewIdentifier(""),
OutputBuffer: `{"alias":""}`,
},
{
Name: "identifier with valid ID",
Identifier: ol.NewIdentifier("Z2lkOi8vb3BzbGV2ZWwvSGVsbG9Xb3JsZC8xMDEw"),
OutputBuffer: `{"id":"Z2lkOi8vb3BzbGV2ZWwvSGVsbG9Xb3JsZC8xMDEw"}`,
},
{
Name: "identifier with valid alias",
Identifier: ol.NewIdentifier("hello_world"),
OutputBuffer: `{"alias":"hello_world"}`,
},
}
for _, testCase := range testCases {
buf, err := json.Marshal(testCase.Identifier)
autopilot.Ok(t, err)
autopilot.Equals(t, testCase.OutputBuffer, string(buf))
}
}
func TestMarshalIdentifiersOmitBehavior(t *testing.T) {
type TestCase struct {
Name string
Owner *ol.IdentifierInput
Maintainer *ol.IdentifierInput
OutputBuffer string
}
testCases := []TestCase{
{
Name: "pass nil, owner should omitempty",
Owner: nil,
Maintainer: nil,
OutputBuffer: `{"maintainer":null}`,
},
{
Name: "pass empty identifier, owner should null",
Owner: ol.NewIdentifier(),
Maintainer: ol.NewIdentifier(),
OutputBuffer: `{"owner":null,"maintainer":null}`,
},
{
Name: "pass normal identifiers",
Owner: ol.NewIdentifier("Z2lkOi8vb3BzbGV2ZWwvSGVsbG9Xb3JsZC8xMDEw"),
Maintainer: ol.NewIdentifier("team2"),
OutputBuffer: `{"owner":{"id":"Z2lkOi8vb3BzbGV2ZWwvSGVsbG9Xb3JsZC8xMDEw"},"maintainer":{"alias":"team2"}}`,
},
}
for _, testCase := range testCases {
type SomethingUpdateInput struct {
Owner *ol.IdentifierInput `json:"owner,omitempty"`
Maintainer *ol.IdentifierInput `json:"maintainer"`
}
input := SomethingUpdateInput{
Owner: testCase.Owner,
Maintainer: testCase.Maintainer,
}
buf, err := json.Marshal(input)
autopilot.Ok(t, err)
autopilot.Equals(t, testCase.OutputBuffer, string(buf))
}
}
func TestConstructQueryIdentifier(t *testing.T) {
// Arrange
id1 := ol.NewIdentifier("my-service")
id2 := ol.NewIdentifier("Z2lkOi8vMTIzNDU2Nzg5")
var q struct {
Account struct {
Output ol.Identifier `graphql:"myQuery(id1: $id1 id2: $id2 id3: $id3 id4: $id4)"`
}
}
v := ol.PayloadVariables{
"id1": *id1,
"id2": *id2,
"id3": id1,
"id4": id2,
}
// Act
query, err := graphql.ConstructQuery(q, v, ol.WithName("MyQuery"))
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, `query MyQuery($id1:IdentifierInput!$id2:IdentifierInput!$id3:IdentifierInput$id4:IdentifierInput){account{myQuery(id1: $id1 id2: $id2 id3: $id3 id4: $id4){id,aliases}}}`, query)
}
func TestConstructMutationIdentifier(t *testing.T) {
// Arrange
id1 := ol.NewIdentifier("my-service")
id2 := ol.NewIdentifier("Z2lkOi8vMTIzNDU2Nzg5")
var q struct {
Account struct {
Output ol.Identifier `graphql:"myMutation(id1: $id1 id2: $id2 id3: $id3 id4: $id4)"`
}
}
v := ol.PayloadVariables{
"id1": *id1,
"id2": *id2,
"id3": id1,
"id4": id2,
}
// Act
query, err := graphql.ConstructMutation(q, v, ol.WithName("MyMutation"))
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, `mutation MyMutation($id1:IdentifierInput!$id2:IdentifierInput!$id3:IdentifierInput$id4:IdentifierInput){account{myMutation(id1: $id1 id2: $id2 id3: $id3 id4: $id4){id,aliases}}}`, query)
}
func TestNewIdentifierArray(t *testing.T) {
// Arrange
s := []string{"my-service", "Z2lkOi8vMTIzNDU2Nzg5"}
result := ol.NewIdentifierArray(s)
// Assert
autopilot.Equals(t, "my-service", *result[0].Alias)
autopilot.Equals(t, ol.ID("Z2lkOi8vMTIzNDU2Nzg5"), *result[1].Id)
}
func TestNewNullString(t *testing.T) {
buf, err := json.Marshal(ol.NewNull[string]())
if err != nil {
t.Errorf("got unexpected error: '%+v'", err)
}
if string(buf) != "null" {
t.Errorf("null value on this type did not marshal to null, got: '%s'", string(buf))
}
}
func TestNewNullableWithValueString(t *testing.T) {
type TestCase struct {
Name string
Value any
OutputBuffer string
}
testCases := []TestCase{
{
Name: "empty string using constructor",
Value: ol.NewNullableFrom(""),
OutputBuffer: `""`,
},
{
Name: "hello world string using constructor",
Value: ol.NewNullableFrom("hello world"),
OutputBuffer: `"hello world"`,
},
{
Name: "a valid string but with set null = true",
Value: &ol.Nullable[string]{
Value: "abc123",
SetNull: true,
},
OutputBuffer: `null`,
},
}
for _, testCase := range testCases {
testName := fmt.Sprintf("%s with input: %+v", testCase.Name, testCase.Value)
t.Run(testName, func(t *testing.T) {
buf, err := json.Marshal(testCase.Value)
if err != nil {
t.Errorf("got unexpected error: '%+v'", err)
}
if string(buf) != testCase.OutputBuffer {
t.Errorf("got unexpected output: '%s' (expected '%s')", string(buf), testCase.OutputBuffer)
}
})
}
}