private long initWithNode( GraphDatabaseService db ) { try ( Transaction tx = db.beginTx() ) { Node theNode = db.createNode(); long id = theNode.getId(); tx.success(); return id; } }
private long nodeWithNoLabel( GraphDatabaseService graphDb, Object value ) { Node node = graphDb.createNode(); node.setProperty( "prop", value ); return node.getId(); }
@Test public void testFromSimpleGraph() { final Node n0 = gdb.createNode(); final Node n1 = gdb.createNode(); n1.setProperty( "name", "Node1" ); final Relationship relationship = n0.createRelationshipTo( n1, RelationshipType.withName( "REL" ) ); relationship.setProperty( "related", true ); final SubGraph graph = DatabaseSubGraph.from( gdb ); assertEquals( "create (_" + n0.getId() + ")" + lineSeparator() + "create (_" + n1.getId() + " {`name`:\"Node1\"})" + lineSeparator() + "create (_" + n0.getId() + ")-[:`REL` {`related`:true}]->(_" + n1.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); } }
@Test public void shouldReuseExcessBatchIdsWhichWerentUsedBeforeClose() throws Exception { // given Node firstNode; try ( Transaction tx = db.beginTx() ) { firstNode = db.createNode(); tx.success(); } // when db.restartDatabase(); Node secondNode; try ( Transaction tx = db.beginTx() ) { secondNode = db.createNode(); tx.success(); } // then assertEquals( firstNode.getId() + 1, secondNode.getId() ); } }
@Test public void shouldIterateThroughNodesInReverse() { // given Path path = new PathProxy( proxySPI, new long[] {1, 2, 3}, new long[] {100, 200}, new int[] {0, ~0} ); Iterator<Node> iterator = path.reverseNodes().iterator(); Node node; // then assertTrue( iterator.hasNext() ); assertThat( node = iterator.next(), instanceOf( Node.class ) ); assertEquals( 3, node.getId() ); assertTrue( iterator.hasNext() ); assertThat( node = iterator.next(), instanceOf( Node.class ) ); assertEquals( 2, node.getId() ); assertTrue( iterator.hasNext() ); assertThat( node = iterator.next(), instanceOf( Node.class ) ); assertEquals( 1, node.getId() ); assertFalse( iterator.hasNext() ); }
@Test public void mixingBeansApiWithKernelAPI() throws Exception { // 1: Start your transactions through the Beans API Transaction transaction = db.beginTx(); // 2: Get a hold of a KernelAPI transaction this way: KernelTransaction ktx = statementContextSupplier.getKernelTransactionBoundToThisThread( true ); // 3: Now you can interact through both the statement context and the kernel API to manipulate the // same transaction. Node node = db.createNode(); int labelId = ktx.tokenWrite().labelGetOrCreateForName( "labello" ); ktx.dataWrite().nodeAddLabel( node.getId(), labelId ); // 4: Commit through the beans API transaction.success(); transaction.close(); }
@Test public void populatingConstraintMustAcceptDatasetThatGetsUpdatedWithUniqueEntries() throws Exception { // Given givenUniqueDataset(); // When Future<?> createConstraintTransaction = applyChangesToPopulatingUpdater( d.getId(), a.getId(), setProperty( d, "d1" ) ); // Then observe that our constraint was created successfully: createConstraintTransaction.get(); // Future.get() will throw an ExecutionException, if the Runnable threw an exception. }
@Test public void shouldCreateNode() throws Exception { long node; try ( Transaction tx = beginTransaction() ) { node = tx.dataWrite().nodeCreate(); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertEquals( node, graphDb.getNodeById( node ).getId() ); } }
@Test public void shouldIncludeNodesCreatedInSameTxInIndexSeek() { // GIVEN createNodes( db, LABEL, nonMatching[0], nonMatching[1] ); MutableLongSet expected = createNodes( db, LABEL, values ); // WHEN MutableLongSet found = new LongHashSet(); try ( Transaction tx = db.beginTx() ) { expected.add( createNode( db, propertyMap( keys, values ), LABEL ).getId() ); createNode( db, propertyMap( keys, nonMatching[2] ), LABEL ); collectNodes( found, indexSeek.findNodes( keys, values, db ) ); } // THEN assertThat( found, equalTo( expected ) ); }
@Test public void indexHitsFromQueryingRemovedDoesNotReturnNegativeCount() { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); Node theNode = graphDb.createNode(); index.remove( theNode ); try ( IndexHits<Node> hits = index.query( "someRandomKey", theNode.getId() ) ) { assertTrue( hits.size() >= 0 ); } }
private void assertReadNode( String propValue, long expectedNodeId ) { try ( Transaction tx = db.beginTx() ) { Node node = db.findNode( LABEL_ONE, propKey, propValue ); assertNotNull( node ); assertEquals( "node id", expectedNodeId, node.getId() ); tx.success(); } } }
private void verifyIds( EmbeddedProxySPI actions, long relationshipId, long nodeId1, int typeId, long nodeId2 ) { RelationshipProxy proxy = new RelationshipProxy( actions, relationshipId, nodeId1, typeId, nodeId2 ); assertEquals( relationshipId, proxy.getId() ); // our mock above is known to return RelationshipTypeToken assertEquals( nodeId1, proxy.getStartNode().getId() ); assertEquals( nodeId1, proxy.getStartNodeId() ); assertEquals( nodeId2, proxy.getEndNode().getId() ); assertEquals( nodeId2, proxy.getEndNodeId() ); assertEquals( nodeId2, proxy.getOtherNode( nodeWithId( nodeId1 ) ).getId() ); assertEquals( nodeId2, proxy.getOtherNodeId( nodeId1 ) ); assertEquals( nodeId1, proxy.getOtherNode( nodeWithId( nodeId2 ) ).getId() ); assertEquals( nodeId1, proxy.getOtherNodeId( nodeId2 ) ); }
private long createNode( Map<String, Object> properties, Label... labels ) { try ( org.neo4j.graphdb.Transaction tx = db.beginTx() ) { Node node = db.createNode( labels ); for ( Map.Entry<String,Object> property : properties.entrySet() ) { node.setProperty( property.getKey(), property.getValue() ); } tx.success(); return node.getId(); } }
public MutableLongSet createNodes( GraphDatabaseService db, Label label, Object[]... propertyValueTuples ) { MutableLongSet expected = new LongHashSet(); try ( Transaction tx = db.beginTx() ) { for ( Object[] valueTuple : propertyValueTuples ) { expected.add( createNode( db, propertyMap( keys, valueTuple ), label ).getId() ); } tx.success(); } return expected; }
private long createNode() { long node; try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() ) { node = graphDb.createNode().getId(); ctx.success(); } return node; }
@Test public void testFromSimpleCypherResult() { Node n = gdb.createNode(); final ExecutionResult result = result( "node", n ); final SubGraph graph = CypherResultSubGraph.from( result, gdb, false ); assertEquals( "create (_" + n.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) ); }
@Test public void shouldPrintCypherEsqueRelationshipToString() { // GIVEN Node start; Node end; RelationshipType type = RelationshipType.withName( "NICE" ); Relationship relationship; try ( Transaction tx = db.beginTx() ) { // GIVEN start = db.createNode(); end = db.createNode(); relationship = start.createRelationshipTo( end, type ); tx.success(); // WHEN String toString = relationship.toString(); // THEN assertEquals( "(" + start.getId() + ")-[" + type + "," + relationship.getId() + "]->(" + end.getId() + ")", toString ); } }
@Test public void shouldIterateThroughNodes() { // given Path path = new PathProxy( proxySPI, new long[] {1, 2, 3}, new long[] {100, 200}, new int[] {0, ~0} ); Iterator<Node> iterator = path.nodes().iterator(); Node node; // then assertTrue( iterator.hasNext() ); assertThat( node = iterator.next(), instanceOf( Node.class ) ); assertEquals( 1, node.getId() ); assertTrue( iterator.hasNext() ); assertThat( node = iterator.next(), instanceOf( Node.class ) ); assertEquals( 2, node.getId() ); assertTrue( iterator.hasNext() ); assertThat( node = iterator.next(), instanceOf( Node.class ) ); assertEquals( 3, node.getId() ); assertFalse( iterator.hasNext() ); }
@Test public void queryResultsMustIncludeNodesAddedInThisTransaction() { db = createDatabase(); try ( Transaction tx = db.beginTx() ) { createSimpleNodesIndex(); tx.success(); } awaitIndexesOnline(); try ( Transaction tx = db.beginTx() ) { Node node = db.createNode( LABEL ); node.setProperty( PROP, "value" ); assertQueryFindsIds( db, true, "nodes", "value", newSetWith( node.getId() ) ); tx.success(); } }