InseeFr / Trevas

Transformation engine and validator for statistics.
MIT License
10 stars 5 forks source link

Quiet failures with un-implemented operator #156

Closed nsenave closed 2 years ago

nsenave commented 2 years ago

When evaluating an operator not yet supported, there is sometimes no exception thrown.

Test example with current_date operator using Trevas 0.3.0 (& junit 5)

import fr.insee.vtl.model.Dataset;
import fr.insee.vtl.model.InMemoryDataset;
import fr.insee.vtl.model.Structured;
import org.junit.jupiter.api.Test;

import javax.script.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SomeTests {

    @Test
    public void testCurrentDate() throws ScriptException {
        //
        Dataset fooDataset = new InMemoryDataset(
                List.of(List.of(1L, "foo")),
                List.of(new Structured.Component("ID", Long.class, Dataset.Role.IDENTIFIER),
                        new Structured.Component("FOO", String.class, Dataset.Role.MEASURE))
        );
        //
        SimpleBindings bindings = new SimpleBindings();
        bindings.put("ds", fooDataset);
        //
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("vtl");
        engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        engine.eval("ds := ds [ calc FOO_YEAR := (substr(cast(current_date(),string),1,4)) ];");

        //
        String currentYear = DateTimeFormatter.ofPattern("yyyy").format(LocalDateTime.now());
        assertEquals(currentYear, ((Dataset) bindings.get("ds")).getDataPoints().get(0).get("FOO_YEAR"));
    }
}

This test logically fails since current_date is not supported in 0.3.0, yet there is neither warning nor exception thrown. (The documentation doesn't cover this case)

nsenave commented 2 years ago

Erratum thanks to @NicoLaval 's answer at issue #155: the operator current_date is actually supported in 0.3.0.

The syntax cast(current_date(), string) is incorrect since date pattern is missing.

Test succeeds when replacing it with cast(current_date(), string, "yyyy-MM-dd")

Still, an exception or warning might be helpful in the wrong case.