Skip to content

Commit

Permalink
Support nodeRemoveProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Dec 4, 2017
1 parent 5ed8545 commit e9241cf
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 39 deletions.
Expand Up @@ -86,7 +86,7 @@ public interface Write
* @param propertyKey the property key id
* @return The removed value, or Values.NO_VALUE if the node did not have the property before
*/
Value nodeRemoveProperty( long node, int propertyKey );
Value nodeRemoveProperty( long node, int propertyKey ) throws KernelException;

/**
* Set a property on a relationship
Expand Down
Expand Up @@ -39,6 +39,7 @@
import static org.neo4j.values.storable.Values.intValue;
import static org.neo4j.values.storable.Values.stringValue;

@SuppressWarnings("Duplicates")
public abstract class NodeWriteTestBase<G extends KernelAPIWriteTestSupport> extends KernelAPIWriteTestBase<G>
{
@Rule
Expand All @@ -54,7 +55,7 @@ public void shouldCreateNode() throws Exception
tx.success();
}

try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertEquals( node, graphDb.getNodeById( node ).getId() );
}
Expand All @@ -70,7 +71,7 @@ public void shouldRollbackOnFailure() throws Exception
tx.failure();
}

try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
graphDb.getNodeById( node );
fail( "There should be no node" );
Expand All @@ -96,7 +97,7 @@ public void shouldRemoveNode() throws Exception
tx.dataWrite().nodeDelete( node );
tx.success();
}
try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
try
{
Expand Down Expand Up @@ -150,7 +151,7 @@ public void shouldAddLabelNode() throws Exception
}

// Then
try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertThat( graphDb.getNodeById( node ).getLabels(), equalTo( Iterables.iterable( label( labelName ) ) ) );
}
Expand All @@ -176,7 +177,7 @@ public void shouldAddLabelNodeOnce() throws Exception
tx.success();
}

try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertThat( graphDb.getNodeById( node ).getLabels(), equalTo( Iterables.iterable( label( labelName ) ) ) );
}
Expand All @@ -202,7 +203,7 @@ public void shouldRemoveLabel() throws Exception
tx.success();
}

try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertThat( graphDb.getNodeById( nodeId ).getLabels(), equalTo( Iterables.empty() ) );
}
Expand Down Expand Up @@ -249,7 +250,7 @@ public void shouldRemoveLabelOnce() throws Exception
tx.success();
}

try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertThat( graphDb.getNodeById( nodeId ).getLabels(), equalTo( Iterables.empty() ) );
}
Expand All @@ -276,7 +277,7 @@ public void shouldAddPropertyToNode() throws Exception
}

// Then
try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertThat( graphDb.getNodeById( node ).getProperty( "prop" ), equalTo( "hello" ) );
}
Expand Down Expand Up @@ -306,12 +307,102 @@ public void shouldUpdatePropertyToNode() throws Exception
}

// Then
try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertThat( graphDb.getNodeById( node ).getProperty( "prop" ), equalTo( "hello" ) );
}
}

@Test
public void shouldRemovePropertyFromNode() throws Exception
{
// Given
long node;
String propertyKey = "prop";
try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
{
Node proxy = graphDb.createNode();
proxy.setProperty( propertyKey, 42 );
node = proxy.getId();
ctx.success();
}

// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeRemoveProperty( node, token ),
equalTo( intValue( 42 ) ) );
tx.success();
}

// Then
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertFalse( graphDb.getNodeById( node ).hasProperty( "prop" ) );
}
}

@Test
public void shouldRemoveNonExistingPropertyFromNode() throws Exception
{
// Given
long node;
String propertyKey = "prop";
try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
{
node = graphDb.createNode().getId();
ctx.success();
}

// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeRemoveProperty( node, token ),
equalTo( NO_VALUE ) );
tx.success();
}

// Then
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertFalse( graphDb.getNodeById( node ).hasProperty( "prop" ) );
}
}

@Test
public void shouldRemovePropertyFromNodeTwice() throws Exception
{
// Given
long node;
String propertyKey = "prop";
try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
{
Node proxy = graphDb.createNode();
proxy.setProperty( propertyKey, 42 );
node = proxy.getId();
ctx.success();
}

// When
try ( Transaction tx = session.beginTransaction() )
{
int token = session.token().propertyKeyGetOrCreateForName( propertyKey );
assertThat( tx.dataWrite().nodeRemoveProperty( node, token ),
equalTo( intValue( 42 ) ) );
assertThat( tx.dataWrite().nodeRemoveProperty( node, token ),
equalTo( NO_VALUE ) );
tx.success();
}

// Then
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertFalse( graphDb.getNodeById( node ).hasProperty( "prop" ) );
}
}

@Test
public void shouldUpdatePropertyToNodeInTransaction() throws Exception
{
Expand All @@ -335,7 +426,7 @@ public void shouldUpdatePropertyToNodeInTransaction() throws Exception
}

// Then
try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertThat( graphDb.getNodeById( node ).getProperty( "prop" ), equalTo( 1337 ) );
}
Expand Down
Expand Up @@ -35,6 +35,7 @@
import static org.neo4j.values.storable.Values.NO_VALUE;
import static org.neo4j.values.storable.Values.stringValue;

@SuppressWarnings( "Duplicates" )
public abstract class TransactionStateTestBase<G extends KernelAPIWriteTestSupport> extends KernelAPIWriteTestBase<G>
{
@Test
Expand Down Expand Up @@ -197,8 +198,8 @@ public void shouldSeeNewNodePropertyInTransaction() throws Exception
nodeId = tx.dataWrite().nodeCreate();
int prop1 = session.token().propertyKeyGetOrCreateForName( propKey1 );
int prop2 = session.token().propertyKeyGetOrCreateForName( propKey2 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, prop1, stringValue( "hello" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, prop2, stringValue( "world" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, prop1, stringValue( "hello" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, prop2, stringValue( "world" ) ), NO_VALUE );

try ( NodeCursor node = cursors.allocateNodeCursor();
PropertyCursor property = cursors.allocatePropertyCursor() )
Expand Down Expand Up @@ -240,7 +241,7 @@ public void shouldSeeAddedPropertyFromExistingNodeWithoutPropertiesInTransaction
{

int propToken = session.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );

try ( NodeCursor node = cursors.allocateNodeCursor();
PropertyCursor property = cursors.allocatePropertyCursor() )
Expand Down Expand Up @@ -281,7 +282,7 @@ public void shouldSeeAddedPropertyFromExistingNodeWithPropertiesInTransaction()
{
nodeId = tx.dataWrite().nodeCreate();
propToken1 = session.token().propertyKeyGetOrCreateForName( propKey1 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken1, stringValue( "hello" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken1, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}

Expand All @@ -290,7 +291,7 @@ public void shouldSeeAddedPropertyFromExistingNodeWithPropertiesInTransaction()
{

propToken2 = session.token().propertyKeyGetOrCreateForName( propKey2 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken2, stringValue( "world" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken2, stringValue( "world" ) ), NO_VALUE );

try ( NodeCursor node = cursors.allocateNodeCursor();
PropertyCursor property = cursors.allocatePropertyCursor() )
Expand Down Expand Up @@ -336,14 +337,15 @@ public void shouldSeeUpdatedPropertyFromExistingNodeWithPropertiesInTransaction(
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}

// When/Then
try ( Transaction tx = session.beginTransaction() )
{
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "world" ) ), stringValue( "hello" ) );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "world" ) ),
stringValue( "hello" ) );
try ( NodeCursor node = cursors.allocateNodeCursor();
PropertyCursor property = cursors.allocatePropertyCursor() )
{
Expand All @@ -370,6 +372,92 @@ public void shouldSeeUpdatedPropertyFromExistingNodeWithPropertiesInTransaction(
}
}


@Test
public void shouldSeeRemovedPropertyInTransaction() throws Exception
{
// Given
long nodeId;
String propKey = "prop1";
int propToken;
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}

// When/Then
try ( Transaction tx = session.beginTransaction() )
{
assertEquals( tx.dataWrite().nodeRemoveProperty( nodeId, propToken ), stringValue( "hello" ) );
try ( NodeCursor node = cursors.allocateNodeCursor();
PropertyCursor property = cursors.allocatePropertyCursor() )
{
tx.dataRead().singleNode( nodeId, node );
assertTrue( "should access node", node.next() );

node.properties( property );
assertFalse( "should not find any properties", property.next() );
assertFalse( "should only find one node", node.next() );
}

tx.success();
}

try ( org.neo4j.graphdb.Transaction ignored = graphDb.beginTx() )
{
assertFalse(
graphDb.getNodeById( nodeId ).hasProperty( propKey ) );
}
}

@Test
public void shouldSeeRemovedThenAddedPropertyInTransaction() throws Exception
{
// Given
long nodeId;
String propKey = "prop1";
int propToken;
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}

// When/Then
try ( Transaction tx = session.beginTransaction() )
{
assertEquals( tx.dataWrite().nodeRemoveProperty( nodeId, propToken ), stringValue( "hello" ) );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "world" ) ), NO_VALUE );
try ( NodeCursor node = cursors.allocateNodeCursor();
PropertyCursor property = cursors.allocatePropertyCursor() )
{
tx.dataRead().singleNode( nodeId, node );
assertTrue( "should access node", node.next() );

node.properties( property );
assertTrue( property.next() );
assertEquals( propToken, property.propertyKey() );
assertEquals( property.propertyValue(), stringValue( "world" ) );

assertFalse( "should not find any properties", property.next() );
assertFalse( "should only find one node", node.next() );
}

tx.success();
}

try ( org.neo4j.graphdb.Transaction ignored = graphDb.beginTx() )
{
assertThat(
graphDb.getNodeById( nodeId ).getProperty( propKey ), equalTo( "world" ) );
}
}

private void assertLabels( LabelSet labels, int... expected )
{
assertEquals( expected.length, labels.numberOfLabels() );
Expand Down

0 comments on commit e9241cf

Please sign in to comment.