Skip to content

Commit

Permalink
Test signatures with Cypher versions
Browse files Browse the repository at this point in the history
  • Loading branch information
loveleif committed Dec 11, 2024
1 parent f6a13c9 commit 5356f2e
Show file tree
Hide file tree
Showing 6 changed files with 21,044 additions and 9,154 deletions.
94 changes: 64 additions & 30 deletions core/src/test/java/apoc/ArgumentDescriptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,27 @@
import apoc.version.Version;
import apoc.warmup.Warmup;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.configuration.GraphDatabaseInternalSettings;
import org.neo4j.configuration.GraphDatabaseSettings;
import org.neo4j.cypher.internal.CypherVersion;
import org.neo4j.test.rule.DbmsRule;
import org.neo4j.test.rule.ImpermanentDbmsRule;

public class ArgumentDescriptionsTest {
private final ObjectMapper json =
new ObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

@Rule
public DbmsRule db = new ImpermanentDbmsRule()
.withSetting(GraphDatabaseSettings.procedure_unrestricted, singletonList("apoc.*"));
.withSetting(GraphDatabaseSettings.procedure_unrestricted, singletonList("apoc.*"))
.withSetting(GraphDatabaseInternalSettings.enable_experimental_cypher_versions, true);

@Before
public void setUp() {
Expand Down Expand Up @@ -189,49 +195,77 @@ public void teardown() {
}

@Test
public void functionArgumentDescriptionsTest() throws IOException {
final var query =
"""
SHOW FUNCTIONS YIELD
name, category, description, signature, isBuiltIn,
argumentDescription, returnDescription, aggregating,
isDeprecated, deprecatedBy
WHERE name STARTS WITH 'apoc'
RETURN *
""";
final var result =
db.executeTransactionally(query, Map.of(), r -> r.stream().toList());
public void functionArgumentDescriptionsDefaultVersion() throws IOException {
assertFunctionDescriptions("", "/functions/cypher5/functions.json");
}

final var json = new ObjectMapper();
final var expected =
json.reader().readTree(ArgumentDescriptionsTest.class.getResourceAsStream("/functions.json"));
final var actual = json.valueToTree(result);
// Uncomment to print out how the file should look :)
// System.out.println("Actual:\n" + json.writer().withDefaultPrettyPrinter().writeValueAsString(actual));
for (int i = 0; i < expected.size(); ++i) assertThat(expected.get(i)).isEqualTo(actual.get(i));
assertThat(actual).isEqualTo(expected);
// Convert to ParametrizedTest with EnumSource in junit 5
@Test
public void functionArgumentDescriptions() throws IOException {
for (final var version : CypherVersion.values()) {
assertFunctionDescriptions(
"CYPHER " + version.versionName,
"/functions/cypher%s/functions.json".formatted(version.versionName));
}
}

@Test
public void procedureArgumentDescriptionsTest() throws IOException {
public void procedureArgumentDescriptionsDefaultVersion() throws IOException {
assertProcedureDescriptions("", "/procedures/cypher5/procedures.json");
}

// Convert to ParametrizedTest with EnumSource in junit 5
@Test
public void procedureArgumentDescriptions() throws IOException {
for (final var version : CypherVersion.values()) {
assertProcedureDescriptions(
"CYPHER " + version.versionName,
"/procedures/cypher%s/procedures.json".formatted(version.versionName));
}
}

private void assertProcedureDescriptions(String preparserOpts, String expectedPath) throws IOException {
final var query =
"""
%s
SHOW PROCEDURES YIELD
name, description, signature, argumentDescription,
returnDescription, isDeprecated, deprecatedBy
WHERE name STARTS WITH 'apoc'
RETURN *
""";
ORDER BY name
"""
.formatted(preparserOpts);
assertResultAsJsonEquals(query, expectedPath);
}

private void assertFunctionDescriptions(String preparserOpts, String expectedPath) throws IOException {
final var query =
"""
%s
SHOW FUNCTIONS YIELD
name, category, description, signature, isBuiltIn,
argumentDescription, returnDescription, aggregating,
isDeprecated, deprecatedBy
WHERE name STARTS WITH 'apoc'
RETURN *
ORDER BY name
"""
.formatted(preparserOpts);
assertResultAsJsonEquals(query, expectedPath);
}

private void assertResultAsJsonEquals(String query, String expectedPath) throws IOException {
final var result =
db.executeTransactionally(query, Map.of(), r -> r.stream().toList());

final var json = new ObjectMapper();
final var expected =
json.reader().readTree(ArgumentDescriptionsTest.class.getResourceAsStream("/procedures.json"));
final var expected = json.reader().readTree(getClass().getResourceAsStream(expectedPath));
final var actual = json.valueToTree(result);

// Uncomment to print out how the file should look :)
// System.out.println("Actual:\n" + json.writer().withDefaultPrettyPrinter().writeValueAsString(actual));
for (int i = 0; i < expected.size(); ++i) assertThat(expected.get(i)).isEqualTo(actual.get(i));
assertThat(actual).isEqualTo(expected);
// System.out.println("Actual %s:%n%s".formatted(query,
// json.writer().withDefaultPrettyPrinter().writeValueAsString(actual)));
for (int i = 0; i < expected.size() && i < actual.size(); ++i)
assertThat(expected.get(i)).describedAs(query).isEqualTo(actual.get(i));
assertThat(actual).describedAs(query).isEqualTo(expected);
}
}
Loading

0 comments on commit 5356f2e

Please sign in to comment.