Skip to content

Commit 0504bfb

Browse files
committed
Add test for deserializing SubscriptionContract union types
1 parent 67c2658 commit 0504bfb

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

ShopifySharp.Tests/Services/Graph/GraphServiceTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
using ShopifySharp.Infrastructure;
1111
using ShopifySharp.Infrastructure.Serialization.Http;
1212
using ShopifySharp.Credentials;
13+
using ShopifySharp.GraphQL;
14+
using ShopifySharp.Infrastructure.Serialization.Json;
1315
using ShopifySharp.Tests.TestClasses;
1416
using Xunit;
17+
using Serializer = ShopifySharp.Infrastructure.Serializer;
1518

1619
namespace ShopifySharp.Tests.Services.Graph;
1720

@@ -76,6 +79,7 @@ public async Task WhenSendingGraphRequest_ShouldUseHttpContentSerializer()
7679
// Setup
7780
var request = new GraphRequest
7881
{
82+
//language=txt
7983
Query = "some-graph-request-query",
8084
Variables = new Dictionary<string, object>
8185
{
@@ -92,4 +96,83 @@ public async Task WhenSendingGraphRequest_ShouldUseHttpContentSerializer()
9296
// Assert
9397
await act.Should().ThrowAsync<TestException>();
9498
}
99+
100+
[Fact]
101+
public async Task WhenDeserializingUnionTypesForSubscriptionContract_ShouldNotThrow()
102+
{
103+
const string json =
104+
"""
105+
{
106+
"data": {
107+
"subscriptionContract": {
108+
"id": "gid://shopify/SubscriptionContract/some-contract-id",
109+
"discounts": {
110+
"edges": [
111+
{
112+
"node": {
113+
"id": "gid://shopify/SubscriptionManualDiscount/some-discount-id",
114+
"value": {
115+
"__typename": "SubscriptionDiscountPercentageValue",
116+
"percentage": 10
117+
}
118+
}
119+
}
120+
]
121+
},
122+
"customerPaymentMethod": {
123+
"id": "gid://shopify/CustomerPaymentMethod/some-payment-id",
124+
"revokedReason": "PAYMENT_METHOD_VERIFICATION_FAILED",
125+
"revokedAt": null,
126+
"instrument": {
127+
"__typename": "CustomerCreditCard",
128+
"brand": "visa",
129+
"expiresSoon": false,
130+
"expiryMonth": 12,
131+
"expiryYear": 2025,
132+
"lastDigits": "4242",
133+
"isRevocable": false,
134+
"maskedNumber": "•••• •••• •••• 4242",
135+
"name": "name",
136+
"source": "credit_card"
137+
}
138+
}
139+
}
140+
}
141+
}
142+
""";
143+
144+
// Act
145+
var document = JsonDocument.Parse(json);
146+
var element = new SystemJsonElement(document.RootElement.GetProperty("data"));
147+
var systemJsonSerializer = new SystemJsonSerializer(Serializer.GraphSerializerOptions);
148+
var result = await systemJsonSerializer.DeserializeAsync<GetSubscriptionContractResult>(element);
149+
150+
// Assert
151+
result.Should().NotBeNull();
152+
result.subscriptionContract.id.Should().Be("gid://shopify/SubscriptionContract/some-contract-id");
153+
154+
result.subscriptionContract.customerPaymentMethod.Should().NotBeNull();
155+
result.subscriptionContract.customerPaymentMethod!.id.Should().Be("gid://shopify/CustomerPaymentMethod/some-payment-id");
156+
result.subscriptionContract.customerPaymentMethod!.revokedReason.Should().Be(CustomerPaymentMethodRevocationReason.PAYMENT_METHOD_VERIFICATION_FAILED);
157+
158+
var instrument = result.subscriptionContract.customerPaymentMethod.instrument!.AsCustomerCreditCard();
159+
instrument.Should().NotBeNull()
160+
.And.BeOfType<CustomerCreditCard>()
161+
.Which
162+
.Should()
163+
.BeEquivalentTo(new CustomerCreditCard
164+
{
165+
brand = "visa",
166+
expiresSoon = false,
167+
expiryMonth = 12,
168+
expiryYear = 2025,
169+
lastDigits = "4242",
170+
isRevocable = false,
171+
maskedNumber = "•••• •••• •••• 4242",
172+
name = "name",
173+
source = "credit_card"
174+
});
175+
}
176+
177+
public record GetSubscriptionContractResult(SubscriptionContract subscriptionContract);
95178
}

0 commit comments

Comments
 (0)