-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ddi expression references resolution (#873)
* test: unit test for ddi expression references resolution * test: add overlapping failing case * fix: ddi expression references resolution Fix cases when binding references overlap. * chore: bump version
- Loading branch information
Showing
3 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...insee/eno/core/processing/in/steps/ddi/DDIResolveVariableReferencesInExpressionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package fr.insee.eno.core.processing.in.steps.ddi; | ||
|
||
import fr.insee.eno.core.model.EnoQuestionnaire; | ||
import fr.insee.eno.core.model.calculated.BindingReference; | ||
import fr.insee.eno.core.model.calculated.CalculatedExpression; | ||
import fr.insee.eno.core.model.navigation.Filter; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class DDIResolveVariableReferencesInExpressionsTest { | ||
|
||
@Test | ||
void filterExpression_oneReference() { | ||
// | ||
EnoQuestionnaire enoQuestionnaire = new EnoQuestionnaire(); | ||
Filter filter = new Filter(); | ||
CalculatedExpression calculatedExpression= new CalculatedExpression(); | ||
calculatedExpression.setValue("foo-reference = 1"); | ||
calculatedExpression.getBindingReferences() | ||
.add(new BindingReference("foo-reference", "FOO")); | ||
filter.setExpression(calculatedExpression); | ||
enoQuestionnaire.getFilters().add(filter); | ||
// | ||
new DDIResolveVariableReferencesInExpressions().apply(enoQuestionnaire); | ||
// | ||
assertEquals("FOO = 1", enoQuestionnaire.getFilters().get(0).getExpression().getValue()); | ||
} | ||
|
||
/** | ||
* In some cases variable references can overlap. This could lead to incorrect replacement of references by the | ||
* corresponding variable name. This nested class contains a group of tests for these cases. | ||
*/ | ||
@Nested | ||
class OverlappingCases { | ||
|
||
/** | ||
* This test uses an ordered implementation of Set for binding references, to simulate what could happen with | ||
* real data. | ||
*/ | ||
@Test | ||
void filterExpression_overlappingReferences_ascendingCase() { | ||
// | ||
EnoQuestionnaire enoQuestionnaire = new EnoQuestionnaire(); | ||
Filter filter = new Filter(); | ||
CalculatedExpression calculatedExpression= new CalculatedExpression(); | ||
calculatedExpression.setValue("foo-ref-1 = 1 and foo-ref-10 = 1"); | ||
Set<BindingReference> bindingReferences = new LinkedHashSet<>(); | ||
bindingReferences.add(new BindingReference("foo-ref-1", "FOO_A")); | ||
bindingReferences.add(new BindingReference("foo-ref-10", "FOO_K")); | ||
calculatedExpression.setBindingReferences(bindingReferences); | ||
filter.setExpression(calculatedExpression); | ||
enoQuestionnaire.getFilters().add(filter); | ||
// | ||
new DDIResolveVariableReferencesInExpressions().apply(enoQuestionnaire); | ||
// | ||
assertEquals("FOO_A = 1 and FOO_K = 1", | ||
enoQuestionnaire.getFilters().get(0).getExpression().getValue()); | ||
} | ||
|
||
/** | ||
* Same test with binding references in the reverse order. (These two tests could have been a parametrized | ||
* test, but it would have been harder to read.) | ||
*/ | ||
@Test | ||
void filterExpression_overlappingReferences_descendingCase() { | ||
// | ||
EnoQuestionnaire enoQuestionnaire = new EnoQuestionnaire(); | ||
Filter filter = new Filter(); | ||
CalculatedExpression calculatedExpression= new CalculatedExpression(); | ||
calculatedExpression.setValue("foo-ref-1 = 1 and foo-ref-10 = 1"); | ||
Set<BindingReference> bindingReferences = new LinkedHashSet<>(); | ||
bindingReferences.add(new BindingReference("foo-ref-10", "FOO_K")); | ||
bindingReferences.add(new BindingReference("foo-ref-1", "FOO_A")); | ||
calculatedExpression.setBindingReferences(bindingReferences); | ||
filter.setExpression(calculatedExpression); | ||
enoQuestionnaire.getFilters().add(filter); | ||
// | ||
new DDIResolveVariableReferencesInExpressions().apply(enoQuestionnaire); | ||
// | ||
assertEquals("FOO_A = 1 and FOO_K = 1", | ||
enoQuestionnaire.getFilters().get(0).getExpression().getValue()); | ||
} | ||
|
||
} | ||
|
||
} |