Skip to content

Commit

Permalink
Added test cases for System.Text.Json serialization and deserialization
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
RemyDuijkeren committed Jun 3, 2024
1 parent ffbc68c commit 8326659
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
3 changes: 2 additions & 1 deletion tests/NodaMoney.Tests/NodaMoney.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net6.0;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;net48;</TargetFrameworks>
<AssemblyName>NodaMoney.Tests</AssemblyName>
<PackageId>NodaMoney.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand All @@ -25,6 +25,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RavenDB.TestDriver" Version="5.3.103" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand Down
110 changes: 107 additions & 3 deletions tests/NodaMoney.Tests/Serialization/MoneySerializableSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Money>(json);

clone.Should().Be(expected);
}

[Theory]
[ClassData(typeof(InvalidJsonTestData))]
public void WhenDeserializingWithInvalidJSON_ThenThisShouldFail(string json)
{
Action action = () => System.Text.Json.JsonSerializer.Deserialize<Money>(json);

action.Should().Throw<SerializationException>().WithMessage("Member '*");
}

[Theory]
[ClassData(typeof(NestedJsonTestData))]
public void WhenDeserializingWithNested_ThenThisShouldSucceed(string json, Order expected)
{
var clone = System.Text.Json.JsonSerializer.Deserialize<Order>(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<object[]> 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<Currency>(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<Money>(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<Order>(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<Order>(json);

clone.Should().BeEquivalentTo(order);
}
}

public class GivenIWantToDeserializeMoneyWithNewtonsoftJsonSerializer
{
[Theory]
[ClassData(typeof(ValidJsonTestData))]
Expand Down Expand Up @@ -42,7 +146,7 @@ public void WhenDeserializingWithNested_ThenThisShouldSucceed(string json, Order
}
}

public class GivenIWantToSerializeMoneyWithJsonNetSerializer
public class GivenIWantToSerializeMoneyWithNewtonsoftJsonSerializer
{
public static IEnumerable<object[]> TestData => new[]
{
Expand All @@ -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<Currency>(json);

clone.Should().Be(money.Currency);
Expand Down

0 comments on commit 8326659

Please sign in to comment.