From 8326659679da8ee18b5a4bb32b5aebe6fc6bc143 Mon Sep 17 00:00:00 2001 From: RemyDuijkeren Date: Mon, 3 Jun 2024 22:29:07 +0800 Subject: [PATCH] Added test cases for System.Text.Json serialization and deserialization #98 (still failing) Added multiple test cases to test serialization and deserialization of the 'Money' object using the System.Text.Json library. The test cases cover both valid and invalid scenarios and also include testing the serialization and deserialization of nested objects. --- tests/NodaMoney.Tests/NodaMoney.Tests.csproj | 3 +- .../Serialization/MoneySerializableSpec.cs | 110 +++++++++++++++++- 2 files changed, 109 insertions(+), 4 deletions(-) diff --git a/tests/NodaMoney.Tests/NodaMoney.Tests.csproj b/tests/NodaMoney.Tests/NodaMoney.Tests.csproj index 8d497a7..9197e7d 100644 --- a/tests/NodaMoney.Tests/NodaMoney.Tests.csproj +++ b/tests/NodaMoney.Tests/NodaMoney.Tests.csproj @@ -1,7 +1,7 @@  - net48;net6.0;netcoreapp3.1 + net8.0;net6.0;net48; NodaMoney.Tests NodaMoney.Tests true @@ -25,6 +25,7 @@ + diff --git a/tests/NodaMoney.Tests/Serialization/MoneySerializableSpec.cs b/tests/NodaMoney.Tests/Serialization/MoneySerializableSpec.cs index 99be53c..08503fa 100644 --- a/tests/NodaMoney.Tests/Serialization/MoneySerializableSpec.cs +++ b/tests/NodaMoney.Tests/Serialization/MoneySerializableSpec.cs @@ -11,7 +11,111 @@ namespace NodaMoney.Tests.Serialization.MoneySerializableSpec { - public class GivenIWantToDeserializeMoneyWithJsonNetSerializer + public class GivenIWantToDeserializeMoneyWithSystemTextJsonSerializer + { + [Theory] + [ClassData(typeof(ValidJsonTestData))] + public void WhenDeserializing_ThenThisShouldSucceed(string json, Money expected) + { + var clone = System.Text.Json.JsonSerializer.Deserialize(json); + + clone.Should().Be(expected); + } + + [Theory] + [ClassData(typeof(InvalidJsonTestData))] + public void WhenDeserializingWithInvalidJSON_ThenThisShouldFail(string json) + { + Action action = () => System.Text.Json.JsonSerializer.Deserialize(json); + + action.Should().Throw().WithMessage("Member '*"); + } + + [Theory] + [ClassData(typeof(NestedJsonTestData))] + public void WhenDeserializingWithNested_ThenThisShouldSucceed(string json, Order expected) + { + var clone = System.Text.Json.JsonSerializer.Deserialize(json); + + clone.Should().BeEquivalentTo(expected); + clone.Discount.Should().BeNull(); + } + } + + // TODO: Write convertors, see https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-8-0 + public class GivenIWantToSerializeMoneyWithSystemTextJsonSerializer + { + public static IEnumerable TestData => new[] + { + new object[] { new Money(765.4321m, Currency.FromCode("JPY")) }, + new object[] { new Money(765.4321m, Currency.FromCode("EUR")) }, + new object[] { new Money(765.4321m, Currency.FromCode("USD")) }, + new object[] { new Money(765.4321m, Currency.FromCode("BHD")) }, + new object[] { default(Money) }, + new object[] { default(Money?) } + }; + + [Theory] + [MemberData(nameof(TestData))] + public void WhenSerializingCurrency_ThenThisShouldSucceed(Money money) + { + string json = System.Text.Json.JsonSerializer.Serialize(money.Currency); + Console.WriteLine(json); + var clone = System.Text.Json.JsonSerializer.Deserialize(json); + + clone.Should().Be(money.Currency); + } + + [Theory] + [MemberData(nameof(TestData))] + public void WhenSerializingMoney_ThenThisShouldSucceed(Money money) + { + string json = System.Text.Json.JsonSerializer.Serialize(money); + // Console.WriteLine(json); + var clone = System.Text.Json.JsonSerializer.Deserialize(json); + + clone.Should().Be(money); + } + + [Theory] + [MemberData(nameof(TestData))] + public void WhenSerializingArticleWithPrice_ThenThisShouldSucceed(Money money) + { + var order = new Order + { + Id = 123, + Name = "Foo", + Price = money + }; + + string json = System.Text.Json.JsonSerializer.Serialize(order); + // Console.WriteLine(json); + var clone = System.Text.Json.JsonSerializer.Deserialize(json); + + clone.Should().BeEquivalentTo(order); + } + + [Theory] + [MemberData(nameof(TestData))] + public void WhenSerializingArticleWithPriceAndDiscount_ThenThisShouldSucceed(Money money) + { + var order = new Order + { + Id = 123, + Name = "Foo", + Price = money, + Discount = money + }; + + string json = System.Text.Json.JsonSerializer.Serialize(order); + // Console.WriteLine(json); + var clone = System.Text.Json.JsonSerializer.Deserialize(json); + + clone.Should().BeEquivalentTo(order); + } + } + + public class GivenIWantToDeserializeMoneyWithNewtonsoftJsonSerializer { [Theory] [ClassData(typeof(ValidJsonTestData))] @@ -42,7 +146,7 @@ public void WhenDeserializingWithNested_ThenThisShouldSucceed(string json, Order } } - public class GivenIWantToSerializeMoneyWithJsonNetSerializer + public class GivenIWantToSerializeMoneyWithNewtonsoftJsonSerializer { public static IEnumerable TestData => new[] { @@ -59,7 +163,7 @@ public class GivenIWantToSerializeMoneyWithJsonNetSerializer public void WhenSerializingCurrency_ThenThisShouldSucceed(Money money) { string json = JsonConvert.SerializeObject(money.Currency); - // Console.WriteLine(json); + //Console.WriteLine(json); var clone = JsonConvert.DeserializeObject(json); clone.Should().Be(money.Currency);