public static<E extends Element> void assertTraversal(GraphTraversal<?, E> req, E... expectedElements) { for (final E expectedElement : expectedElements) { assertEquals(expectedElement, req.next()); } assertFalse(req.hasNext()); }
private void updateVertexWithProperties(String propertyKey, Object propertyValue, Map<Object, Object> map) { if (graph.traversal().V().has(propertyKey, propertyValue).hasNext()) { final Vertex v = graph.traversal().V().has(propertyKey, propertyValue).next(); map.forEach((key, value) -> v.property((String) key, value)); graph.tx().commit(); } }
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices())); assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices())); assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices())); assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices())); g.V().drop().iterate(); v1 = g.addV().property(property, value1).property(property, value2).next(); g.addV().property(property, value1).property(property, value2).next(); assertEquals(2, g.V().has(property, value1).toList().size()); g.V().properties().drop().iterate();
@Test public void testSimpleTinkerPopTraversal() { Vertex v1 = graph.addVertex("name", "josh"); Vertex v2 = graph.addVertex("name", "lop"); v1.addEdge("created", v2); //graph.tx().commit(); Object id = graph.traversal().V().has("name", "josh").outE("created").as("e").inV().has("name", "lop").<Edge>select("e").next().id(); assertNotNull(id); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldNotBeEqualPropertiesAsThereIsDifferentId() { assertFalse(DetachedFactory.detach(g.E(convertToEdgeId("marko", "created", "lop")).next().property("weight")).equals(DetachedFactory.detach(g.E(convertToEdgeId("josh", "created", "lop")).next().property("weight")))); }
private static void assertNumStep(int expectedResults, int expectedSteps, GraphTraversal traversal, Class<? extends Step>... expectedStepTypes) { int num = 0; while (traversal.hasNext()) { traversal.next(); num++; } // System.out.println(traversal); assertEquals(expectedResults, num); //Verify that steps line up with what is expected after Titan's optimizations are applied List<Step> steps = traversal.asAdmin().getSteps(); Set<Class<? extends Step>> expSteps = Sets.newHashSet(expectedStepTypes); int numSteps = 0; for (Step s : steps) { // System.out.println(s.getClass()); if (s.getClass().equals(GraphStep.class) || s.getClass().equals(StartStep.class)) continue; assertTrue(s.getClass().getName(), expSteps.contains(s.getClass())); numSteps++; } assertEquals(expectedSteps, numSteps); }
@Test public void testReadGraphOfTheGods() { GraphOfTheGodsFactory.load(graph, null, true); assertEquals(12L, (long) graph.traversal().V().count().next()); Graph g = GraphFactory.open("target/test-classes/cassandra-read.properties"); GraphTraversalSource t = g.traversal(GraphTraversalSource.computer(SparkGraphComputer.class)); assertEquals(12L, (long) t.V().count().next()); }
@Test(expected = NoSuchElementException.class) @LoadGraphWith(MODERN) public void shouldGetExcludedEdge() throws Exception { final Traversal<Edge, ?> edgeCriterion = __.or( has("weight", 1.0d).hasLabel("knows"), // 8 has("weight", 0.4d).hasLabel("created").outV().has("name", "marko"), // 9 has("weight", 1.0d).hasLabel("created") // 10 ); final GraphTraversalSource sg = g.withStrategies(SubgraphStrategy.build().edges(edgeCriterion).create()); sg.E(sg.E(convertToEdgeId("marko", "knows", "vadas")).next()).next(); }
private static void assertNumStep(int expectedResults, int expectedSteps, GraphTraversal traversal, Class<? extends Step>... expectedStepTypes) { int num = 0; while (traversal.hasNext()) { traversal.next(); num++; } assertEquals(expectedResults, num); //Verify that steps line up with what is expected after JanusGraph's optimizations are applied List<Step> steps = traversal.asAdmin().getSteps(); Set<Class<? extends Step>> expSteps = Sets.newHashSet(expectedStepTypes); int numSteps = 0; for (Step s : steps) { if (s.getClass().equals(GraphStep.class) || s.getClass().equals(StartStep.class)) continue; if (expSteps.contains(s.getClass())) { numSteps++; } } assertEquals(expectedSteps, numSteps); }
private void testOr(final Graph aGraph) { final GraphTraversalSource g = aGraph.traversal(); final Vertex hiro = g.addV().property("name", "Hiro").property("age", 2).property("length", 90).next(); final Vertex totoro = g.addV().property("name", "Totoro").property("age", 1).next(); final Vertex john = g.addV().property("name", "John").property("age", 3).property("length", 110).next(); final Vertex mike = g.addV().property("name", "Mike").property("age", 4).property("length", 130).next(); aGraph.tx().commit(); assertCount(1, g.V().has("name", "Totoro")); assertCount(1, g.V().has("age", 2)); assertCount(1, g.V().or(__.has("name", "Hiro"),__.has("age", 2))); assertCount(2, g.V().or(__.has("name", "Totoro"),__.has("age", 2))); assertCount(2, g.V().or(__.has("name", "Totoro").has("age", 1),__.has("age", 2)));
@Test public void testReadWideVertexWithManyProperties() { int numProps = 1 << 16; long numV = 1; mgmt.makePropertyKey("p").cardinality(Cardinality.LIST).dataType(Integer.class).make(); mgmt.commit(); finishSchema(); for (int j = 0; j < numV; j++) { Vertex v = graph.addVertex(); for (int i = 0; i < numProps; i++) { v.property("p", i); } } graph.tx().commit(); assertEquals(numV, (long) graph.traversal().V().count().next()); Map<String, Object> propertiesOnVertex = graph.traversal().V().valueMap().next(); List<?> valuesOnP = (List)propertiesOnVertex.values().iterator().next(); assertEquals(numProps, valuesOnP.size()); Graph g = GraphFactory.open("target/test-classes/cassandra-read.properties"); GraphTraversalSource t = g.traversal(GraphTraversalSource.computer(SparkGraphComputer.class)); assertEquals(numV, (long) t.V().count().next()); propertiesOnVertex = t.V().valueMap().next(); valuesOnP = (List)propertiesOnVertex.values().iterator().next(); assertEquals(numProps, valuesOnP.size()); }
@Test public void testSimpleTinkerPopTraversal() { Vertex v1 = graph.addVertex("name", "josh"); Vertex v2 = graph.addVertex("name", "lop"); v1.addEdge("created", v2); //graph.tx().commit(); Object id = graph.traversal().V().has("name", "josh").outE("created").as("e").inV().has("name", "lop").<Edge>select("e").next().id(); assertNotNull(id); }
@Test @LoadGraphWith(MODERN) public void shouldNotThrowFastNoSuchElementException() { //FastNoSuchElement exceptions don't have a stack trace. //They should be converted to regular exceptions before returning to the the user. try { g.V().has("foo").next(); } catch (NoSuchElementException e) { assertEquals(NoSuchElementException.class, e.getClass()); } }
@Test public void testTinkerPopCardinality() { PropertyKey id = mgmt.makePropertyKey("id").cardinality(Cardinality.SINGLE).dataType(Integer.class).make(); PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.SINGLE).dataType(String.class).make(); PropertyKey names = mgmt.makePropertyKey("names").cardinality(Cardinality.LIST).dataType(String.class).make(); mgmt.buildIndex("byId", Vertex.class).addKey(id).buildCompositeIndex(); finishSchema(); GraphTraversalSource gts; Vertex v; v = graph.addVertex("id", 1); v.property(single, "name", "t1"); graph.addVertex("id", 2, "names", "n1", "names", "n2"); graph.tx().commit(); gts = graph.traversal(); v = gts.V().has("id", 1).next(); v.property(single, "name", "t2"); v = gts.V().has("id", 1).next(); v.property(single, "name", "t3"); assertCount(1, gts.V(v).properties("name")); assertCount(2, gts.V().has("id", 2).properties("names")); assertCount(2, gts.V().hasLabel("vertex")); }