Skip to content

Commit 3ca6f0d

Browse files
committed
Add grouping fixtures and tests for PxBuilder groupings
Introduce GroupingFixtures for reusable test groupings and update PxBuilderTests to use these fixtures. Add comprehensive tests for grouping application, elimination, and aggregation, improving coverage of grouped variable handling in PxBuilder.
1 parent 3bd086b commit 3ca6f0d

File tree

2 files changed

+198
-1
lines changed

2 files changed

+198
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
namespace PxWeb.UnitTests.Fixtures
2+
{
3+
internal static class GroupingFixtures
4+
{
5+
internal readonly static GroupingInfo MartialStatusInfo = new("in-memory-martial-status")
6+
{
7+
Name = "Marital status",
8+
GroupPres = GroupingIncludesType.AggregatedValues
9+
};
10+
11+
internal readonly static GroupingInfo SexInfo = new("in-memory-sex")
12+
{
13+
Name = "Sex",
14+
GroupPres = GroupingIncludesType.AggregatedValues
15+
};
16+
17+
internal readonly static Grouping GroupMaritalStatus;
18+
internal readonly static Grouping GroupSex;
19+
20+
static GroupingFixtures()
21+
{
22+
// Marital status
23+
GroupMaritalStatus = new Grouping();
24+
GroupMaritalStatus.Name = "In memmory";
25+
GroupMaritalStatus.Map = null;
26+
GroupMaritalStatus.Groups.Add(new Group()
27+
{
28+
GroupCode = "S",
29+
Name = "Small",
30+
ChildCodes = [new() { Code = "OG" }]
31+
});
32+
33+
GroupMaritalStatus.Groups.Add(new Group()
34+
{
35+
GroupCode = "L",
36+
Name = "Large",
37+
ChildCodes = [new() { Code = "G" }, new() { Code = "ÄNKL" }, new() { Code = "SK" }]
38+
});
39+
40+
GroupMaritalStatus.Groups.Add(new Group()
41+
{
42+
GroupCode = "T",
43+
Name = "Total",
44+
ChildCodes = [new() { Code = "total" }]
45+
});
46+
47+
// Sex
48+
GroupSex = new Grouping();
49+
GroupSex.Name = "In memmory";
50+
GroupSex.Map = null;
51+
GroupSex.Groups.Add(new Group()
52+
{
53+
GroupCode = "T2",
54+
Name = "Grouped Total",
55+
ChildCodes = [new() { Code = "1" }, new() { Code = "2" }]
56+
});
57+
}
58+
59+
}
60+
}

PxWeb.UnitTests/PxFile/PxBuilderTests.cs

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,26 @@ protected override GroupRegistryWrapper GetGroupRegistryProvider()
3535

3636
private static Mock<GroupRegistryWrapper> _groupRegistryMock = null!;
3737

38+
3839
[ClassInitialize]
3940
public static void SetupTests(TestContext testContext)
4041
{
42+
4143
_groupRegistryMock = new Mock<GroupRegistryWrapper>();
4244
_groupRegistryMock.Setup(gr => gr.IsLoaded).Returns(true);
4345

44-
_groupRegistryMock.Setup(gr => gr.GetDefaultGroupings(It.IsAny<string>()))
46+
47+
48+
_groupRegistryMock.Setup(gr => gr.GetDefaultGroupings(It.Is<string>(d => d == "Marital status")))
49+
.Returns(new List<GroupingInfo> { GroupingFixtures.MartialStatusInfo });
50+
_groupRegistryMock.Setup(gr => gr.GetDefaultGroupings(It.Is<string>(d => d == "Sex")))
51+
.Returns(new List<GroupingInfo> { GroupingFixtures.SexInfo });
52+
_groupRegistryMock.Setup(gr => gr.GetDefaultGroupings(It.Is<string>(d => d != "Marital status" && d != "Sex")))
4553
.Returns(new List<GroupingInfo>());
4654

55+
_groupRegistryMock.Setup(gr => gr.GetGrouping(It.Is<GroupingInfo>(g => g.Name == "Marital status"))).Returns(GroupingFixtures.GroupMaritalStatus);
56+
_groupRegistryMock.Setup(gr => gr.GetGrouping(It.Is<GroupingInfo>(g => g.Name == "Sex"))).Returns(GroupingFixtures.GroupSex);
57+
4758
}
4859

