Skip to content

Commit b7c0414

Browse files
committed
chore: add unit tests for ShopifyGraphUserErrorsException
1 parent fc489dd commit b7c0414

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#nullable enable
2+
using System;
3+
using System.Collections.Generic;
4+
using FluentAssertions;
5+
using JetBrains.Annotations;
6+
using ShopifySharp.Services.Graph;
7+
using Xunit;
8+
9+
namespace ShopifySharp.Tests.Infrastructure;
10+
11+
[Trait("Category", "Infrastructure"), TestSubject(typeof(ShopifyGraphErrorsException))]
12+
public class ShopifyGraphErrorsExceptionTests
13+
{
14+
private const string GenericExceptionMessage = "GraphQL operation returned one or more errors.";
15+
16+
private static IReadOnlyList<GraphError> CreateErrors(Action<GraphError, GraphErrorExtensions>? customize = null)
17+
{
18+
var list = new List<GraphError>();
19+
var userError = new GraphError
20+
{
21+
Message = "some-message",
22+
Extensions = new GraphErrorExtensions
23+
{
24+
Code = "some-code",
25+
}
26+
};
27+
customize?.Invoke(userError, userError.Extensions);
28+
list.Add(userError);
29+
30+
return list;
31+
}
32+
33+
[Fact]
34+
public void WhenErrorsListIsEmpty_ItShouldUseAGenericExceptionMessage()
35+
{
36+
// Setup
37+
const string expectedRequestId = "some-request-id";
38+
39+
// Act
40+
var exn = new ShopifyGraphErrorsException([], expectedRequestId);
41+
42+
// Assert
43+
exn.Message.Should().Be(GenericExceptionMessage);
44+
exn.RequestId.Should().Be(expectedRequestId);
45+
}
46+
47+
[Fact]
48+
public void WhenErrorsListIsNotEmpty_ItShouldCreateAnExceptionMessageFromTheFirstErrorCodeAndMessage()
49+
{
50+
// Setup
51+
const string expectedRequestId = "some-request-id";
52+
const string expectedCode = "some-expected-code";
53+
const string expectedMessage = "some-expected-message";
54+
var userErrors = CreateErrors((err, ex) =>
55+
{
56+
err.Message = expectedMessage;
57+
ex.Code = expectedCode;
58+
});
59+
60+
// Act
61+
var exn = new ShopifyGraphErrorsException(userErrors, expectedRequestId);
62+
63+
// Assert
64+
exn.Message.Should().Be($"{expectedCode}: {expectedMessage}");
65+
exn.RequestId.Should().Be(expectedRequestId);
66+
exn.GraphErrors.Should().HaveCount(1).And.AllSatisfy(x =>
67+
{
68+
x.Message.Should().Be(expectedMessage);
69+
x.Extensions.Should().BeEquivalentTo(new GraphErrorExtensions
70+
{
71+
Code = expectedCode
72+
});
73+
});
74+
}
75+
76+
[Theory]
77+
[CombinatorialData]
78+
public void WhenErrorsListIsNotEmpty_AndTheFirstErrorHasAnEmptyMessage_AndTheFirstErrorHasANonEmptyCode_ItShouldCreateAnExceptionMessageFromTheFirstCode(
79+
[CombinatorialValues(null, "")] string? expectedMessageValue
80+
)
81+
{
82+
// Setup
83+
const string expectedRequestId = "some-request-id";
84+
const string expectedCode = "some-expected-code";
85+
var userErrors = CreateErrors((err, ex) =>
86+
{
87+
err.Message = expectedMessageValue!;
88+
ex.Code = expectedCode;
89+
});
90+
91+
// Act
92+
var exn = new ShopifyGraphErrorsException(userErrors, expectedRequestId);
93+
94+
// Assert
95+
exn.Message.Should().Be($"{expectedCode}: ");
96+
exn.RequestId.Should().Be(expectedRequestId);
97+
exn.GraphErrors.Should().HaveCount(1).And.AllSatisfy(x =>
98+
{
99+
x.Message.Should().Be(expectedMessageValue);
100+
x.Extensions.Should().BeEquivalentTo(new GraphErrorExtensions
101+
{
102+
Code = expectedCode
103+
});
104+
});
105+
}
106+
107+
[Theory]
108+
[CombinatorialData]
109+
public void WhenErrorsListIsNotEmpty_AndTheFirstErrorHasANonEmptyMessage_AndTheFirstErrorHasAnEmptyCode_ItShouldCreateAnExceptionMessageFromTheFirstMessage(
110+
[CombinatorialValues(null, "")] string? expectedCodeValue
111+
)
112+
{
113+
// Setup
114+
const string expectedRequestId = "some-request-id";
115+
const string expectedMessage = "some-expected-message";
116+
var userErrors = CreateErrors((err, ex) =>
117+
{
118+
err.Message = expectedMessage;
119+
ex.Code = expectedCodeValue!;
120+
});
121+
122+
// Act
123+
var exn = new ShopifyGraphErrorsException(userErrors, expectedRequestId);
124+
125+
// Assert
126+
exn.Message.Should().Be(expectedMessage, "the exception should use the first error's message value as the exception message");
127+
exn.RequestId.Should().Be(expectedRequestId);
128+
exn.GraphErrors.Should().HaveCount(1).And.AllSatisfy(x =>
129+
{
130+
x.Message.Should().Be(expectedMessage);
131+
x.Extensions.Should().BeEquivalentTo(new GraphErrorExtensions
132+
{
133+
Code = expectedCodeValue!
134+
});
135+
});
136+
}
137+
138+
[Theory]
139+
[CombinatorialData]
140+
public void WhenErrorsListIsNotEmpty_AndTheFirstErrorHasAnEmptyMessage_AndTheFirstErrorHasAnEmptyCode_ItShouldUseAGenericExceptionMessage(
141+
[CombinatorialValues(null, "")] string? expectedMessageValue,
142+
[CombinatorialValues(null, "")] string? expectedCodeValue
143+
)
144+
{
145+
// Setup
146+
const string expectedRequestId = "some-request-id";
147+
var userErrors = CreateErrors((err, ex) =>
148+
{
149+
err.Message = expectedMessageValue!;
150+
ex.Code = expectedCodeValue!;
151+
});
152+
153+
// Act
154+
var exn = new ShopifyGraphErrorsException(userErrors, expectedRequestId);
155+
156+
// Assert
157+
exn.Message.Should().Be(GenericExceptionMessage);
158+
exn.RequestId.Should().Be(expectedRequestId);
159+
exn.GraphErrors.Should().HaveCount(1).And.AllSatisfy(x =>
160+
{
161+
x.Message.Should().Be(expectedMessageValue);
162+
x.Extensions.Should().BeEquivalentTo(new GraphErrorExtensions
163+
{
164+
Code = expectedCodeValue!
165+
});
166+
});
167+
}
168+
}

0 commit comments

Comments
 (0)