Skip to content

Commit

Permalink
chore: add unit test for exceptions thrown during final deserializati…
Browse files Browse the repository at this point in the history
…on into expected return type
  • Loading branch information
nozzlegear committed Jan 15, 2025
1 parent e761833 commit f886c93
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
41 changes: 41 additions & 0 deletions ShopifySharp.Tests/Services/Graph/GraphService.PostAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,47 @@ public async Task PostAsync_WithReturnTypeParameter_WhenGivenInvalidJson_ShouldT
.Path.Should().BeNull(exn.Which.JsonPropertyName);
}

[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")]
public async Task PostAsync_WithReturnTypeParameter_WhenTheJsonSerializerThrowsAnExceptionDuringFinalDeserializationToTheDesiredReturnType_ShouldThrowAShopifyJsonParseException()
{
// Setup
const string responseJson =
"""
{
"data": {
"fooOperation": {
"foo": "some-foo",
"bar": "some-bar",
"baz": {
"bat": "some-bat",
"qux": null
}
}
}
}
""";
const string expectedRequestId = "some-expected-request-id";
var expectedReturnType = typeof(TestGraphOperation);
var graphRequest = GraphServiceTestUtils.MakeGraphRequest();

var expectedDeserializeCall = A.CallTo(() =>
_jsonSerializer.DeserializeAsync(A<IJsonElement>._, expectedReturnType, CancellationToken.None));
expectedDeserializeCall.Throws<TestException>();

A.CallTo(_policy)
.WithReturnType<Task<RequestResult<string>>>()
.Returns(Utils.MakeRequestResult(responseJson, x => x.RequestId = expectedRequestId));

// Act
var act = async () => await _sut.PostAsync(graphRequest, expectedReturnType);

// Assert
await act.Should().ThrowAsync<ShopifyJsonParseException>()
.Where(x => x.RequestId == expectedRequestId)
.WithInnerExceptionExactly(typeof(TestException));
expectedDeserializeCall.MustHaveHappenedOnceExactly();
}

[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")]
[InlineData(""" "data": null """, JsonValueKind.Null)]
[InlineData(""" "data": true """, JsonValueKind.True)]
Expand Down
1 change: 0 additions & 1 deletion ShopifySharp/Services/Graph/GraphService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public virtual async Task<GraphResult<object>> PostAsync(GraphRequest graphReque

try
{
// TODO: add a test for this
data = await _jsonSerializer.DeserializeAsync(dataElement, resultType, cancellationToken);
}
catch (Exception exn)
Expand Down

0 comments on commit f886c93

Please sign in to comment.