-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphQLTypeGeneratorTests.cs
More file actions
149 lines (142 loc) · 4.95 KB
/
GraphQLTypeGeneratorTests.cs
File metadata and controls
149 lines (142 loc) · 4.95 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shopify.Types;
using System.Text.Json;
namespace GraphQLSharp.Tests;
[TestClass]
public class GraphQLSharp_Tests
{
[TestMethod]
public void GenerateShopifyTypes()
{
var options = new GraphQLTypeGeneratorOptions
{
NamespaceClient = "Shopify",
NamespaceTypes = "Shopify.Types",
ClientClassName = "ShopifyClient",
ScalarTypeNameToDotNetTypeName = new Dictionary<string, string>
{
{ "UnsignedInt64", "ulong" },
{ "Money", "decimal" },
{ "Float", "decimal" },
{ "Decimal", "decimal" },
{ "DateTime", "DateTime" },
{ "Date", "DateOnly" },
{ "UtcOffset", "TimeSpan" },
{ "URL", "string" },
{ "HTML", "string" },
{ "JSON", "string" },
{ "FormattedString", "string" },
{ "ARN", "string" },
{ "StorefrontID", "string" },
{ "Color", "string" },
{ "BigInt", "long" },
},
GraphQLTypeToTypeNameOverride = new Dictionary<(string, string), string>
{
{ ("ShopifyPaymentsDispute", "evidenceDueBy"), "DateTime" },
{ ("ShopifyPaymentsDispute", "evidenceSentOn"), "DateTime" },
{ ("ShopifyPaymentsDispute", "finalizedOn"), "DateTime" },
},
EnumMembersAsString = true,
};
var generator = new GraphQLTypeGenerator();
var shopifyDoc = JsonDocument.Parse(File.OpenRead(@"./shopify.json"));
var code = generator.GenerateTypes(options, shopifyDoc);
File.WriteAllText("../../../shopify.cs", code);
}
[TestMethod]
public void GenerateSquareTypes()
{
var options = new GraphQLTypeGeneratorOptions
{
NamespaceClient = "Square",
NamespaceTypes = "Square.Types",
ClientClassName = "SquareClient",
ScalarTypeNameToDotNetTypeName = new Dictionary<string, string>
{
{ "Decimal", "decimal" },
{ "DateTime", "DateTime" },
{ "JsonSafeLong", "long" },
//there might be better types for these:
{ "iCalendarEvent", "string" },
{ "HexColor", "string" },
{ "UID", "string" },
{ "Url", "string" },
{ "EmailAddress", "string" },
{ "LanguageCode", "string" },
{ "PhoneNumber", "string" },
{ "TimeZone", "string" },
{ "Duration", "string" },
{ "Cursor", "string" },
}
};
var generator = new GraphQLTypeGenerator();
var squareDoc = JsonDocument.Parse(File.OpenRead(@"./square.json"));
var code = generator.GenerateTypes(options, squareDoc);
File.WriteAllText("../../../square.cs", code);
}
[TestMethod]
public void SerializerMustRoundTrip()
{
var order1 = new Order
{
id = "123",
customer = new Customer
{
id = "234"
},
purchasingEntity = new PurchasingCompany
{
contact = new CompanyContact
{
id = "456"
}
}
};
string json1 = order1.ToJson();
var order2 = Order.FromJson(json1);
var json2 = order2.ToJson();
Assert.AreEqual(json1, json2);
}
[TestMethod]
public void SerializerMustRoundTripArray()
{
var orders1 = new[]
{
new Order
{
id = "123",
customer = new Customer
{
id = "234"
},
purchasingEntity = new PurchasingCompany
{
contact = new CompanyContact
{
id = "456"
}
}
},
new Order
{
id = "abc",
customer = new Customer
{
id = "def"
},
purchasingEntity = new PurchasingCompany
{
contact = new CompanyContact
{
id = "ghi"
}
}
}
};
var json1 = Serializer.Serialize(orders1);
var orders2 = Serializer.Deserialize<Order[]>(json1);
var json2 = Serializer.Serialize(orders2);
Assert.AreEqual(json1, json2);
}
}