private boolean nodeHasLabels( Node node, String[] labels ) { for ( String name : labels ) { if ( !node.hasLabel( Label.label( name ) ) ) { return false; } } return true; }
@Override protected boolean matchesSafely( Node item, Description mismatchDescription ) { boolean result = item.hasLabel( myLabel ); if ( !result ) { Set<String> labels = asLabelNameSet( item.getLabels() ); mismatchDescription.appendText( labels.toString() ); } return result; } };
@Test @Graph( { "a:Person EATS b:Banana", "a EATS b:Apple" } ) public void ensurePeopleCanEatBananasAndApples() { Map<String,Node> graph = data.get(); Node a = graph.get( "a" ); Node b = graph.get( "b" ); try ( Transaction ignored = graphdb.beginTx() ) { assertTrue( "Person label missing", a.hasLabel( label( "Person" ) ) ); assertTrue( "Banana label missing", b.hasLabel( label( "Banana" ) ) ); assertTrue( "Apple label missing", b.hasLabel( label( "Apple" ) ) ); } }
@Test @Graph( { "a:Person EATS b:Banana" } ) public void ensurePeopleCanEatBananas() { Map<String,Node> graph = data.get(); Node a = graph.get( "a" ); Node b = graph.get( "b" ); try ( Transaction ignored = graphdb.beginTx() ) { assertTrue( a.hasLabel( label( "Person" ) ) ); assertTrue( b.hasLabel( label( "Banana" ) ) ); } }
@Override void perform( Graph graph, ExpectedTransactionData expectations ) { Node node = graph.randomNode(); if ( node != null ) { Label label = graph.randomLabel(); if ( node.hasLabel( label ) ) { node.removeLabel( label ); expectations.removedLabel( node, label ); debug( node + " " + label ); } } } },
@Test public void shouldHandleAdditiveLabelsWithSpaces() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; final Label label1 = label( "My First Label" ); final Label label2 = label( "My Other Label" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes:My First Label:My Other Label", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath(), "--relationships", relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() ); // THEN verifyData( node -> { assertTrue( node.hasLabel( label1 ) ); assertTrue( node.hasLabel( label2 ) ); }, Validators.emptyValidator() ); }
@Override void perform( Graph graph, ExpectedTransactionData expectations ) { Node node = graph.randomNode(); if ( node != null ) { Label label = graph.randomLabel(); if ( !node.hasLabel( label ) ) { node.addLabel( label ); expectations.assignedLabel( node, label ); debug( node + " " + label ); } } } },
private void assertNode( ResourceIterator<Node> reader, int id ) { assertTrue( reader.hasNext() ); Node node = reader.next(); assertTrue( node.hasLabel( LABEL ) ); assertEquals( "Expected node " + node + " (returned by index reader) to have 'id' property w/ value " + id, id, node.getProperty( KEY ) ); }
@Test public void removingUncommittedLabel() { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Label label = Labels.MY_LABEL; // When Node myNode; try ( Transaction tx = beansAPI.beginTx() ) { myNode = beansAPI.createNode(); myNode.addLabel( label ); myNode.removeLabel( label ); // THEN assertFalse( myNode.hasLabel( label ) ); tx.success(); } }
if ( !node.hasLabel( label ) )
for ( Node node : filter( n -> n.hasLabel( label ) && n.hasProperty( NAME_PROPERTY ), db.getAllNodes() ) )
private Node copyLabels(Node source, Node target) { for (Label label : source.getLabels()) { if (!target.hasLabel(label)) { target.addLabel(label); } } return target; }
@Test public void shouldWorkWithPoint2DArrays() throws Exception { HTTP.Response response = runQuery( "create (:Node {points: [point({x:1, y:1}), point({x:2, y:2}), point({x: 3.0, y: 3.0})]})" ); assertEquals( 200, response.status() ); assertNoErrors( response ); GraphDatabaseFacade db = server().getDatabase().getGraph(); try ( Transaction tx = db.beginTx() ) { for ( Node node : db.getAllNodes() ) { if ( node.hasLabel( label( "Node" ) ) && node.hasProperty( "points" ) ) { Point[] points = (Point[]) node.getProperty( "points" ); verifyPoint( points[0], Cartesian, 1.0, 1.0 ); verifyPoint( points[1], Cartesian, 2.0, 2.0 ); verifyPoint( points[2], Cartesian, 3.0, 3.0 ); } } tx.success(); } }
for ( Label label : labels ) assertTrue( node.hasLabel( label ) );
@UserFunction("apoc.label.exists") @Description("apoc.label.exists(element, label) - returns true or false related to label existance") public boolean exists(@Name("node") Object element, @Name("label") String label) { return element instanceof Node ? ((Node) element).hasLabel(org.neo4j.graphdb.Label.label(label)) : element instanceof Relationship ? ((Relationship) element).isType(RelationshipType.withName(label)) : false; } }
@Test public void testCreateNodes() throws Exception { testResult(db, "CALL apoc.create.nodes(['Person'],[{name:'John'},{name:'Jane'}])", (res) -> { Node node = (Node) res.next().get("node"); assertEquals(true, node.hasLabel(PERSON)); assertEquals("John", node.getProperty("name")); node = (Node) res.next().get("node"); assertEquals(true, node.hasLabel(PERSON)); assertEquals("Jane", node.getProperty("name")); }); } @Test public void testCreateRelationship() throws Exception {
@Test public void testVirtualNodeFunction() throws Exception { testCall(db, "RETURN apoc.create.vNode(['Person'],{name:'John'}) as node", (row) -> { Node node = (Node) row.get("node"); assertEquals(true, node.hasLabel(Label.label("Person"))); assertEquals("John", node.getProperty("name")); }); } @Test public void testCreateNodes() throws Exception {
private void assertPath(Map<String, Object> row, int length, String startLabel, String endLabel) { Path p = (Path) row.get("p"); assertEquals(length,p.length()); assertEquals(true,p.startNode().hasLabel(Label.label(startLabel))); assertEquals(true,p.endNode().hasLabel(Label.label(endLabel))); assertEquals(length > 0,p.relationships().iterator().hasNext()); for (Relationship rel : p.relationships()) { assertEquals("NEXT",rel.getType().name()); } } }
@Test public void testDeleteOneNode() throws Exception { long id = db.execute("CREATE (p1:Person {ID:1}), (p2:Person {ID:2}) RETURN id(p1) as id ").<Long>columnAs("id").next(); 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"); assertNotEquals(id, node.getId()); assertEquals(true, node.hasLabel(Label.label("Person"))); assertEquals(2L, node.getProperty("ID")); }); }
@Test public void testGroupAllLabels() throws Exception { db.execute("CREATE (u:User {name:'Joe',gender:'male'})").close(); ResourceIterator<Node> it = db.execute("CALL apoc.nodes.group(['*'],['gender'])").columnAs("node"); assertEquals(true,(it.next()).hasLabel(Label.label("User"))); assertFalse(it.hasNext()); } @Test