-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add unit tests for ShopifyGraphUserErrorsException
- Loading branch information
1 parent
fc489dd
commit b7c0414
Showing
1 changed file
with
168 additions
and
0 deletions.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
ShopifySharp.Tests/Infrastructure/ShopifyGraphErrorsExceptionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#nullable enable | ||
using System; | ||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using JetBrains.Annotations; | ||
using ShopifySharp.Services.Graph; | ||
using Xunit; | ||
|
||
namespace ShopifySharp.Tests.Infrastructure; | ||
|
||
[Trait("Category", "Infrastructure"), TestSubject(typeof(ShopifyGraphErrorsException))] | ||
public class ShopifyGraphErrorsExceptionTests | ||
{ | ||
private const string GenericExceptionMessage = "GraphQL operation returned one or more errors."; | ||
|
||
private static IReadOnlyList<GraphError> CreateErrors(Action<GraphError, GraphErrorExtensions>? customize = null) | ||
{ | ||
var list = new List<GraphError>(); | ||
var userError = new GraphError | ||
{ | ||
Message = "some-message", | ||
Extensions = new GraphErrorExtensions | ||
{ | ||
Code = "some-code", | ||
} | ||
}; | ||
customize?.Invoke(userError, userError.Extensions); | ||
list.Add(userError); | ||
|
||
return list; | ||
} | ||
|
||
[Fact] | ||
public void WhenErrorsListIsEmpty_ItShouldUseAGenericExceptionMessage() | ||
{ | ||
// Setup | ||
const string expectedRequestId = "some-request-id"; | ||
|
||
// Act | ||
var exn = new ShopifyGraphErrorsException([], expectedRequestId); | ||
|
||
// Assert | ||
exn.Message.Should().Be(GenericExceptionMessage); | ||
exn.RequestId.Should().Be(expectedRequestId); | ||
} | ||
|
||
[Fact] | ||
public void WhenErrorsListIsNotEmpty_ItShouldCreateAnExceptionMessageFromTheFirstErrorCodeAndMessage() | ||
{ | ||
// Setup | ||
const string expectedRequestId = "some-request-id"; | ||
const string expectedCode = "some-expected-code"; | ||
const string expectedMessage = "some-expected-message"; | ||
var userErrors = CreateErrors((err, ex) => | ||
{ | ||
err.Message = expectedMessage; | ||
ex.Code = expectedCode; | ||
}); | ||
|
||
// Act | ||
var exn = new ShopifyGraphErrorsException(userErrors, expectedRequestId); | ||
|
||
// Assert | ||
exn.Message.Should().Be($"{expectedCode}: {expectedMessage}"); | ||
exn.RequestId.Should().Be(expectedRequestId); | ||
exn.GraphErrors.Should().HaveCount(1).And.AllSatisfy(x => | ||
{ | ||
x.Message.Should().Be(expectedMessage); | ||
x.Extensions.Should().BeEquivalentTo(new GraphErrorExtensions | ||
{ | ||
Code = expectedCode | ||
}); | ||
}); | ||
} | ||
|
||
[Theory] | ||
[CombinatorialData] | ||
public void WhenErrorsListIsNotEmpty_AndTheFirstErrorHasAnEmptyMessage_AndTheFirstErrorHasANonEmptyCode_ItShouldCreateAnExceptionMessageFromTheFirstCode( | ||
[CombinatorialValues(null, "")] string? expectedMessageValue | ||
) | ||
{ | ||
// Setup | ||
const string expectedRequestId = "some-request-id"; | ||
const string expectedCode = "some-expected-code"; | ||
var userErrors = CreateErrors((err, ex) => | ||
{ | ||
err.Message = expectedMessageValue!; | ||
ex.Code = expectedCode; | ||
}); | ||
|
||
// Act | ||
var exn = new ShopifyGraphErrorsException(userErrors, expectedRequestId); | ||
|
||
// Assert | ||
exn.Message.Should().Be($"{expectedCode}: "); | ||
exn.RequestId.Should().Be(expectedRequestId); | ||
exn.GraphErrors.Should().HaveCount(1).And.AllSatisfy(x => | ||
{ | ||
x.Message.Should().Be(expectedMessageValue); | ||
x.Extensions.Should().BeEquivalentTo(new GraphErrorExtensions | ||
{ | ||
Code = expectedCode | ||
}); | ||
}); | ||
} | ||
|
||
[Theory] | ||
[CombinatorialData] | ||
public void WhenErrorsListIsNotEmpty_AndTheFirstErrorHasANonEmptyMessage_AndTheFirstErrorHasAnEmptyCode_ItShouldCreateAnExceptionMessageFromTheFirstMessage( | ||
[CombinatorialValues(null, "")] string? expectedCodeValue | ||
) | ||
{ | ||
// Setup | ||
const string expectedRequestId = "some-request-id"; | ||
const string expectedMessage = "some-expected-message"; | ||
var userErrors = CreateErrors((err, ex) => | ||
{ | ||
err.Message = expectedMessage; | ||
ex.Code = expectedCodeValue!; | ||
}); | ||
|
||
// Act | ||
var exn = new ShopifyGraphErrorsException(userErrors, expectedRequestId); | ||
|
||
// Assert | ||
exn.Message.Should().Be(expectedMessage, "the exception should use the first error's message value as the exception message"); | ||
exn.RequestId.Should().Be(expectedRequestId); | ||
exn.GraphErrors.Should().HaveCount(1).And.AllSatisfy(x => | ||
{ | ||
x.Message.Should().Be(expectedMessage); | ||
x.Extensions.Should().BeEquivalentTo(new GraphErrorExtensions | ||
{ | ||
Code = expectedCodeValue! | ||
}); | ||
}); | ||
} | ||
|
||
[Theory] | ||
[CombinatorialData] | ||
public void WhenErrorsListIsNotEmpty_AndTheFirstErrorHasAnEmptyMessage_AndTheFirstErrorHasAnEmptyCode_ItShouldUseAGenericExceptionMessage( | ||
[CombinatorialValues(null, "")] string? expectedMessageValue, | ||
[CombinatorialValues(null, "")] string? expectedCodeValue | ||
) | ||
{ | ||
// Setup | ||
const string expectedRequestId = "some-request-id"; | ||
var userErrors = CreateErrors((err, ex) => | ||
{ | ||
err.Message = expectedMessageValue!; | ||
ex.Code = expectedCodeValue!; | ||
}); | ||
|
||
// Act | ||
var exn = new ShopifyGraphErrorsException(userErrors, expectedRequestId); | ||
|
||
// Assert | ||
exn.Message.Should().Be(GenericExceptionMessage); | ||
exn.RequestId.Should().Be(expectedRequestId); | ||
exn.GraphErrors.Should().HaveCount(1).And.AllSatisfy(x => | ||
{ | ||
x.Message.Should().Be(expectedMessageValue); | ||
x.Extensions.Should().BeEquivalentTo(new GraphErrorExtensions | ||
{ | ||
Code = expectedCodeValue! | ||
}); | ||
}); | ||
} | ||
} |