Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bjosttveit committed Sep 15, 2023
1 parent 152d09c commit e66497e
Showing 1 changed file with 118 additions and 2 deletions.
120 changes: 118 additions & 2 deletions test/Altinn.App.Core.Tests/LayoutExpressions/TestDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,122 @@ public void TestRemoveFields()
model.Friends.First().Friends.Should().BeNull();
}

[Fact]
public void TestRemoveRows()
{
var model = new Model()
{
Id = 2,
Name = new()
{
Value = "Per"
},
Friends = new List<Friend>
{
new()
{
Name = new()
{
Value = "Første venn"
},
Age = 1235,
Friends = new List<Friend>
{
new()
{
Name = new()
{
Value = "Første venn sin første venn",
},
Age = 233
},
new()
{
Name = new()
{
Value = "Første venn sin andre venn",
},
Age = 233
},
new()
{
Name = new()
{
Value = "Første venn sin tredje venn",
},
Age = 233
}
}
},
new()
{
Name = new()
{
Value = "Andre venn"
},
Age = 1235,
Friends = new List<Friend>
{
new()
{
Name = new()
{
Value = "Andre venn sin venn",
},
Age = 233
}
}
},
new()
{
Name = new()
{
Value = "Tredje venn"
},
Age = 1235,
Friends = new List<Friend>
{
new()
{
Name = new()
{
Value = "Tredje venn sin venn",
},
Age = 233
}
}
}
}
};
var serializedModel = System.Text.Json.JsonSerializer.Serialize(model);

// deleteRows = false
var model1 = System.Text.Json.JsonSerializer.Deserialize<Model>(serializedModel)!;
IDataModelAccessor modelHelper1 = new DataModel(model1);

modelHelper1.RemoveField("friends[0].friends[0]");
model1.Friends![0].Friends![0].Should().BeNull();
model1.Friends![0].Friends!.Count.Should().Be(3);
model1.Friends[0].Friends![1].Name!.Value.Should().Be("Første venn sin andre venn");

modelHelper1.RemoveField("friends[1]");
model1.Friends[1].Should().BeNull();
model1.Friends.Count.Should().Be(3);
model1.Friends[2].Name!.Value.Should().Be("Tredje venn");

// deleteRows = true
var model2 = System.Text.Json.JsonSerializer.Deserialize<Model>(serializedModel)!;
IDataModelAccessor modelHelper2 = new DataModel(model2);

modelHelper2.RemoveField("friends[0].friends[0]", true);
model2.Friends![0].Friends!.Count.Should().Be(2);
model2.Friends[0].Friends![0].Name!.Value.Should().Be("Første venn sin andre venn");

modelHelper2.RemoveField("friends[1]", true);
model2.Friends.Count.Should().Be(2);
model2.Friends[1].Name!.Value.Should().Be("Tredje venn");
}

[Fact]
public void TestErrorCases()
{
Expand Down Expand Up @@ -374,7 +490,7 @@ public class Model

[JsonProperty("friends")]
[JsonPropertyName("friends")]
public IEnumerable<Friend>? Friends { get; set; }
public IList<Friend>? Friends { get; set; }
}

public class Name
Expand All @@ -397,5 +513,5 @@ public class Friend
// Infinite recursion. Simple way for testing
[JsonProperty("friends")]
[JsonPropertyName("friends")]
public IEnumerable<Friend>? Friends { get; set; }
public IList<Friend>? Friends { get; set; }
}

0 comments on commit e66497e

Please sign in to comment.