Search in sources :

Example 1 with PureGrammarComposer

use of org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer in project legend-engine by finos.

the class TestRelationalGrammarComposer method testMappingWithTestCSV.

@Test
public void testMappingWithTestCSV() throws Exception {
    PureModelContextData context = objectMapper.readValue(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("mappingWithTestsCSV.json")), PureModelContextData.class);
    PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
    String formatted = grammarTransformer.renderPureModelContextData(context);
    String expected = "###Mapping\n" + "Mapping meta::pure::mapping::modelToModel::test::simple::simpleModelMapping\n" + "(\n" + "  MappingTests\n" + "  [\n" + "    test2\n" + "    (\n" + "      query: |model::domain::Target.all()->graphFetchChecked(#{model::domain::Target{name}}#)->serialize(#{model::domain::Target{name}}#);\n" + "      data:\n" + "      [\n" + "        <Relational, CSV, aa::db, \n" + "          'default\\n'+\n" + "          'PersonTable\\n'+\n" + "          'id,lastName\\n'+\n" + "          '1,Doe;\\n'+\n" + "          '2,Doe2\\n'+\n" + "          '\\n\\n\\n'\n" + "        >\n" + "      ];\n" + "      assert: '{\"defects\":[],\"value\":{\"name\":\"oneName 99\"},\"source\":{\"defects\":[],\"value\":{\"oneName\":\"oneName 99\"},\"source\":{\"number\":1,\"record\":\"{\\\"oneName\\\":\\\"oneName 99\\\",\\\"anotherName\\\":\\\"anotherName 17\\\",\\\"oneDate\\\":\\\"2020-04-13\\\",\\\"anotherDate\\\":\\\"2020-02-25\\\",\\\"oneNumber\\\":27,\\\"anotherNumber\\\":28}\"}}}';\n" + "    )\n" + "  ]\n" + ")\n";
    Assert.assertEquals(expected, formatted);
}
Also used : PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureGrammarComposer(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer) Test(org.junit.Test)

Example 2 with PureGrammarComposer

use of org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer in project legend-engine by finos.

the class TestRelationalGrammarComposer method TestMappingWithLeftOuterJoin.

@Test
public void TestMappingWithLeftOuterJoin() throws Exception {
    PureModelContextData context = objectMapper.readValue(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("mappingWithLeftOuterJoin.json")), PureModelContextData.class);
    PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
    String formatted = grammarTransformer.renderPureModelContextData(context);
    String expected = "###Mapping\n" + "Mapping testDB::test\n" + "(\n" + "  *meta::relational::tests::model::simple::Person[meta_relational_tests_model_simple_Person]: Relational\n" + "  {\n" + "    ~primaryKey\n" + "    (\n" + "      [meta::relational::tests::mapping::join::model::store::db]personTable.ID\n" + "    )\n" + "    ~mainTable [meta::relational::tests::mapping::join::model::store::db]personTable\n" + "    firstName: [meta::relational::tests::mapping::join::model::store::db]personTable.FIRSTNAME,\n" + "    lastName: [meta::relational::tests::mapping::join::model::store::db]personTable.LASTNAME,\n " + "   age: [meta::relational::tests::mapping::join::model::store::db]@Person_MiddleTable > (OUTER) [meta::relational::tests::mapping::join::model::store::db]@MiddleTable_PersonExtension | [meta::relational::tests::mapping::join::model::store::db]personExtensionTable.AGE\n" + "  }\n" + ")\n";
    Assert.assertEquals(expected, formatted);
}
Also used : PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureGrammarComposer(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer) Test(org.junit.Test)

Example 3 with PureGrammarComposer

use of org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer in project legend-engine by finos.

the class TestRelationalGrammarComposer method selfJoinTest.

@Test
public void selfJoinTest() throws Exception {
    PureModelContextData context = objectMapper.readValue(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("selfJoinTest.json")), PureModelContextData.class);
    PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
    String formatted = grammarTransformer.renderPureModelContextData(context);
    // Test checks that we do not output schema name before {target} - the grammar round trip tests do not exercise this code path as the graph
    // produced for the roundtrip always sets the schema name in the table alias to default.
    // This is testing out a graph as would be produced by tools such as Studio and checking that syntactically valid Pure is produced for self-joins
    String expected = "###Relational\n" + "Database test::db\n" + "(\n" + "  Schema mySchema\n" + "  (\n" + "    Table table1\n" + "    (\n" + "      col1 CHAR(32) PRIMARY KEY,\n" + "      col2 CHAR(32)\n" + "    )\n" + "  )\n" + "\n" + "  Join selfJoin(mySchema.table1.col1 = {target}.col2)\n" + ")\n";
    Assert.assertEquals(expected, formatted);
}
Also used : PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureGrammarComposer(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer) Test(org.junit.Test)

Example 4 with PureGrammarComposer

use of org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer in project legend-engine by finos.

the class TestSectionRoundtrip method testGrammarRoundtripWithoutSectionIndex.

@Test
public void testGrammarRoundtripWithoutSectionIndex() {
    ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();
    // NOTE: stress test to account for flakiness
    for (int i = 0; i < 500; i++) {
        StringBuilder code = new StringBuilder();
        for (int t = 200; t > 0; t--) {
            code.append("Class model::class").append(t).append("\n").append("{\n").append("  prop: String[1];\n").append("}\n");
            if (t != 1) {
                code.append("\n");
            }
        }
        PureModelContextData modelData = null;
        try {
            modelData = PureGrammarParser.newInstance().parseModel(code.toString());
            String json = objectMapper.writeValueAsString(modelData);
            modelData = objectMapper.readValue(json, PureModelContextData.class);
            List<PackageableElement> elements = ListIterate.select(modelData.getElements(), element -> !(element instanceof SectionIndex));
            modelData = PureModelContextData.newPureModelContextData(modelData.getSerializer(), modelData.getOrigin(), elements);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
        Assert.assertEquals(code.toString(), grammarTransformer.renderPureModelContextData(modelData));
    }
}
Also used : PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) SectionIndex(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureGrammarComposer(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer) Test(org.junit.Test)

Example 5 with PureGrammarComposer

use of org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer in project legend-engine by finos.

the class TestGrammarParser method testFromJson.

public static void testFromJson(Class<?> _class, String path, String code) {
    PureModelContextData modelData = null;
    try {
        String json = getJsonString(_class, path);
        modelData = objectMapper.readValue(json, PureModelContextData.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
    Assert.assertEquals(code, grammarTransformer.renderPureModelContextData(Objects.requireNonNull(modelData)));
}
Also used : IOException(java.io.IOException) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureGrammarComposer(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer)

Aggregations

PureGrammarComposer (org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer)11 PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)10 Test (org.junit.Test)6 IOException (java.io.IOException)2 PackageableElement (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Scope (io.opentracing.Scope)1 GlobalTracer (io.opentracing.util.GlobalTracer)1 Api (io.swagger.annotations.Api)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiParam (io.swagger.annotations.ApiParam)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1 Procedure (org.eclipse.collections.api.block.procedure.Procedure)1 MutableList (org.eclipse.collections.api.list.MutableList)1 MutableMap (org.eclipse.collections.api.map.MutableMap)1