Skip to content

Commit

Permalink
Set line separator to \n when using readFile to read expected results…
Browse files Browse the repository at this point in the history
… for tests (#2891)

* Set line separator to \n when reading expected result from file in testTravelX.pure

* Fix TestGraphQLFileGeneration so it works independently of OS line separator
  • Loading branch information
kevin-m-knight-gs authored Jun 11, 2024
1 parent 7265113 commit b337b32
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@

package org.finos.legend.engine.query.graphQL.api.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.eclipse.collections.impl.*;
import org.eclipse.collections.impl.factory.*;
import org.eclipse.collections.impl.factory.Iterables;
import org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel;
import org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.fileGeneration.FileGenerationSpecification;
import org.finos.legend.engine.query.graphQL.api.format.generation.GraphQLGenerationExtension;
import org.finos.legend.engine.shared.core.ObjectMapperFactory;
import org.finos.legend.engine.shared.core.deployment.DeploymentMode;
import org.finos.legend.engine.shared.core.identity.Identity;
import org.finos.legend.engine.shared.core.identity.factory.*;
import org.finos.legend.pure.generated.Root_meta_pure_generation_metamodel_GenerationOutput;
import org.junit.Assert;
import org.junit.Test;

import java.io.InputStream;
import java.util.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.regex.Pattern;

import static org.finos.legend.pure.generated.core_external_query_graphql_deprecated_tests_generationTests.Root_meta_external_query_graphQL_generation_tests_constants_comparisonExpressions_String_MANY__String_1_;

Expand All @@ -39,81 +42,97 @@ public class TestGraphQLFileGeneration
@Test
public void testSimpleGraphQL()
{
try
{
PureModelContextData pureModelContextData = getProtocol("simpleFileGeneration.json");
PureModel pureModel = new PureModel(pureModelContextData, Identity.getAnonymousIdentity().getName(), DeploymentMode.TEST);
FileGenerationSpecification fileGeneration = pureModelContextData.getElementsOfType(FileGenerationSpecification.class).get(0);
GraphQLGenerationExtension graphQLGenerationExtension = new GraphQLGenerationExtension();
List<? extends Root_meta_pure_generation_metamodel_GenerationOutput> outputs = graphQLGenerationExtension.generateFromElement(fileGeneration, pureModel.getContext());
PureModelContextData pureModelContextData = getProtocol("simpleFileGeneration.json");
PureModel pureModel = new PureModel(pureModelContextData, Identity.getAnonymousIdentity().getName(), DeploymentMode.TEST);
FileGenerationSpecification fileGeneration = pureModelContextData.getElementsOfType(FileGenerationSpecification.class).get(0);
GraphQLGenerationExtension graphQLGenerationExtension = new GraphQLGenerationExtension();
List<? extends Root_meta_pure_generation_metamodel_GenerationOutput> outputs = graphQLGenerationExtension.generateFromElement(fileGeneration, pureModel.getContext());

Assert.assertEquals(outputs.size(), 5);
outputs.forEach(o ->
{
if (o._fileName().endsWith("scalars.graphql"))
{
Assert.assertEquals(getResourceAsString("scalars.graphql"), o._content());
}
else if (o._fileName().endsWith("primitive_comparisons.graphql"))
{
Assert.assertEquals(Root_meta_external_query_graphQL_generation_tests_constants_comparisonExpressions_String_MANY__String_1_(Iterables.iList("Int", "String"), pureModel.getExecutionSupport()), o._content());
}
else
{
Assert.assertEquals(getResourceAsString(o._fileName().substring(o._fileName().lastIndexOf('/') + 1)), o._content());
}
});
}
catch (Exception e)
Assert.assertEquals(outputs.size(), 5);
outputs.forEach(o ->
{
throw new RuntimeException(e);
}
if (o._fileName().endsWith("scalars.graphql"))
{
Assert.assertEquals(getResourceAsString("scalars.graphql"), o._content());
}
else if (o._fileName().endsWith("primitive_comparisons.graphql"))
{
Assert.assertEquals(Root_meta_external_query_graphQL_generation_tests_constants_comparisonExpressions_String_MANY__String_1_(Iterables.iList("Int", "String"), pureModel.getExecutionSupport()), o._content());
}
else
{
Assert.assertEquals(getResourceAsString(o._fileName().substring(o._fileName().lastIndexOf('/') + 1)), o._content());
}
});
}

