-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for GraphHttpContentSerializer
(cherry picked from commit dad7d4f)
- Loading branch information
1 parent
83e1221
commit ca3306d
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
ShopifySharp.Tests/Infrastructure/Serialization/Http/GraphHttpContentSerializerTests.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,121 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using JetBrains.Annotations; | ||
using ShopifySharp.Infrastructure; | ||
using ShopifySharp.Services.Graph; | ||
using Xunit; | ||
|
||
namespace ShopifySharp.Tests.Infrastructure.Serialization.Http; | ||
|
||
[Trait("Category", "Serialization"), TestSubject(typeof(GraphHttpContentSerializer))] | ||
public class GraphHttpContentSerializerTests | ||
{ | ||
private const string Query = "some-query"; | ||
private readonly RequestUri _requestUri = new (new Uri("https://example.com")); | ||
|
||
private readonly GraphHttpContentSerializer _sut = new(Serializer.SerializerDefaults); | ||
|
||
[Fact] | ||
public async Task SerializeGraphRequest_ShouldSerializeDataToJson() | ||
{ | ||
// Setup | ||
var variables = new Dictionary<string, object> | ||
{ | ||
{ "baz", "bat" }, | ||
{ "hello", "world" }, | ||
}; | ||
|
||
// Act | ||
var result = _sut.SerializeGraphRequest(_requestUri, new GraphRequest | ||
{ | ||
Query = Query, | ||
Variables = variables, | ||
}); | ||
var jsonStr = await result.ReadAsStringAsync(); | ||
|
||
// Assert | ||
jsonStr.Should().Be($$$"""{"query":"{{{Query}}}","variables":{"baz":"bat","hello":"world"}}"""); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
[Fact] | ||
public void SerializeGraphRequest_WhenExecutingInEnvironmentThatSupportsSystemMemory_ShouldReturnReadOnlyMemoryContent() | ||
{ | ||
// Setup | ||
var variables = new Dictionary<string, object> | ||
{ | ||
{ "baz", "bat" }, | ||
{ "hello", "world" }, | ||
}; | ||
|
||
// Act | ||
var result = _sut.SerializeGraphRequest(_requestUri, new GraphRequest | ||
{ | ||
Query = Query, | ||
Variables = variables, | ||
}); | ||
|
||
// Assert | ||
result.Should().BeOfType<ReadOnlyMemoryContent>(); | ||
result.Headers.ContentType?.MediaType.Should().Be("application/json"); | ||
} | ||
#else | ||
[Fact] | ||
public void SerializeGraphRequest_WhenExecutingInEnvironmentThatDoesNotSupportSystemMemory_ShouldReturnStringContent() | ||
{ | ||
// Setup | ||
var variables = new Dictionary<string, object> | ||
{ | ||
{ "baz", "bat" }, | ||
{ "hello", "world" }, | ||
}; | ||
|
||
// Act | ||
var result = _sut.SerializeGraphRequest(_requestUri, new GraphRequest | ||
{ | ||
Query = Query, | ||
Variables = variables, | ||
}); | ||
|
||
// Assert | ||
result.Should().BeOfType<StringContent>(); | ||
result.Headers.ContentType.MediaType.Should().Be("application/json"); | ||
} | ||
#endif | ||
|
||
#region Serializer.SerializerDefaults | ||
|
||
[Fact] | ||
public async Task WhenSerializingGraphRequest_ShouldOnlySerializeTheQueryAndVariablesPropertiesAndIgnoreAllOtherProperties() | ||
{ | ||
// Setup | ||
var variables = new Dictionary<string, object> | ||
{ | ||
{ "baz", "bat" }, | ||
{ "hello", null }, | ||
{ "world", 1 } | ||
}; | ||
const string expectedJson = | ||
$$$""" | ||
{"query":"{{{Query}}}","variables":{"baz":"bat","hello":null,"world":1}} | ||
"""; | ||
|
||
// Act | ||
var result = _sut.SerializeGraphRequest(_requestUri, new GraphRequest | ||
{ | ||
Query = Query, | ||
Variables = variables, | ||
EstimatedQueryCost = 123, | ||
UserErrorHandling = GraphRequestUserErrorHandling.DoNotThrow | ||
}); | ||
var jsonStr = await result.ReadAsStringAsync(); | ||
|
||
// Assert | ||
jsonStr.Should().Be(expectedJson); | ||
} | ||
|
||
#endregion | ||
} |