-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
InputFieldDescriptorTests.cs
254 lines (216 loc) · 8.03 KB
/
InputFieldDescriptorTests.cs
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
using System;
using System.Collections.Generic;
using HotChocolate.Language;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using Xunit;
namespace HotChocolate.Types
{
public class InputFieldDescriptorTests
: DescriptorTestBase
{
[Fact]
public void DotNetTypesDoNotOverwriteSchemaTypes()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor
.Type<ListType<StringType>>()
.Type<NativeType<IReadOnlyDictionary<string, string>>>();
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
ITypeReference typeRef = description.Type;
Assert.Equal(
typeof(ListType<StringType>),
Assert.IsType<ExtendedTypeReference>(typeRef).Type.Source);
}
[Fact]
public void SchemaTypesOverwriteDotNetTypes()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor
.Type<NativeType<IReadOnlyDictionary<string, string>>>()
.Type<ListType<StringType>>();
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
ITypeReference typeRef = description.Type;
Assert.Equal(
typeof(ListType<StringType>),
Assert.IsType<ExtendedTypeReference>(typeRef).Type.Source);
}
[Fact]
public void SetSchemaType()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor.Type(new StringType());
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
ITypeReference typeRef = description.Type;
Assert.IsType<StringType>(
Assert.IsType<SchemaTypeReference>(typeRef).Type);
}
[Fact]
public void SetGenericType()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor.Type<StringType>();
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
ITypeReference typeRef = description.Type;
Assert.Equal(
typeof(StringType),
Assert.IsType<ExtendedTypeReference>(typeRef).Type.Source);
}
[Fact]
public void SetNonGenericType()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor.Type(typeof(StringType));
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
ITypeReference typeRef = description.Type;
Assert.Equal(
typeof(StringType),
Assert.IsType<ExtendedTypeReference>(typeRef).Type.Source);
}
[Fact]
public void OverwriteName()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
"field1234");
// act
descriptor.Name("args");
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
Assert.Equal("args", description.Name);
}
[Fact]
public void OverwriteName2()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor.Name("args");
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
Assert.Equal("args", description.Name);
}
[Fact]
public void SetDescription()
{
// arrange
string expectedDescription = Guid.NewGuid().ToString();
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor.Description(expectedDescription);
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
Assert.Equal(expectedDescription, description.Description);
}
[Fact]
public void SetDefaultValueAndInferType()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor.DefaultValue("string");
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
Assert.Equal(
typeof(string),
Assert.IsType<ExtendedTypeReference>(description.Type).Type.Source);
Assert.Equal("string", description.NativeDefaultValue);
}
[Fact]
public void OverwriteDefaultValueLiteralWithNativeDefaultValue()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
descriptor
.DefaultValue(new StringValueNode("123"))
.DefaultValue("string");
// asser
InputFieldDefinition description = descriptor.CreateDefinition();
Assert.Null(description.DefaultValue);
Assert.Equal("string", description.NativeDefaultValue);
}
[Fact]
public void SettingTheNativeDefaultValueToNullCreatesNullLiteral()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
((IInputFieldDescriptor)descriptor)
.DefaultValue(new StringValueNode("123"))
.DefaultValue("string")
.DefaultValue(null);
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
Assert.IsType<NullValueNode>(description.DefaultValue);
Assert.Null(description.NativeDefaultValue);
}
[Fact]
public void OverwriteNativeDefaultValueWithDefaultValueLiteral()
{
// arrange
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// act
((IInputFieldDescriptor)descriptor)
.DefaultValue("string")
.DefaultValue(new StringValueNode("123"));
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
Assert.IsType<StringValueNode>(description.DefaultValue);
Assert.Equal("123",
((StringValueNode)description.DefaultValue).Value);
Assert.Null(description.NativeDefaultValue);
}
[Fact]
public void InferTypeFromProperty()
{
// act
var descriptor = InputFieldDescriptor.New(
Context,
typeof(ObjectField).GetProperty("Arguments"));
// assert
InputFieldDefinition description = descriptor.CreateDefinition();
Assert.Equal(typeof(FieldCollection<Argument>),
Assert.IsType<ExtendedTypeReference>(description.Type).Type.Source);
Assert.Equal("arguments", description.Name);
}
}
}