@Test
public void testScalarGraphQL()
{
try
{
PureModelContextData pureModelContextData = getProtocol("scalarFileGeneration.json");
PureModel pureModel = new PureModel(pureModelContextData, Identity.getAnonymousIdentity().getName(), DeploymentMode.TEST);
FileGenerationSpecification fileGeneration = pureModelContextData.getElementsOfType(FileGenerationSpecification.class).get(0);
GraphQLGenerationExtension graphQLGenerationExtension = new GraphQLGenerationExtension();
List<? extends Root_meta_pure_generation_metamodel_GenerationOutput> outputs = graphQLGenerationExtension.generateFromElement(fileGeneration, pureModel.getContext());
PureModelContextData pureModelContextData = getProtocol("scalarFileGeneration.json");
PureModel pureModel = new PureModel(pureModelContextData, Identity.getAnonymousIdentity().getName(), DeploymentMode.TEST);
FileGenerationSpecification fileGeneration = pureModelContextData.getElementsOfType(FileGenerationSpecification.class).get(0);
GraphQLGenerationExtension graphQLGenerationExtension = new GraphQLGenerationExtension();
List<? extends Root_meta_pure_generation_metamodel_GenerationOutput> outputs = graphQLGenerationExtension.generateFromElement(fileGeneration, pureModel.getContext());

Assert.assertEquals(outputs.size(), 3);
outputs.forEach(o ->
Assert.assertEquals(outputs.size(), 3);
outputs.forEach(o ->
{
if (o._fileName().endsWith("scalars.graphql"))
{
if (o._fileName().endsWith("scalars.graphql"))
{
Assert.assertEquals(getResourceAsString("scalars.graphql"), o._content());
}
else if (o._fileName().endsWith("primitive_comparisons.graphql"))
{
Assert.assertEquals(Root_meta_external_query_graphQL_generation_tests_constants_comparisonExpressions_String_MANY__String_1_(Iterables.iList("BigDecimal","Date", "DateTime"), pureModel.getExecutionSupport()), o._content());
}
else
{
Assert.assertEquals(getResourceAsString(o._fileName().substring(o._fileName().lastIndexOf('/') + 1)), o._content());
}
});
Assert.assertEquals(getResourceAsString("scalars.graphql"), o._content());
}
else if (o._fileName().endsWith("primitive_comparisons.graphql"))
{
Assert.assertEquals(Root_meta_external_query_graphQL_generation_tests_constants_comparisonExpressions_String_MANY__String_1_(Iterables.iList("BigDecimal","Date", "DateTime"), pureModel.getExecutionSupport()), o._content());
}
else
{
Assert.assertEquals(getResourceAsString(o._fileName().substring(o._fileName().lastIndexOf('/') + 1)), o._content());
}
});
}

private PureModelContextData getProtocol(String fileName)
{
String jsonString = getResourceAsString(fileName, null);
try
{
return ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports().readValue(jsonString, PureModelContextData.class);
}
catch (Exception e)
catch (IOException e)
{
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
}

private PureModelContextData getProtocol(String fileName) throws JsonProcessingException
private String getResourceAsString(String fileName)
{
String jsonString = this.getResourceAsString(fileName);
return ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports().readValue(jsonString, PureModelContextData.class);
return getResourceAsString(fileName, "\n");
}

private String getResourceAsString(String fileName)
private String getResourceAsString(String fileName, String lineSeparator)
{
InputStream inputStream = TestGraphQLFileGeneration.class.getResourceAsStream(fileName);
Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
URL url = getClass().getResource(fileName);
if (url == null)
{
throw new RuntimeException("Cannot find: " + fileName);
}
StringBuilder builder = new StringBuilder();
try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))
{
char[] buffer = new char[8192];
int read;
while ((read = reader.read(buffer)) != -1)
{
builder.append(buffer, 0, read);
}
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}

return (lineSeparator == null) ? builder.toString() : Pattern.compile("\\R").matcher(builder).replaceAll(lineSeparator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import meta::pure::metamodel::serialization::grammar::*;
function <<test.Test>> meta::external::query::graphQL::binding::toPure::introspection::tests::testTravelFromIntrospection():Boolean[1]
{
let str = readFile('/core_external_query_graphql/binding/toPure/introspection/tests/travel/travelx.json')->toOne();
let expected = readFile('/core_external_query_graphql/binding/toPure/introspection/tests/travel/travelResult.txt')->toOne();
let expected = readFile('/core_external_query_graphql/binding/toPure/introspection/tests/travel/travelResult.txt', '\n')->toOne();
let res = $str->meta::json::fromJson(__Schema)->meta::external::query::graphQL::binding::toPure::introspection::buildPureTypesFromGraphQLSchema('');
assertEquals($expected, $res->sortBy(r|$r->elementToPath())->map(r|$r->printType())->joinStrings('\n'));
}
}

0 comments on commit b337b32

Please sign in to comment.