Skip to content

Commit f886c93

Browse files
committed
chore: add unit test for exceptions thrown during final deserialization into expected return type
1 parent e761833 commit f886c93

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

ShopifySharp.Tests/Services/Graph/GraphService.PostAsyncTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,47 @@ public async Task PostAsync_WithReturnTypeParameter_WhenGivenInvalidJson_ShouldT
769769
.Path.Should().BeNull(exn.Which.JsonPropertyName);
770770
}
771771

772+
[Fact(DisplayName = "PostAsync(GraphRequest graphRequest, Type returnType) should throw a ShopifyJsonParseException when the json serializer throws an exception during final deserialization to the desired return type")]
773+
public async Task PostAsync_WithReturnTypeParameter_WhenTheJsonSerializerThrowsAnExceptionDuringFinalDeserializationToTheDesiredReturnType_ShouldThrowAShopifyJsonParseException()
774+
{
775+
// Setup
776+
const string responseJson =
777+
"""
778+
{
779+
"data": {
780+
"fooOperation": {
781+
"foo": "some-foo",
782+
"bar": "some-bar",
783+
"baz": {
784+
"bat": "some-bat",
785+
"qux": null
786+
}
787+
}
788+
}
789+
}
790+
""";
791+
const string expectedRequestId = "some-expected-request-id";
792+
var expectedReturnType = typeof(TestGraphOperation);
793+
var graphRequest = GraphServiceTestUtils.MakeGraphRequest();
794+
795+
var expectedDeserializeCall = A.CallTo(() =>
796+
_jsonSerializer.DeserializeAsync(A<IJsonElement>._, expectedReturnType, CancellationToken.None));
797+
expectedDeserializeCall.Throws<TestException>();
798+
799+
A.CallTo(_policy)
800+
.WithReturnType<Task<RequestResult<string>>>()
801+
.Returns(Utils.MakeRequestResult(responseJson, x => x.RequestId = expectedRequestId));
802+
803+
// Act
804+
var act = async () => await _sut.PostAsync(graphRequest, expectedReturnType);
805+
806+
// Assert
807+
await act.Should().ThrowAsync<ShopifyJsonParseException>()
808+
.Where(x => x.RequestId == expectedRequestId)
809+
.WithInnerExceptionExactly(typeof(TestException));
810+
expectedDeserializeCall.MustHaveHappenedOnceExactly();
811+
}
812+
772813
[Theory(DisplayName = "PostAsync(GraphRequest graphRequest, Type returnType) should throw a ShopifyJsonParseException when the data object is null, missing or does not match the expected primitive type")]
773814
[InlineData(""" "data": null """, JsonValueKind.Null)]
774815
[InlineData(""" "data": true """, JsonValueKind.True)]

ShopifySharp/Services/Graph/GraphService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public virtual async Task<GraphResult<object>> PostAsync(GraphRequest graphReque
105105

106106
try
107107
{
108-
// TODO: add a test for this
109108
data = await _jsonSerializer.DeserializeAsync(dataElement, resultType, cancellationToken);
110109
}
111110
catch (Exception exn)

0 commit comments

Comments
 (0)