Skip to content

Commit

Permalink
exclude empty values from MultiString when deserializing JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Jan 15, 2025
1 parent 558ec54 commit df22ca0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
72 changes: 72 additions & 0 deletions backend/FwLite/MiniLcm.Tests/MultiStringTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Text.Json;
using SystemTextJsonPatch;

namespace MiniLcm.Tests;

public class MultiStringTests
{

[Fact]
public void CanDeserializeMultiString()
{
//lang=json
var json = """{"en": "test"}""";
var expectedMs = new MultiString() { Values = { { "en", "test" } } };
var actualMs = JsonSerializer.Deserialize<MultiString>(json);
actualMs.Should().NotBeNull();
actualMs.Values.Should().ContainKey("en");
actualMs.Should().BeEquivalentTo(expectedMs);
}

[Fact]
public void MultiString_DeserializesEmptyString()
{
//lang=json
var json = """{"en": ""}""";
var actualMs = JsonSerializer.Deserialize<MultiString>(json);
actualMs.Should().NotBeNull();
actualMs.Values.Should().NotContainKey("en");
}

[Fact]
public void MultiString_DeserializesWhitespaceString()
{
//lang=json
var json = """{"en": "\t"}""";
var actualMs = JsonSerializer.Deserialize<MultiString>(json);
actualMs.Should().NotBeNull();
actualMs.Values.Should().ContainKey("en");
}

[Fact]
public void JsonPatchCanUpdateMultiString()
{
var ms = new MultiString() { Values = { { "en", "test" } } };
var patch = new JsonPatchDocument<MultiString>();
patch.Replace(ms => ms["en"], "updated");
patch.ApplyTo(ms);
ms.Values.Should().ContainKey("en");
ms.Values["en"].Should().Be("updated");
}

[Fact]
public void JsonPatchCanRemoveMultiString()
{
var ms = new MultiString() { Values = { { "en", "test" } } };
var patch = new JsonPatchDocument<MultiString>();
patch.Remove(ms => ms["en"]);
patch.ApplyTo(ms);
ms.Values.Should().NotContainKey("en");
}

[Fact]
public void JsonPatchCanAddMultiString()
{
var ms = new MultiString() { Values = { { "en", "test" } } };
var patch = new JsonPatchDocument<MultiString>();
patch.Add(ms => ms["fr"], "test");
patch.ApplyTo(ms);
ms.Values.Should().ContainKey("fr");
ms.Values["fr"].Should().Be("test");
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Text.Json;
using MiniLcm.Models;
using Xunit.Abstractions;

namespace LcmCrdt.Tests;
namespace MiniLcm.Tests;

public class SerializationTests(ITestOutputHelper output)
{
Expand Down Expand Up @@ -65,20 +64,6 @@ public void CanDeserializeEntry()
fromJson.Should().BeEquivalentTo(entry);
}

[Fact]
public void CanDeserializeMultiString()
{
//lang=json
var json = """{"en": "test"}""";
var expectedMs = new MultiString()
{
Values = { { "en", "test" } }
};
var actualMs = JsonSerializer.Deserialize<MultiString>(json);
actualMs.Should().NotBeNull();
actualMs.Values.Should().ContainKey("en");
actualMs.Should().BeEquivalentTo(expectedMs);
}

[Fact]
public void EqualityTest()
Expand Down
1 change: 1 addition & 0 deletions backend/FwLite/MiniLcm/Models/MultiString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public class MultiStringConverter : JsonConverter<MultiString>
var ms = new MultiString();
foreach (var (key, value) in dict)
{
if (string.IsNullOrEmpty(value)) continue;
ms.Values[key] = value;
}

Expand Down

0 comments on commit df22ca0

Please sign in to comment.