Skip to content

Commit

Permalink
fixes #550 test call silent failure (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarmbruster authored and jexp committed Aug 25, 2017
1 parent 9728992 commit 6ea5396
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/test/java/apoc/export/csv/ExportCsvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ private void assertResults(File output, Map<String, Object> r, final String sour
assertEquals(source + ": nodes(3), rels(1)", r.get("source"));
assertEquals(output.getAbsolutePath(), r.get("file"));
assertEquals("csv", r.get("format"));
assertEquals(true, ((long) r.get("time")) > 0);
assertEquals(true, ((long) r.get("time")) >= 0);
}
}
2 changes: 1 addition & 1 deletion src/test/java/apoc/index/FulltextIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void tearDown() {
@Test
public void testNodes() throws Exception {
createData();
testCall(db,"CALL apoc.index.nodes('Person', 'name:jo*')",
testCall(db,"CALL apoc.index.nodes('Person', 'name:Jo*')",
(row) -> {
Node node = (Node) row.get("node");
assertEquals(true, node.hasLabel(Label.label(PERSON)));
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/apoc/monitor/IdsProcedureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public void testGetNodeIdsInUse() {
}

private void createData() {
testCall(db, "CREATE (n)", (row) -> {});
testCall(db, "CREATE (n)-[:REL_TYPE1]->(n2)", (row) -> {});
testCall(db, "CREATE (n)-[:REL_TYPE2]->(n2)", (row) -> {});
testCall(db, "CREATE (n) SET n.key = 123", (row) -> {});
db.execute("CREATE (n)");
db.execute("CREATE (n)-[:REL_TYPE1]->(n2)");
db.execute("CREATE (n)-[:REL_TYPE2]->(n2)");
db.execute("CREATE (n) SET n.key = 123");
}

}
70 changes: 24 additions & 46 deletions src/test/java/apoc/refactor/GraphRefactoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static apoc.util.MapUtil.map;
import static apoc.util.TestUtil.testCall;
import static apoc.util.TestUtil.testCallEmpty;
import static apoc.util.TestUtil.testResult;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -48,11 +49,11 @@ public void testDeleteOneNode() throws Exception {
ExecutionPlanDescription plan = db.execute("EXPLAIN MATCH (o:Person {ID:{oldID}}), (n:Person {ID:{newID}}) DELETE o RETURN o as node").getExecutionPlanDescription();
System.out.println(plan);
System.out.flush();
testCall(db, "EXPLAIN MATCH (o:Person {ID:{oldID}}), (n:Person {ID:{newID}}) DELETE o RETURN o as node",
testCall(db, "MATCH (o:Person {ID:{oldID}}), (n:Person {ID:{newID}}) DELETE o RETURN n as node",
map("oldID", 1L, "newID",2L),
(r) -> {
Node node = (Node) r.get("node");
assertEquals(id, node.getId());
assertNotEquals(id, node.getId());
assertEquals(true, node.hasLabel(Label.label("Person")));
assertEquals(2L, node.getProperty("ID"));
});
Expand Down Expand Up @@ -167,32 +168,34 @@ public void testNormalizeAsBoolean() throws Exception {
);
}

@Test
public void testCategorizeOutgoing() throws Exception {
private void categorizeWithDirection(Direction direction) {
db.execute(
"CREATE ({prop: 'A', k: 'a', id: 1}) " +
"CREATE ({prop: 'A', k: 'a', id: 2}) " +
"CREATE ({prop: 'C', k: 'c', id: 3}) " +
"CREATE ({ id: 4}) " +
"CREATE ({prop: 'B', k: 'b', id: 5}) " +
"CREATE ({prop: 'C', k: 'c', id: 6})").close();
"CREATE ({prop: 'A', k: 'a', id: 2}) " +
"CREATE ({prop: 'C', k: 'c', id: 3}) " +
"CREATE ({ id: 4}) " +
"CREATE ({prop: 'B', k: 'b', id: 5}) " +
"CREATE ({prop: 'C', k: 'c', id: 6})").close();

testCall(
db,
"CALL apoc.refactor.categorize('prop','IS_A',true,'Letter','name',['k'],1)",
(r) -> assertThat(((Number)r.get("count")).longValue(), equalTo(6L))

final boolean outgoing = direction == Direction.OUTGOING ? true : false;
testCallEmpty(
db,
"CALL apoc.refactor.categorize('prop','IS_A', $direction, 'Letter','name',['k'],1)",
map("direction", outgoing)
);

String traversePattern = (outgoing ? "" : "<") + "-[:IS_A]-" + (outgoing ? ">" : "");
{
Result result = db.execute("MATCH (n) WITH n ORDER BY n.id MATCH (n)-[:IS_A]->(cat:Letter) RETURN collect(cat.name) AS cats");
Result result = db.execute("MATCH (n) WITH n ORDER BY n.id MATCH (n)" + traversePattern + "(cat:Letter) RETURN collect(cat.name) AS cats");
List<?> cats = (List<?>) result.next().get("cats");
result.close();

assertThat(cats, equalTo(asList("A", "A", "C", "B", "C")));
}

{
Result result = db.execute("MATCH (n) WITH n ORDER BY n.id MATCH (n)-[:IS_A]->(cat:Letter) RETURN collect(cat.k) AS cats");
Result result = db.execute("MATCH (n) WITH n ORDER BY n.id MATCH (n)" + traversePattern + "(cat:Letter) RETURN collect(cat.k) AS cats");
List<?> cats = (List<?>) result.next().get("cats");
result.close();

Expand All @@ -203,38 +206,13 @@ public void testCategorizeOutgoing() throws Exception {
}

@Test
public void testCategorizeIncoming() throws Exception {
db.execute(
"CREATE ({prop: 'A', k: 'a', id: 1}) " +
"CREATE ({prop: 'A', k: 'a', id: 2}) " +
"CREATE ({prop: 'C', k: 'c', id: 3}) " +
"CREATE ({ id: 4}) " +
"CREATE ({prop: 'B', k: 'b', id: 5}) " +
"CREATE ({prop: 'C', k: 'c', id: 6})").close();

testCall(
db,
"CALL apoc.refactor.categorize('prop','IS_A',false,'Letter','name',['k'],1)",
(r) -> assertThat(((Number)r.get("count")).longValue(), equalTo(6L))
);

{
Result result = db.execute("MATCH (n) WITH n ORDER BY n.id MATCH (n)<-[:IS_A]-(cat:Letter) RETURN collect(cat.name) AS cats");
List<?> cats = (List<?>) result.next().get("cats");
result.close();

assertThat(cats, equalTo(asList("A", "A", "C", "B", "C")));
}

{
Result result = db.execute("MATCH (n) WITH n ORDER BY n.id MATCH (n)<-[:IS_A]-(cat:Letter) RETURN collect(cat.k) AS cats");
List<?> cats = (List<?>) result.next().get("cats");
result.close();

assertThat(cats, equalTo(asList("a", "a", "c", "b", "c")));
}
public void testCategorizeOutgoing() throws Exception {
categorizeWithDirection(Direction.OUTGOING);
}

testCall(db, "MATCH (n) WHERE n.prop IS NOT NULL RETURN count(n) AS count", (r) -> assertThat(((Number)r.get("count")).longValue(), equalTo(0L)));
@Test
public void testCategorizeIncoming() throws Exception {
categorizeWithDirection(Direction.INCOMING);
}

@Test
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/apoc/schema/SchemasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static apoc.util.TestUtil.*;
import static java.util.Arrays.asList;
Expand Down Expand Up @@ -251,6 +252,10 @@ public void testKeepSchema() throws Exception {
@Test
public void testIndexes() {
db.execute("CREATE INDEX ON :Foo(bar)").close();
try (Transaction tx = db.beginTx()) {
db.schema().awaitIndexesOnline(5, TimeUnit.SECONDS);
tx.success();
}
testResult(db, "CALL apoc.schema.nodes()", (result) -> {
// Get the index info
Map<String, Object> r = result.next();
Expand Down
16 changes: 6 additions & 10 deletions src/test/java/apoc/trigger/TriggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
import apoc.util.TestUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.*;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.test.TestGraphDatabaseFactory;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.neo4j.helpers.collection.MapUtil.map;

/**
Expand Down Expand Up @@ -41,7 +36,7 @@ public void tearDown() {
@Test
public void testListTriggers() throws Exception {
String query = "MATCH (c:Counter) SET c.count = c.count + size([f IN {deletedNodes} WHERE id(f) > 0])";
db.execute("CALL apoc.trigger.add('count-removals',{query},{})",map("query",query)).close();
assertEquals(1, Iterators.count(db.execute("CALL apoc.trigger.add('count-removals',{query},{}) YIELD name RETURN name", map("query", query))));
TestUtil.testCall(db, "CALL apoc.trigger.list()", (row) -> {
assertEquals("count-removals", row.get("name"));
assertEquals(query, row.get("query"));
Expand All @@ -54,17 +49,18 @@ public void testRemoveNode() throws Exception {
db.execute("CREATE (f:Foo)").close();
db.execute("CALL apoc.trigger.add('count-removals','MATCH (c:Counter) SET c.count = c.count + size([f IN {deletedNodes} WHERE id(f) > 0])',{})").close();
db.execute("MATCH (f:Foo) DELETE f").close();
TestUtil.testCall(db, "MATCH (c:Count) RETURN c.count as count", (row) -> {
TestUtil.testCall(db, "MATCH (c:Counter) RETURN c.count as count", (row) -> {
assertEquals(1L, row.get("count"));
});
}

@Test
public void testRemoveRelationship() throws Exception {
db.execute("CREATE (:Counter {count:0})").close();
db.execute("CREATE (f:Foo)-[:X]->(f)").close();
db.execute("CALL apoc.trigger.add('count-removed-rels','MATCH (c:Counter) SET c.count = c.count + size({deletedRelationships})',{})").close();
db.execute("MATCH (f:Foo) DETACH DELETE f").close();
TestUtil.testCall(db, "MATCH (c:Count) RETURN c.count as count", (row) -> {
TestUtil.testCall(db, "MATCH (c:Counter) RETURN c.count as count", (row) -> {
assertEquals(1L, row.get("count"));
});
}
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/apoc/util/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ public static void testCall(GraphDatabaseService db, String call, Consumer<Map<S
public static void testCall(GraphDatabaseService db, String call,Map<String,Object> params, Consumer<Map<String, Object>> consumer) {
testResult(db, call, params, (res) -> {
try {
if (res.hasNext()) {
Map<String, Object> row = res.next();
consumer.accept(row);
}
assertTrue(res.hasNext());
Map<String, Object> row = res.next();
consumer.accept(row);
assertFalse(res.hasNext());
} catch(Throwable t) {
printFullStackTrace(t);
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/apoc/util/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.concurrent.Future;
import java.util.function.Predicate;

import static apoc.util.TestUtil.testCallEmpty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -65,21 +66,19 @@ public void testValidateTrue() throws Exception {
@Test
public void testSleep() {
String cypherSleep = "call apoc.util.sleep({duration})";
TestUtil.testCall(db, cypherSleep, MapUtil.map("duration", 0l), r -> {
}); // force building query plan
testCallEmpty(db, cypherSleep, MapUtil.map("duration", 0l)); // force building query plan

long duration = 300;
TestUtil.assertDuration(Matchers.greaterThanOrEqualTo(duration), () -> {
TestUtil.testCall(db, cypherSleep, MapUtil.map("duration", duration), r -> { });
testCallEmpty(db, cypherSleep, MapUtil.map("duration", duration));
return null;
});
}

@Test
public void testSleepWithTerminate() {
String cypherSleep = "call apoc.util.sleep({duration})";
TestUtil.testCall(db, cypherSleep, MapUtil.map("duration", 0l), r -> {
}); // force building query plan
testCallEmpty(db, cypherSleep, MapUtil.map("duration", 0l)); // force building query plan

long duration = 300;
TestUtil.assertDuration(Matchers.lessThan(duration), () -> {
Expand Down

0 comments on commit 6ea5396

Please sign in to comment.