4960
[TestMethod]
@@ -99,5 +110,131 @@ public void BuildForPresentation_WithOneTimeValue_ShouldCleanTimeValue()
99110
Assert.AreEqual(@"TLIST(A1),""2001""", timeVal);
100111
}
101112

113+
[TestMethod]
114+
public void BuildForPresentation_WithGrouping_ReturnGroupedValues()
115+
{
116+
var builder = new TestablePxBuilder(PxFileFixtures.EliminationPxFile);
117+
var model = builder.BuildForSelection();
118+
119+
120+
var variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
121+
Assert.HasCount(1, variable.Groupings, "Initial groupings count mismatch for 'martial status'");
122+
123+
builder.ApplyGrouping(variable.Code, variable.Groupings[0], GroupingIncludesType.AggregatedValues);
124+
125+
variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
126+
Assert.HasCount(3, variable.Values, "Grouped values count mismatch");
127+
128+
var selections = Selection.SelectAll(builder.Model.Meta);
129+
builder.BuildForPresentation(selections);
130+
131+
variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
132+
Assert.HasCount(3, variable.Values, "Grouped values count mismatch");
133+
Assert.AreEqual("S", variable.Values[0].Code, "First grouped value code mismatch");
134+
Assert.AreEqual("L", variable.Values[1].Code, "Second grouped value code mismatch");
135+
Assert.AreEqual("T", variable.Values[2].Code, "Third grouped value code mismatch");
136+
Assert.AreEqual(2016801, builder.Model.Data.ReadElement(3, 0), "Large, men, 2001");
137+
}
138+
139+
[TestMethod]
140+
public void BuildForPresentation_WithGroupingAndElimination_ReturnGroupedValues()
141+
{
142+
var builder = new TestablePxBuilder(PxFileFixtures.EliminationPxFile);
143+
var model = builder.BuildForSelection();
144+
145+
var variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
146+
Assert.HasCount(1, variable.Groupings, "Initial groupings count mismatch for 'martial status'");
147+
148+
builder.ApplyGrouping(variable.Code, variable.Groupings[0], GroupingIncludesType.AggregatedValues);
149+
150+
variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
151+
Assert.HasCount(3, variable.Values, "Grouped values count mismatch after applied grouping");
152+
153+
var selections = Selection.SelectAll(builder.Model.Meta);
154+
selections.First(v => v.VariableCode == "sex").ValueCodes.Clear();
155+
156+
builder.BuildForPresentation(selections);
157+
158+
159+
variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
160+
Assert.HasCount(2, builder.Model.Meta.Variables, "Eliminated variable sex, variable count missmatch");
161+
Assert.HasCount(3, variable.Values, "Grouped values count mismatch");
162+
Assert.AreEqual("S", variable.Values[0].Code, "First grouped value code mismatch");
163+
Assert.AreEqual("L", variable.Values[1].Code, "Second grouped value code mismatch");
164+
Assert.AreEqual("T", variable.Values[2].Code, "Third grouped value code mismatch");
165+
Assert.AreEqual(4456408, builder.Model.Data.ReadElement(0, 0), "Small, Total(eliminated), 2001");
166+
Assert.AreEqual(4452720, builder.Model.Data.ReadElement(1, 0), "Large, Total(eliminated), 2001");
167+
Assert.AreEqual(8909128, builder.Model.Data.ReadElement(2, 0), "Total, Total(eliminated), 2001");
168+
169+
Assert.AreEqual(4445279, builder.Model.Data.ReadElement(1, 1), "Large, Grouped(eliminated), 2002");
170+
Assert.AreEqual(4441440, builder.Model.Data.ReadElement(1, 2), "Large, Grouped(eliminated), 2003");
171+
Assert.AreEqual(4443053, builder.Model.Data.ReadElement(1, 3), "Large, Grouped(eliminated), 2004");
172+
Assert.AreEqual(4447637, builder.Model.Data.ReadElement(1, 4), "Large, Grouped(eliminated), 2005");
173+
Assert.AreEqual(4466261, builder.Model.Data.ReadElement(1, 5), "Large, Grouped(eliminated), 2006");
174+
}
175+
176+
[TestMethod]
177+
public void BuildForPresentation_WithMultipleGroupings_ReturnGroupedValues()
178+
{
179+
var builder = new TestablePxBuilder(PxFileFixtures.EliminationPxFile);
180+
var model = builder.BuildForSelection();
181+
182+
var variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
183+
Assert.HasCount(1, variable.Groupings, "Initial groupings count mismatch for 'martial status'");
184+
builder.ApplyGrouping(variable.Code, variable.Groupings[0], GroupingIncludesType.AggregatedValues);
185+
variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
186+
Assert.HasCount(3, variable.Values, "Grouped values for 'marital status' count mismatch after applied grouping");
187+
188+
variable = builder.Model.Meta.Variables.First(v => v.Code == "sex");
189+
Assert.HasCount(1, variable.Groupings, "Initial groupings count mismatch for 'sex'");
190+
builder.ApplyGrouping(variable.Code, variable.Groupings[0], GroupingIncludesType.AggregatedValues);
191+
variable = builder.Model.Meta.Variables.First(v => v.Code == "sex");
192+
Assert.HasCount(1, variable.Values, "Grouped values for 'sex' count mismatch after applied grouping");
193+
194+
var selections = Selection.SelectAll(builder.Model.Meta);
195+
builder.BuildForPresentation(selections);
196+
197+
variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
198+
Assert.HasCount(3, variable.Values, "Grouped values count mismatch for 'marital status'");
199+
Assert.AreEqual("S", variable.Values[0].Code, "First grouped value code mismatch for 'marital status'");
200+
Assert.AreEqual("L", variable.Values[1].Code, "Second grouped value code mismatch for 'marital status'");
201+
Assert.AreEqual("T", variable.Values[2].Code, "Third grouped value code mismatch for 'marital status'");
202+
203+
variable = builder.Model.Meta.Variables.First(v => v.Code == "sex");
204+
Assert.HasCount(1, variable.Values, "Grouped values count mismatch for 'sex'");
205+
Assert.AreEqual("T2", variable.Values[0].Code, "First grouped value code mismatch for 'sex'");
206+
207+
Assert.AreEqual(4456408, builder.Model.Data.ReadElement(0, 0), "Small, Grouped Total, 2001");
208+
Assert.AreEqual(4452720, builder.Model.Data.ReadElement(1, 0), "Large, Grouped Total, 2001");
209+
Assert.AreEqual(8909128, builder.Model.Data.ReadElement(2, 0), "Total, Grouped Total, 2001");
210+
Assert.AreEqual(4445279, builder.Model.Data.ReadElement(1, 1), "Large, Grouped Total, 2002");
211+
Assert.AreEqual(4441440, builder.Model.Data.ReadElement(1, 2), "Large, Grouped Total, 2003");
212+
Assert.AreEqual(4443053, builder.Model.Data.ReadElement(1, 3), "Large, Grouped Total, 2004");
213+
Assert.AreEqual(4447637, builder.Model.Data.ReadElement(1, 4), "Large, Grouped Total, 2005");
214+
Assert.AreEqual(4466261, builder.Model.Data.ReadElement(1, 5), "Large, Grouped Total, 2006");
215+
}
216+
217+
[TestMethod]
218+
public void BuildForPresentation_WithGroupingWithGroupingTypeAll_ReturnGroupedValuesAndOriginalValues()
219+
{
220+
var builder = new TestablePxBuilder(PxFileFixtures.EliminationPxFile);
221+
var model = builder.BuildForSelection();
222+
223+
224+
var variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
225+
Assert.HasCount(1, variable.Groupings, "Initial groupings count mismatch for 'martial status'");
226+
227+
builder.ApplyGrouping(variable.Code, variable.Groupings[0], GroupingIncludesType.SingleValues);
228+
229+
variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
230+
Assert.HasCount(5, variable.Values, "Grouped values count mismatch");
231+
232+
var selections = Selection.SelectAll(builder.Model.Meta);
233+
builder.BuildForPresentation(selections);
234+
235+
variable = builder.Model.Meta.Variables.First(v => v.Code == "marital status");
236+
Assert.HasCount(5, variable.Values, "Grouped values count mismatch");
237+
}
238+
102239
}
103240
}

0 commit comments

Comments
 (0)