Skip to content

Commit 7e57802

Browse files
authored
Fix for note duplication when restoring notes when changing codelist (#382)
* Refactor note addition in Value object Introduce RetoreNote method to prevent duplicate notes. The method checks for existing notes before adding a new one, ensuring that notes with the same text, type, and mandatory status are not duplicated. This enhances the integrity of the notes associated with a Value object. * Added unit test
1 parent 9da098c commit 7e57802

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

PxWeb.UnitTests/Data/PaxiomFixUtilTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ public void RestoreNotes_WhenNotesExist_ReturnsNotes()
6767
Assert.AreEqual(1, variable.Values[0].Notes.Count);
6868
}
6969

70+
[TestMethod]
71+
public void RestoreNotes_WhenSameNoteExist_DoesNotAddNote()
72+
{
73+
// Arrange
74+
var variable = new Variable("VAR1", "VAR1", PlacementType.Heading, 1);
75+
var value = new Value("v1");
76+
value.AddNote(new Note("My note", NoteType.Value, true));
77+
PaxiomUtil.SetCode(value, "v1");
78+
variable.Values.Add(value);
79+
value = new Value("v2");
80+
PaxiomUtil.SetCode(value, "v2");
81+
variable.Values.Add(value);
82+
value = new Value("v3");
83+
PaxiomUtil.SetCode(value, "v3");
84+
variable.Values.Add(value);
85+
var notes = new Dictionary<string, Notes>();
86+
var list = new Notes();
87+
list.Add(new Note("My note", NoteType.Value, true));
88+
notes.Add("v1", list);
89+
90+
// Act
91+
PaxiomFixUtil.RestoreNotes(variable, notes);
92+
93+
// Assert
94+
Assert.AreEqual(1, variable.Values[0].Notes.Count);
95+
}
96+
7097
[TestMethod]
7198
public void RestoreNotes_WhenNotesNotApplicable_ReturnsNotes()
7299
{

PxWeb/Code/Api2/DataSelection/PaxiomFixUtil.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,28 @@ public static void RestoreNotes(Variable variable, Dictionary<string, Notes> not
3131
{
3232
foreach (var note in notes[valueCode])
3333
{
34-
value.AddNote(note);
34+
RetoreNote(value, note);
3535
}
3636
}
3737
}
3838
}
3939

40+
private static void RetoreNote(Value value, Note note)
41+
{
42+
if (value.Notes is null)
43+
{
44+
value.AddNote(note);
45+
}
46+
else
47+
{
48+
if (value.Notes.Any(n => n.Text == note.Text && n.Type == note.Type && n.Mandantory == note.Mandantory))
49+
{
50+
return; // Note already exists
51+
}
52+
value.AddNote(note);
53+
}
54+
}
55+
4056
public static Dictionary<string, Notes> ExtractNotes(Variable variable)
4157
{
4258

0 commit comments

Comments
 (0)