From fc6a9a3cae93470332b9676296a88d94c94a415d Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Mon, 2 Nov 2020 12:50:49 +0200 Subject: [PATCH 1/6] increase code coverage --- .../exceptions/JRedisGraphErrorTest.java | 246 +++++++++++------- .../redisgraph/graph_entities/PathTest.java | 6 +- 2 files changed, 155 insertions(+), 97 deletions(-) diff --git a/src/test/java/com/redislabs/redisgraph/exceptions/JRedisGraphErrorTest.java b/src/test/java/com/redislabs/redisgraph/exceptions/JRedisGraphErrorTest.java index 443588c..7f57d41 100644 --- a/src/test/java/com/redislabs/redisgraph/exceptions/JRedisGraphErrorTest.java +++ b/src/test/java/com/redislabs/redisgraph/exceptions/JRedisGraphErrorTest.java @@ -6,136 +6,190 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.HashMap; public class JRedisGraphErrorTest { - private RedisGraphContextGenerator api; - - @Before - public void createApi(){ - api = new RedisGraph(); - Assert.assertNotNull(api.query("social", "CREATE (:person{mixed_prop: 'strval'}), (:person{mixed_prop: 50})")); + private RedisGraphContextGenerator api; + + @Before + public void createApi(){ + api = new RedisGraph(); + Assert.assertNotNull(api.query("social", "CREATE (:person{mixed_prop: 'strval'}), (:person{mixed_prop: 50})")); + } + @After + public void deleteGraph() { + + api.deleteGraph("social"); + api.close(); + } + + @Test + public void testExceptions() { + try { + throw new JRedisGraphCompileTimeException("Test message 1"); + }catch (JRedisGraphCompileTimeException e) { + Assert.assertEquals( "Test message 1", e.getMessage()); } - @After - public void deleteGraph() { - api.deleteGraph("social"); - api.close(); + Exception cause = new IndexOutOfBoundsException("Index Error"); + try { + throw new JRedisGraphCompileTimeException("Test message 2", cause); + }catch (JRedisGraphCompileTimeException e) { + Assert.assertEquals( "Test message 2", e.getMessage()); + Assert.assertEquals( cause, e.getCause()); } - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); - - @Test - public void testSyntaxErrorReporting() { - exceptionRule.expect(JRedisGraphCompileTimeException.class); - exceptionRule.expectMessage("Type mismatch: expected String but was Integer"); + try { + throw new JRedisGraphCompileTimeException(cause); + }catch (JRedisGraphCompileTimeException e) { + Assert.assertEquals( "java.lang.IndexOutOfBoundsException: Index Error", e.getMessage()); + Assert.assertEquals( cause, e.getCause()); + } + + try { + throw new JRedisGraphRunTimeException("Test message 3"); + }catch (JRedisGraphRunTimeException e) { + Assert.assertEquals( "Test message 3", e.getMessage()); + } - // Issue a query that causes a compile-time error - api.query("social", "RETURN toUpper(5)"); + try { + throw new JRedisGraphRunTimeException("Test message 4", cause); + }catch (JRedisGraphRunTimeException e) { + Assert.assertEquals( "Test message 4", e.getMessage()); + Assert.assertEquals( cause, e.getCause()); + } + try { + throw new JRedisGraphRunTimeException(cause); + }catch (JRedisGraphRunTimeException e) { + Assert.assertEquals( "java.lang.IndexOutOfBoundsException: Index Error", e.getMessage()); + Assert.assertEquals( cause, e.getCause()); + } + } + + @Test + public void testSyntaxErrorReporting() { + try { + // Issue a query that causes a compile-time error + api.query("social", "RETURN toUpper(5)"); + } catch (Exception e) { + Assert.assertEquals(JRedisGraphCompileTimeException.class, e.getClass()); + Assert.assertEquals( "redis.clients.jedis.exceptions.JedisDataException: Type mismatch: expected String but was Integer", e.getMessage()); } - @Test - public void testRuntimeErrorReporting() { - exceptionRule.expect(JRedisGraphRunTimeException.class); - exceptionRule.expectMessage("Type mismatch: expected String but was Integer"); + } - // Issue a query that causes a run-time error - api.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); + @Test + public void testRuntimeErrorReporting() { + try { + // Issue a query that causes a run-time error + api.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); + } catch (Exception e) { + Assert.assertEquals(JRedisGraphRunTimeException.class, e.getClass()); + Assert.assertEquals( "redis.clients.jedis.exceptions.JedisDataException: Type mismatch: expected String but was Integer", e.getMessage()); } + } - @Test - public void testExceptionFlow() { - try { - // Issue a query that causes a compile-time error - api.query("social", "RETURN toUpper(5)"); - } - catch (Exception e) { - Assert.assertEquals(JRedisGraphCompileTimeException.class, e.getClass()); - Assert.assertTrue( e.getMessage().contains("Type mismatch: expected String but was Integer")); - } + @Test + public void testExceptionFlow() { - // On general api usage, user should get a new connection + try { + // Issue a query that causes a compile-time error + api.query("social", "RETURN toUpper(5)"); + } + catch (Exception e) { + Assert.assertEquals(JRedisGraphCompileTimeException.class, e.getClass()); + Assert.assertTrue( e.getMessage().contains("Type mismatch: expected String but was Integer")); + } - try { - // Issue a query that causes a compile-time error - api.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); - } - catch (Exception e) { - Assert.assertEquals(JRedisGraphRunTimeException.class, e.getClass()); - Assert.assertTrue( e.getMessage().contains("Type mismatch: expected String but was Integer")); - } + // On general api usage, user should get a new connection + try { + // Issue a query that causes a compile-time error + api.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); + } + catch (Exception e) { + Assert.assertEquals(JRedisGraphRunTimeException.class, e.getClass()); + Assert.assertTrue( e.getMessage().contains("Type mismatch: expected String but was Integer")); } + } - @Test - public void testContextSyntaxErrorReporting() { - exceptionRule.expect(JRedisGraphCompileTimeException.class); - exceptionRule.expectMessage("Type mismatch: expected String but was Integer"); - RedisGraphContext c = api.getContext(); - - // Issue a query that causes a compile-time error - c.query("social", "RETURN toUpper(5)"); + @Test + public void testContextSyntaxErrorReporting() { + RedisGraphContext c = api.getContext(); + try { + // Issue a query that causes a compile-time error + c.query("social", "RETURN toUpper(5)"); + } catch (Exception e) { + Assert.assertEquals(JRedisGraphCompileTimeException.class, e.getClass()); + Assert.assertEquals( "redis.clients.jedis.exceptions.JedisDataException: Type mismatch: expected String but was Integer", e.getMessage()); } - - @Test - public void testMissingParametersSyntaxErrorReporting(){ - exceptionRule.expect(JRedisGraphRunTimeException.class); - exceptionRule.expectMessage("Missing parameters"); - api.query("social","RETURN $param"); + } + + @Test + public void testMissingParametersSyntaxErrorReporting(){ + try { + api.query("social","RETURN $param"); + } catch (Exception e) { + Assert.assertEquals(JRedisGraphRunTimeException.class, e.getClass()); + Assert.assertEquals( "redis.clients.jedis.exceptions.JedisDataException: Missing parameters", e.getMessage()); } - - @Test - public void testMissingParametersSyntaxErrorReporting2(){ - exceptionRule.expect(JRedisGraphRunTimeException.class); - exceptionRule.expectMessage("Missing parameters"); - api.query("social","RETURN $param", new HashMap<>()); + } + + @Test + public void testMissingParametersSyntaxErrorReporting2(){ + try { + api.query("social","RETURN $param", new HashMap<>()); + } catch (Exception e) { + Assert.assertEquals(JRedisGraphRunTimeException.class, e.getClass()); + Assert.assertEquals( "redis.clients.jedis.exceptions.JedisDataException: Missing parameters", e.getMessage()); } - - @Test - public void testContextRuntimeErrorReporting() { - exceptionRule.expect(JRedisGraphRunTimeException.class); - exceptionRule.expectMessage("Type mismatch: expected String but was Integer"); - - RedisGraphContext c = api.getContext(); - // Issue a query that causes a run-time error - c.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); + } + + @Test + public void testContextRuntimeErrorReporting() { + + RedisGraphContext c = api.getContext(); + try { + // Issue a query that causes a run-time error + c.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); + } catch (Exception e) { + Assert.assertEquals(JRedisGraphRunTimeException.class, e.getClass()); + Assert.assertEquals( "redis.clients.jedis.exceptions.JedisDataException: Type mismatch: expected String but was Integer", e.getMessage()); } + } - @Test - public void testContextExceptionFlow() { - - RedisGraphContext c = api.getContext(); - try { - // Issue a query that causes a compile-time error - c.query("social", "RETURN toUpper(5)"); - } - catch (Exception e) { - Assert.assertEquals(JRedisGraphCompileTimeException.class, e.getClass()); - Assert.assertTrue( e.getMessage().contains("Type mismatch: expected String but was Integer")); - } + @Test + public void testContextExceptionFlow() { - // On contexted api usage, connection should stay open + RedisGraphContext c = api.getContext(); + try { + // Issue a query that causes a compile-time error + c.query("social", "RETURN toUpper(5)"); + } + catch (Exception e) { + Assert.assertEquals(JRedisGraphCompileTimeException.class, e.getClass()); + Assert.assertTrue( e.getMessage().contains("Type mismatch: expected String but was Integer")); + } - try { - // Issue a query that causes a compile-time error - c.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); - } - catch (Exception e) { - Assert.assertEquals(JRedisGraphRunTimeException.class, e.getClass()); - Assert.assertTrue( e.getMessage().contains("Type mismatch: expected String but was Integer")); - } + // On contexted api usage, connection should stay open + try { + // Issue a query that causes a compile-time error + c.query("social", "MATCH (p:person) RETURN toUpper(p.mixed_prop)"); } + catch (Exception e) { + Assert.assertEquals(JRedisGraphRunTimeException.class, e.getClass()); + Assert.assertTrue( e.getMessage().contains("Type mismatch: expected String but was Integer")); + } + + } } diff --git a/src/test/java/com/redislabs/redisgraph/graph_entities/PathTest.java b/src/test/java/com/redislabs/redisgraph/graph_entities/PathTest.java index ee65e68..6056ccf 100644 --- a/src/test/java/com/redislabs/redisgraph/graph_entities/PathTest.java +++ b/src/test/java/com/redislabs/redisgraph/graph_entities/PathTest.java @@ -45,6 +45,8 @@ public void testEmptyPath(){ assertEquals(0, path.nodeCount()); assertThrows(IndexOutOfBoundsException.class, ()->path.getNode(0)); assertThrows(IndexOutOfBoundsException.class, ()->path.getEdge(0)); + + assertEquals("Path{nodes=[], edges=[]}", path.toString()); } @Test @@ -57,6 +59,8 @@ public void testSingleNodePath(){ assertEquals(n, path.firstNode()); assertEquals(n, path.lastNode()); assertEquals(n, path.getNode(0)); + + assertEquals("Path{nodes=[Node{labels=[], id=0, propertyMap={}}], edges=[]}", path.toString()); } @Test @@ -65,7 +69,7 @@ public void testRandomLengthPath(){ Path path = buildPath(nodeCount); assertEquals(buildNodeArray(nodeCount), path.getNodes()); assertEquals(buildEdgeArray(nodeCount-1), path.getEdges()); - assertDoesNotThrow(()->path.getEdge(0)); + assertDoesNotThrow(()->path.getEdge(0)); } @Test From 3f69a0e614ea8734ad331ba446e6b7d8be0fc88a Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Mon, 2 Nov 2020 14:00:16 +0200 Subject: [PATCH 2/6] add test to record.containsKey --- src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java b/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java index f1f3a60..cab0417 100644 --- a/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java +++ b/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java @@ -313,7 +313,9 @@ public void testRecord(){ Assert.assertEquals( 32L, ((Long)record.getValue("a.age")).longValue()); Assert.assertEquals( "roi", record.getString("a.name")); Assert.assertEquals( "32", record.getString("a.age")); - + + Assert.assertTrue( record.containsKey("a.name")); + Assert.assertFalse( record.containsKey("a.addr")); } From dc1150350241732ad59fa5c323c17c91dfb73c5f Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Mon, 2 Nov 2020 16:27:22 +0200 Subject: [PATCH 3/6] Extend the Edge tests coverage --- src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java b/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java index cab0417..1c10f9b 100644 --- a/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java +++ b/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java @@ -290,6 +290,9 @@ public void testRecord(){ Edge edge = record.getValue(1); Assert.assertNotNull(edge); Assert.assertEquals(expectedEdge, edge); + Assert.assertEquals(1, edge.getDestination()); + Assert.assertEquals(0, edge.getSource()); + Assert.assertEquals("knows", edge.getRelationshipType()); edge = record.getValue("r"); Assert.assertEquals(expectedEdge, edge); From 4243cf60c62d84f2833a93f1a27469364b2acc1d Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Mon, 2 Nov 2020 16:49:16 +0200 Subject: [PATCH 4/6] add Edge.toString() test --- .../redislabs/redisgraph/RedisGraphAPITest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java b/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java index 1c10f9b..dce12f6 100644 --- a/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java +++ b/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java @@ -319,6 +319,21 @@ public void testRecord(){ Assert.assertTrue( record.containsKey("a.name")); Assert.assertFalse( record.containsKey("a.addr")); + Assert.assertEquals("Record{" + + "values=[Node{labels=[person], id=0, propertyMap={" + + "name=Property{name='name', value=roi}, " + + "boolValue=Property{name='boolValue', value=true}, " + + "doubleValue=Property{name='doubleValue', value=3.14}, " + + "nullValue=Property{name='nullValue', value=null}, " + + "age=Property{name='age', value=32}}}, " + + "Edge{relationshipType='knows', source=0, destination=1, id=0, " + + "propertyMap={boolValue=Property{name='boolValue', value=false}, " + + "place=Property{name='place', value=TLV}, " + + "doubleValue=Property{name='doubleValue', value=3.14}, " + + "nullValue=Property{name='nullValue', value=null}, " + + "since=Property{name='since', value=2000}}}, " + + "roi, 32, 3.14, true, null, TLV, 2000, 3.14, false, null]" + + "}", record.toString()); } From 71da3f047d94e9ddf6cad049a241e94053fa2405 Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Mon, 2 Nov 2020 17:17:28 +0200 Subject: [PATCH 5/6] Cover StatisticsImpl toString() --- .../java/com/redislabs/redisgraph/RedisGraphAPITest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java b/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java index dce12f6..1ba2f2b 100644 --- a/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java +++ b/src/test/java/com/redislabs/redisgraph/RedisGraphAPITest.java @@ -2,6 +2,7 @@ import java.util.*; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -272,8 +273,8 @@ public void testRecord(){ Assert.assertEquals(0, resultSet.getStatistics().relationshipsCreated()); Assert.assertEquals(0, resultSet.getStatistics().relationshipsDeleted()); Assert.assertNotNull(resultSet.getStatistics().getStringValue(Label.QUERY_INTERNAL_EXECUTION_TIME)); - - + Assert.assertTrue(Pattern.matches("StatisticsImpl\\{statistics=\\{Cached execution=0, Query internal execution time=[0-9]*\\.[0-9]* milliseconds\\}\\}", resultSet.getStatistics().toString())); + Assert.assertEquals(1, resultSet.size()); Assert.assertTrue(resultSet.hasNext()); Record record = resultSet.next(); From 983b9f30822b327c51748236a1aae4b4c6217df2 Mon Sep 17 00:00:00 2001 From: Guy Korland Date: Wed, 4 Nov 2020 10:42:45 +0200 Subject: [PATCH 6/6] Refactor code --- .../redislabs/redisgraph/impl/api/RedisGraphTransaction.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/redislabs/redisgraph/impl/api/RedisGraphTransaction.java b/src/main/java/com/redislabs/redisgraph/impl/api/RedisGraphTransaction.java index 11fca09..4d0b4f1 100644 --- a/src/main/java/com/redislabs/redisgraph/impl/api/RedisGraphTransaction.java +++ b/src/main/java/com/redislabs/redisgraph/impl/api/RedisGraphTransaction.java @@ -96,7 +96,7 @@ public ResultSet build(Object o) { * @return response with result set with the procedure data */ public Response callProcedure(String graphId, String procedure){ - return callProcedure(graphId, procedure, Utils.DUMMY_LIST, Utils.DUMMY_MAP); + return callProcedure(graphId, procedure, Utils.DUMMY_LIST); } /** @@ -106,7 +106,7 @@ public Response callProcedure(String graphId, String procedure){ * @param args procedure arguments * @return response with result set with the procedure data */ - public Response callProcedure(String graphId, String procedure, List args ){ + public Response callProcedure(String graphId, String procedure, List args){ return callProcedure(graphId, procedure, args, Utils.DUMMY_MAP); }