private void createNode( String label ) { try ( Transaction tx = db.beginTx() ) { db.createNode( label( label ) ); tx.success(); } }
private long nodeWithWhatever( GraphDatabaseService graphDb, Object value ) { Node node = graphDb.createNode( label( "What" ) ); node.setProperty( "ever", value ); return node.getId(); }
@Test public void shouldIncludeLabel() { // GIVEN String labelName = "person"; String propertyKey = "name"; IndexDefinition definition = mock( IndexDefinition.class ); when( definition.isNodeIndex() ).thenReturn( true ); when( definition.getLabels() ).thenReturn( singletonList( label( labelName ) ) ); when( definition.getPropertyKeys() ).thenReturn( singletonList( propertyKey ) ); IndexDefinitionRepresentation representation = new IndexDefinitionRepresentation( definition ); Map<String, Object> serialized = RepresentationTestAccess.serialize( representation ); // THEN assertEquals( singletonList( propertyKey ), serialized.get( "property_keys" ) ); assertEquals( labelName, serialized.get( "label" ) ); } }
@Test public void testSingleNodeLabels() { Node n = gdb.createNode(); n.addLabel( Label.label( "Foo" ) ); n.addLabel( Label.label( "Bar" ) ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + ":`Foo`:`Bar`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void shouldNotWarnOnSmallLabelScansWithLoadCVS() { try ( Transaction tx = db().beginTx() ) { db().createNode().addLabel( label( "A" ) ); tx.success(); } Stream.of( "CYPHER 3.1", "CYPHER 3.5" ).forEach( version -> { shouldNotNotifyInStream( version, "EXPLAIN LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MATCH (a:A) RETURN *" ); shouldNotNotifyInStream( version, "EXPLAIN LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MERGE (a:A) RETURN *" ); } ); }
@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" ) ) ); } }
@Test public void shouldAllowConcurrentCreationOfNonConflictingData() throws Exception { // given database.executeAndCommit( createUniquenessConstraint( "Label1", "key1" ) ); // when Future<Boolean> created = database.executeAndCommit( db -> { db.createNode( label( "Label1" ) ).setProperty( "key1", "value1" ); return otherThread.execute( createNode( db, "Label1", "key1", "value2" ) ); } ); // then assertTrue( "Node creation should succeed", created.get() ); }
private void addConstraints( String... labelProps ) { assert labelProps.length % 2 == 0; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { for ( int i = 0; i < labelProps.length; i += 2 ) { graphDb.schema().constraintFor( label( labelProps[i] ) ).assertPropertyIsUnique( labelProps[i + 1] ) .create(); } tx.success(); } } }
private static void generateTransaction( GraphDatabaseAPI database ) { try ( Transaction transaction = database.beginTx() ) { Node startNode = database.createNode( Label.label( "startNode" ) ); startNode.setProperty( "key", "value" ); Node endNode = database.createNode( Label.label( "endNode" ) ); endNode.setProperty( "key", "value" ); startNode.createRelationshipTo( endNode, RelationshipType.withName( "connects" ) ); transaction.success(); } }
private long createNodeWithLabel( String labelName ) { long node; try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() ) { node = graphDb.createNode( label( labelName ) ).getId(); ctx.success(); } return node; }
@Test public void lookupWithinTransactionWithCacheEviction() { try ( Transaction tx = db.beginTx() ) { // when db.createNode( label( "Node" ) ).setProperty( "prop", store ); // then assertEquals( 1, count( db.findNodes( label( "Node" ), "prop", lookup ) ) ); // no need to actually commit this node } }
@Test public void testExportIndexesViaCypherResult() { final Label label = Label.label( "Foo" ); gdb.schema().indexFor( label ).on( "bar" ).create(); gdb.schema().indexFor( label ).on( "bar2" ).create(); commitAndStartNewTransactionAfterSchemaChanges(); Node n = gdb.createNode( label ); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, true ); assertEquals( "create index on :`Foo`(`bar2`);" + lineSeparator() + "create index on :`Foo`(`bar`);" + lineSeparator() + "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void shouldWarnOnLargeLabelScansWithLoadCVSMatch() { for ( int i = 0; i < 11; i++ ) { try ( Transaction tx = db().beginTx() ) { db().createNode().addLabel( label( "A" ) ); tx.success(); } } Stream.of( "CYPHER 3.1", "CYPHER 3.5" ).forEach( version -> assertNotifications( version + "EXPLAIN LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MATCH (a:A) RETURN *", containsNoItem( largeLabelCSVWarning ) ) ); }
@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" ) ) ); } }
private void createIndex() { try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { graphDb.schema().indexFor( Label.label( "Node" ) ).on( "prop" ).create(); tx.success(); } try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { graphDb.schema().awaitIndexesOnline( 1, TimeUnit.MINUTES ); } }
private long nodeWithProp( GraphDatabaseService graphDb, Object value ) { Node node = graphDb.createNode( label( "Node" ) ); node.setProperty( "prop", value ); return node.getId(); }