|
6 | 6 |
|
7 | 7 | import fr.insee.vtl.model.Dataset; |
8 | 8 | import fr.insee.vtl.model.InMemoryDataset; |
| 9 | +import fr.insee.vtl.model.PersistentDataset; |
9 | 10 | import fr.insee.vtl.model.Structured; |
| 11 | +import java.util.Arrays; |
10 | 12 | import java.util.List; |
11 | 13 | import java.util.Map; |
12 | 14 | import java.util.stream.Collectors; |
13 | | -import javax.script.ScriptContext; |
14 | | -import javax.script.ScriptEngine; |
15 | | -import javax.script.ScriptEngineManager; |
16 | | -import javax.script.ScriptException; |
| 15 | +import javax.script.*; |
17 | 16 | import org.junit.jupiter.api.BeforeEach; |
18 | 17 | import org.junit.jupiter.api.Test; |
19 | 18 |
|
@@ -247,4 +246,45 @@ public void testUnionMultiple() throws ScriptException { |
247 | 246 | Map.of("name", "Franck2", "age", 12L, "weight", 9L), |
248 | 247 | Map.of("name", "Hadrien2", "age", 10L, "weight", 11L)); |
249 | 248 | } |
| 249 | + |
| 250 | + @Test |
| 251 | + public void testUnion456Issue() throws ScriptException { |
| 252 | + InMemoryDataset multimodeDs = |
| 253 | + new InMemoryDataset( |
| 254 | + List.of( |
| 255 | + new Structured.Component("interrogationId", String.class, Dataset.Role.IDENTIFIER), |
| 256 | + new Structured.Component("LOOP", String.class, Dataset.Role.IDENTIFIER), |
| 257 | + new Structured.Component("LOOP.FOO1", String.class, Dataset.Role.MEASURE), |
| 258 | + new Structured.Component("FOO", String.class, Dataset.Role.MEASURE)), |
| 259 | + Arrays.asList("T01", "LOOP-01", "foo11", "foo1"), |
| 260 | + Arrays.asList("T01", "LOOP-02", "foo12", "foo1"), |
| 261 | + Arrays.asList("T02", null, "foo21", "foo2")); |
| 262 | + |
| 263 | + engine.put("$vtl.engine.use_dag", "false"); |
| 264 | + ScriptContext context = engine.getContext(); |
| 265 | + context.getBindings(ScriptContext.ENGINE_SCOPE).put("MULTIMODE", multimodeDs); |
| 266 | + |
| 267 | + engine.eval( |
| 268 | + "TEMP_RACINE := MULTIMODE [keep interrogationId, FOO];\n" |
| 269 | + + "RACINE := union(TEMP_RACINE, TEMP_RACINE) ;\n" |
| 270 | + + "TEMP_LOOP := MULTIMODE [keep interrogationId, LOOP, LOOP.FOO1]\n" |
| 271 | + + " [filter LOOP <> \"\"]\n" |
| 272 | + + " [rename LOOP.FOO1 to FOO1];\n" |
| 273 | + + "LOOP <- union(TEMP_LOOP, TEMP_LOOP);"); |
| 274 | + |
| 275 | + Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); |
| 276 | + Map<String, PersistentDataset> persitentDatasets = |
| 277 | + bindings.entrySet().stream() |
| 278 | + .filter(entry -> entry.getValue() instanceof PersistentDataset) |
| 279 | + .collect(Collectors.toMap(Map.Entry::getKey, e -> (PersistentDataset) e.getValue())); |
| 280 | + |
| 281 | + assertThat(persitentDatasets.keySet().size()).isEqualTo(1); |
| 282 | + assertThat(persitentDatasets.containsKey("LOOP")).isTrue(); |
| 283 | + |
| 284 | + Dataset loopDs = persitentDatasets.get("LOOP"); |
| 285 | + |
| 286 | + assertThat(loopDs.getDataAsList().size()).isEqualTo(2); |
| 287 | + assertThat(loopDs.getDataAsList().stream().map(l -> l.get(1))) |
| 288 | + .isEqualTo(List.of("LOOP-01", "LOOP-02")); |
| 289 | + } |
250 | 290 | } |
0 commit comments