Skip to content

Commit

Permalink
[ivaiMqVb] CALL apoc.export.cypher.schema(null, { stream: true }) ret…
Browse files Browse the repository at this point in the history
…urns nothing (neo4j/apoc#374) (#3571)
  • Loading branch information
vga91 authored May 11, 2023
1 parent 6f3dac5 commit 258c447
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/apoc/export/cypher/ExportCypher.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public Stream<DataProgressInfo> query(@Name("query") String query, @Name(value =
public Stream<DataProgressInfo> schema(@Name(value = "file",defaultValue = "") String fileName, @Name(value = "config",defaultValue = "{}") Map<String, Object> config) throws IOException {
if (Util.isNullOrEmpty(fileName)) fileName=null;
String source = String.format("database: nodes(%d), rels(%d)", Util.nodeCount(tx), Util.relCount(tx));
// streamStatements
config.put("stream", false);
config.put("streamStatements", false);
return exportCypher(fileName, source, new DatabaseSubGraph(tx), new ExportConfig(config), true);
}

Expand Down
47 changes: 40 additions & 7 deletions core/src/test/java/apoc/export/cypher/ExportCypherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;

import static apoc.export.cypher.ExportCypherTest.ExportCypherResults.*;
Expand Down Expand Up @@ -176,7 +177,7 @@ public void testExportQueryCypherForNeo4j() throws Exception {
private static String readFile(String fileName) {
return readFile(fileName, CompressionAlgo.NONE);
}

private static String readFile(String fileName, CompressionAlgo algo) {
return BinaryTestUtil.readFileToString(new File(directory, fileName), UTF_8, algo);
}
Expand Down Expand Up @@ -263,7 +264,7 @@ public void testExportAllCypherSchemaWithSaveConstraintNames() throws Exception
final Map<String, Object> config = new HashMap<>(ExportCypherTest.exportConfig);
config.put("saveConstraintNames", true);
String fileName = "all.cypher";
TestUtil.testCall(db, "CALL apoc.export.cypher.all($file,$config)",
TestUtil.testCall(db, "CALL apoc.export.cypher.all($file,$config)",
map("file", fileName, "config", config),
(r) -> assertResults(fileName, r, "database"));
final String expectedFile = String.format(EXPECTED_SCHEMA_WITH_NAMES, StringUtils.EMPTY, StringUtils.EMPTY, " consBar", " uniqueConstraintComposite");
Expand Down Expand Up @@ -379,6 +380,30 @@ public void testExportSchemaCypher() throws Exception {
assertEquals(EXPECTED_ONLY_SCHEMA_NEO4J_SHELL, readFile(fileName));
}

@Test
public void testExportSchemaCypherWithStreamTrue() {
Consumer<Map<String, Object>> resultAssertion = (r) -> {
Object actual = r.get("cypherStatements");
assertEquals(EXPECTED_ONLY_SCHEMA_CYPHER_SHELL, actual);
};

// with {stream: true}
TestUtil.testCall(db, "CALL apoc.export.cypher.schema(null, {stream: true})",
resultAssertion);

// with {streamStatements: true}
TestUtil.testCall(db, "CALL apoc.export.cypher.schema(null, {streamStatements: true})",
resultAssertion);

// assert `schemaStatements` of `apoc.export.cypher.all`
// is equal to `cypher.schema` output plus the `CREATE CONSTRAINT UNIQUE_IMPORT_NAME ...` statement
TestUtil.testCall(db, "CALL apoc.export.cypher.all(null, { separateFiles: true, stream: true})",
r -> {
Object actual = r.get("schemaStatements");
assertEquals(convertToCypherShellFormat(EXPECTED_SCHEMA), actual);
});
}

@Test
public void testExportSchemaCypherWithIdxAndConsNames() throws Exception {
final Map<String, Object> config = new HashMap<>(ExportCypherTest.exportConfig);
Expand Down Expand Up @@ -1101,7 +1126,7 @@ static class ExportCypherResults {
"BEGIN%n" +
"DROP CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT (node.`UNIQUE IMPORT ID`) IS UNIQUE;%n" +
"COMMIT%n");

static final String EXPECTED_SCHEMA_WITH_RELS_OPTIMIZED = String.format("BEGIN%n" +
"CREATE BTREE INDEX FOR (node:Bar) ON (node.first_name, node.last_name);%n" +
"CREATE BTREE INDEX FOR (node:Foo) ON (node.name);%n" +
Expand All @@ -1111,7 +1136,7 @@ static class ExportCypherResults {
"CREATE CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT (node.`UNIQUE IMPORT ID`) IS UNIQUE;%n" +
"COMMIT%n" +
"SCHEMA AWAIT%n");

static final String EXPECTED_SCHEMA_WITH_RELS_AND_NAME_OPTIMIZED = String.format("BEGIN%n" +
"CREATE BTREE INDEX barIndex FOR (node:Bar) ON (node.first_name, node.last_name);%n" +
"CREATE BTREE INDEX fooIndex FOR (node:Foo) ON (node.name);%n" +
Expand All @@ -1121,7 +1146,7 @@ static class ExportCypherResults {
"CREATE CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT (node.`UNIQUE IMPORT ID`) IS UNIQUE;%n" +
"COMMIT%n" +
"SCHEMA AWAIT%n");

static final String EXPECTED_SCHEMA_WITH_RELS_AND_IF_NOT_EXISTS = String.format("BEGIN%n" +
"CREATE BTREE INDEX IF NOT EXISTS FOR (node:Bar) ON (node.first_name, node.last_name);%n" +
"CREATE BTREE INDEX IF NOT EXISTS FOR (node:Foo) ON (node.name);%n" +
Expand Down Expand Up @@ -1531,12 +1556,20 @@ static class ExportCypherResults {
":begin%n" +
"DROP CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT (node.`UNIQUE IMPORT ID`) IS UNIQUE;%n" +
":commit%n", 3);
static final String EXPECTED_QUERY_PARAMS_ODD = (EXPECTED_SCHEMA + EXPECTED_NODES_PARAMS_ODD + EXPECTED_RELATIONSHIPS_PARAMS_ODD + EXPECTED_DROP_PARAMS_ODD)

public static final String EXPECTED_QUERY_PARAMS_ODD = (EXPECTED_SCHEMA + EXPECTED_NODES_PARAMS_ODD + EXPECTED_RELATIONSHIPS_PARAMS_ODD + EXPECTED_DROP_PARAMS_ODD)
.replace(NEO4J_SHELL.begin(), CYPHER_SHELL.begin())
.replace(NEO4J_SHELL.commit(), CYPHER_SHELL.commit())
.replace(NEO4J_SHELL.schemaAwait(), EXPECTED_INDEXES_AWAIT)
.replace(NEO4J_SHELL.schemaAwait(), CYPHER_SHELL.schemaAwait());

public static String convertToCypherShellFormat(String input) {
return input
.replace( NEO4J_SHELL.begin(), CYPHER_SHELL.begin() )
.replace( NEO4J_SHELL.commit(), CYPHER_SHELL.commit() )
.replace( NEO4J_SHELL.schemaAwait(), EXPECTED_INDEXES_AWAIT )
.replace( NEO4J_SHELL.schemaAwait(), CYPHER_SHELL.schemaAwait() );
}
}

}

0 comments on commit 258c447

Please sign in to comment.