Skip to content

Commit

Permalink
Add tests for GraphHttpContentSerializer
Browse files Browse the repository at this point in the history
(cherry picked from commit dad7d4f)
  • Loading branch information
nozzlegear committed Jan 1, 2025
1 parent 83e1221 commit ca3306d
Showing 1 changed file with 121 additions and 0 deletions.
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
}

0 comments on commit ca3306d

Please sign in to comment.