private int mapToken( ThrowingFunction<Token, Integer, KernelException> f ) { try ( Transaction tx = beginTransaction() ) { return f.apply( tx.token() ); } catch ( KernelException e ) { fail( "Unwanted exception: " + e.getMessage() ); return -1; // unreachable } }
private void forToken( ThrowingConsumer<Token, KernelException> f ) { try ( Transaction tx = beginTransaction() ) { f.accept( tx.token() ); } catch ( KernelException e ) { fail( "Unwanted exception: " + e.getMessage() ); } } }
static void assertCount( org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor relationship, Map<String,Integer> expectedCounts, int expectedType, Direction direction ) throws KernelException { String key = computeKey( transaction.token().relationshipTypeName( expectedType ), direction ); int expectedCount = expectedCounts.getOrDefault( key, 0 ); int count = 0; while ( relationship.next() ) { assertEquals( "same type", expectedType, relationship.type() ); count++; } assertEquals( format( "expected number of relationships for key '%s'", key ), expectedCount, count ); }
@Test public void shouldNotWriteWhenSettingPropertyToSameValue() throws Exception { // Given Value theValue = stringValue( "The Value" ); long nodeId = createNodeWithProperty( propertyKey, theValue.asObject() ); // When Transaction tx = beginTransaction(); int property = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().nodeSetProperty( nodeId, property, theValue ), equalTo( theValue ) ); tx.success(); assertThat( tx.closeTransaction(), equalTo( Transaction.READ_ONLY ) ); }
@Test public void shouldRemoveNonExistingPropertyFromNode() throws Exception { // Given long node = createNode(); // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().nodeRemoveProperty( node, token ), equalTo( NO_VALUE ) ); tx.success(); } // Then assertNoProperty( node, propertyKey ); }
@Test public void shouldRollbackSetNodeProperty() throws Exception { // Given long node = createNode(); // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.failure(); } // Then assertNoProperty( node, propertyKey ); }
@Test public void shouldAddPropertyToNode() throws Exception { // Given long node = createNode(); // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.success(); } // Then assertProperty( node, propertyKey, "hello" ); }
@Test public void shouldNotAddLabelToNonExistingNode() throws Exception { long node = 1337L; try ( Transaction tx = beginTransaction() ) { int labelId = tx.token().labelGetOrCreateForName( labelName ); exception.expect( KernelException.class ); tx.dataWrite().nodeAddLabel( node, labelId ); } }
private void assertIllegalToken( ThrowingConsumer<Token, KernelException> f ) { try ( Transaction tx = beginTransaction() ) { f.accept( tx.token() ); fail( "Expected IllegalTokenNameException" ); } catch ( IllegalTokenNameException e ) { // wanted } catch ( KernelException e ) { fail( "Unwanted exception: " + e.getMessage() ); } }
@Test public void shouldRemovePropertyFromNode() throws Exception { // Given long node = createNodeWithProperty( propertyKey, 42 ); // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().nodeRemoveProperty( node, token ), equalTo( intValue( 42 ) ) ); tx.success(); } // Then assertNoProperty( node, propertyKey ); }
@Test public void shouldUpdatePropertyToNodeInTransaction() throws Exception { // Given long node = createNode(); // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "world" ) ), equalTo( stringValue( "hello" ) ) ); assertThat( tx.dataWrite().nodeSetProperty( node, token, intValue( 1337 ) ), equalTo( stringValue( "world" ) ) ); tx.success(); } // Then assertProperty( node, propertyKey, 1337 ); }
@Test public void shouldUpdatePropertyToNode() throws Exception { // Given long node = createNodeWithProperty( propertyKey, 42 ); // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().nodeSetProperty( node, token, stringValue( "hello" ) ), equalTo( intValue( 42 ) ) ); tx.success(); } // Then assertProperty( node, propertyKey, "hello" ); }
private static String computeKey( org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor r ) throws KernelException { Direction d; if ( r.sourceNodeReference() == r.targetNodeReference() ) { d = Direction.BOTH; } else if ( r.sourceNodeReference() == r.originNodeReference() ) { d = Direction.OUTGOING; } else { d = Direction.INCOMING; } return computeKey( transaction.token().relationshipTypeName( r.type() ), d ); }
@Test public void shouldAddLabelNodeOnce() throws Exception { long node = createNodeWithLabel( labelName ); try ( Transaction tx = beginTransaction() ) { int labelId = tx.token().labelGetOrCreateForName( labelName ); assertFalse( tx.dataWrite().nodeAddLabel( node, labelId ) ); tx.success(); } assertLabels( node, labelName ); }
@Test public void shouldRemoveLabel() throws Exception { long nodeId = createNodeWithLabel( labelName ); try ( Transaction tx = beginTransaction() ) { int labelId = tx.token().labelGetOrCreateForName( labelName ); assertTrue( tx.dataWrite().nodeRemoveLabel( nodeId, labelId ) ); tx.success(); } assertNoLabels( nodeId ); }
@Test public void shouldAddLabelNode() throws Exception { // Given long node = createNode(); // When try ( Transaction tx = beginTransaction() ) { int labelId = tx.token().labelGetOrCreateForName( labelName ); assertTrue( tx.dataWrite().nodeAddLabel( node, labelId ) ); tx.success(); } // Then assertLabels( node, labelName ); }
@Test public void shouldThrowWhenSettingPropertyOnDeletedNode() throws Exception { // Given long node = createNode(); deleteNode( node ); // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); tx.dataWrite().nodeSetProperty( node, token, stringValue( "hello" ) ); fail( "Expected EntityNotFoundException" ); } catch ( EntityNotFoundException e ) { // wanted } }
@Test public void shouldCreateRelationshipBetweenInTransactionNodes() throws Exception { long n1, n2, r; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); int label = tx.token().relationshipTypeGetOrCreateForName( "R" ); r = tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { List<Relationship> relationships = Iterables.asList( graphDb.getNodeById( n1 ).getRelationships() ); assertEquals( 1, relationships.size() ); assertEquals( relationships.get( 0 ).getId(), r ); } }
@Before public void setupGraph() throws IOException, KernelException { if ( testSupport == null ) { folder.create(); testSupport = newTestSupport(); testSupport.setup( folder.getRoot(), this::createTestGraph ); } Kernel kernel = testSupport.kernelToTest(); tx = kernel.beginTransaction( Transaction.Type.implicit, LoginContext.AUTH_DISABLED ); token = tx.token(); read = tx.dataRead(); indexRead = tx.indexRead(); schemaRead = tx.schemaRead(); cursors = new ManagedTestCursors( tx.cursors() ); }
@Test public void shouldRemoveReSetAndTwiceRemovePropertyOnNode() throws Exception { // given long node = createNodeWithProperty( propertyKey, "bar" ); // when try ( Transaction tx = beginTransaction() ) { int prop = tx.token().propertyKeyGetOrCreateForName( propertyKey ); tx.dataWrite().nodeRemoveProperty( node, prop ); tx.dataWrite().nodeSetProperty( node, prop, Values.of( "bar" ) ); tx.dataWrite().nodeRemoveProperty( node, prop ); tx.dataWrite().nodeRemoveProperty( node, prop ); tx.success(); } // then assertNoProperty( node, propertyKey ); }