Skip to content

Commit

Permalink
add running testable tests in showcase tests (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauricioUyaguari authored Nov 14, 2023
1 parent cb2c3d1 commit 0954f39
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Mapping mapping::ModelToModelMapping
}#
];
},
ExpectedToFail:
IBMData2:
{
doc: '';
data:
Expand All @@ -275,7 +275,7 @@ Mapping mapping::ModelToModelMapping
ExternalFormat
#{
contentType: 'application/json';
data: '{\n "employees" : [ {\n "fullName" : "John Smith"\n } ],\n "name" : "IBMWhoops",\n "myLegalName()" : "my name is: IBM"\n}';
data: '{\n "employees" : [ {\n "fullName" : "John Smith"\n } ],\n "name" : "IBM",\n "myLegalName()" : "my name is: IBM"\n}';
}#;
}#
];
Expand Down
26 changes: 26 additions & 0 deletions showcases/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,33 @@
<version>${legend.engine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-extensions-collection-execution</artifactId>
<version>${legend.engine.version}</version>
<scope>test</scope>
</dependency>

<!-- DEPENDENCIES BELOW ARE MISSING FROM COLLECTIONS ABOVE -->
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-test-runner-service</artifactId>
<version>${legend.engine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-test-runner-mapping</artifactId>
<version>${legend.engine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-configuration</artifactId>
<version>${legend.engine.version}</version>
<scope>test</scope>
</dependency>
<!-- -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
86 changes: 78 additions & 8 deletions showcases/src/test/java/org/example/ShowcaseCompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,48 @@
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Sets;
import org.finos.legend.engine.language.pure.compiler.Compiler;
import org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel;
import org.finos.legend.engine.language.pure.grammar.from.PureGrammarParser;
import org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer;
import org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposerContext;
import org.finos.legend.engine.language.pure.modelManager.ModelManager;
import org.finos.legend.engine.protocol.pure.v1.ProtocolToClassifierPathLoader;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement;
import org.finos.legend.engine.protocol.pure.v1.model.test.assertion.status.AssertFail;
import org.finos.legend.engine.protocol.pure.v1.model.test.assertion.status.AssertionStatus;
import org.finos.legend.engine.protocol.pure.v1.model.test.assertion.status.EqualToJsonAssertFail;
import org.finos.legend.engine.protocol.pure.v1.model.test.result.TestError;
import org.finos.legend.engine.protocol.pure.v1.model.test.result.TestExecuted;
import org.finos.legend.engine.protocol.pure.v1.model.test.result.TestExecutionStatus;
import org.finos.legend.engine.protocol.pure.v1.model.test.result.TestResult;
import org.finos.legend.engine.shared.core.api.grammar.RenderStyle;
import org.finos.legend.engine.shared.core.deployment.DeploymentMode;
import org.finos.legend.engine.testable.TestableRunner;
import org.finos.legend.engine.testable.extension.TestableRunnerExtensionLoader;
import org.finos.legend.engine.testable.model.RunTestsResult;
import org.finos.legend.engine.testable.model.RunTestsTestableInput;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.PathMatcher;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

/**
Expand All @@ -34,6 +53,8 @@
@RunWith(Parameterized.class)
public class ShowcaseCompilerTest
{
private final Set<String> testableClassifiers = TestableRunnerExtensionLoader.getClassifierPathToTestableRunnerMap().keySet();
private final Map<Class<? extends PackageableElement>, String> classifierPathMap = ProtocolToClassifierPathLoader.getProtocolClassToClassifierMap();
private static final PathMatcher PURE_FILE_MATCHER = FileSystems.getDefault().getPathMatcher("glob:*.pure");
@Parameter
public Path showcasePath;
Expand Down Expand Up @@ -89,17 +110,66 @@ public void processShowcaseFile() throws IOException

assumeFalse(pureGrammar.isEmpty());

/*
* TODO:
* - Run all testables within a showcase
*/
PureModelContextData pureModelContextData = PureGrammarParser.newInstance().parseModel(pureGrammar, "", 0, 0, true);
PureGrammarComposer grammarComposer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().withRenderStyle(RenderStyle.PRETTY).build());

// compile
Compiler.compile(pureModelContextData, DeploymentMode.PROD, Lists.mutable.empty());
PureModel puremodel = Compiler.compile(pureModelContextData, DeploymentMode.PROD, Lists.mutable.empty());

// Grammar composer adds a trailing newline
assertEquals(pureGrammar + "\n", grammarComposer.renderPureModelContextData(pureModelContextData));

// run tests
runAllTests(puremodel, pureModelContextData);
}


// TODO Move to legend-engine
public void runAllTests(PureModel puremodel, PureModelContextData data)
{

for (PackageableElement packageableElement: data.getElements())
{
Assert.assertTrue("Error getting classifier path for element '" + packageableElement.getPath() + "'", classifierPathMap.containsKey(packageableElement.getClass()));
String classifierPath = classifierPathMap.get(packageableElement.getClass());
if(testableClassifiers.contains(classifierPath))
{
TestableRunner runner = new TestableRunner(new ModelManager(DeploymentMode.PROD));
RunTestsTestableInput runTestsTestableInput = new RunTestsTestableInput();
runTestsTestableInput.testable = packageableElement.getPath();
RunTestsResult testsResult = runner.doTests(Collections.singletonList(runTestsTestableInput), puremodel, data);
for (TestResult result: testsResult.results)
{
if (result instanceof TestError)
{
TestError testError = (TestError) result;
fail("Error running test '" + testError.atomicTestId + "' in suite " + testError.testSuiteId + " in testable " + testError.testable + " : " + testError.error);
}
if (result instanceof TestExecuted) {
TestExecuted testExecuted = (TestExecuted) result;
if (testExecuted.testExecutionStatus != TestExecutionStatus.PASS)
{
StringBuilder failMessage = new StringBuilder();
failMessage.append("Error running test '" + result.atomicTestId + "' in suite " + result.testSuiteId + " in testable " + result.testable + ".");
for(AssertionStatus assertionStatus: testExecuted.assertStatuses)
{
if(assertionStatus instanceof AssertFail)
{
failMessage.append(assertionStatus.id).append(": FAILURE\n").append(((AssertFail) assertionStatus).message);
if (assertionStatus instanceof EqualToJsonAssertFail)
{
EqualToJsonAssertFail fail = (EqualToJsonAssertFail) assertionStatus;
failMessage.append("\nexpected: ").append(fail.expected);
failMessage.append("\nactual: ").append(fail.actual);
}
}
}
Assert.fail(failMessage.toString());
}
}
}
}
}
}

}

0 comments on commit 0954f39

Please sign in to comment.