Skip to content

Commit

Permalink
Isolates property record access tests to store-specific testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Jun 19, 2018
1 parent ae4c6d4 commit 4175f78
Show file tree
Hide file tree
Showing 3 changed files with 340 additions and 225 deletions.
Expand Up @@ -26,17 +26,13 @@
import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.Relationship;
import org.neo4j.kernel.impl.AbstractNeo4jTestCase;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.impl.store.PropertyStore;
import org.neo4j.test.rule.DatabaseRule; import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.GraphTransactionRule; import org.neo4j.test.rule.GraphTransactionRule;
import org.neo4j.test.rule.ImpermanentDatabaseRule; import org.neo4j.test.rule.ImpermanentDatabaseRule;


import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.neo4j.graphdb.RelationshipType.withName; import static org.neo4j.graphdb.RelationshipType.withName;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.hasProperty; import static org.neo4j.test.mockito.matcher.Neo4jMatchers.hasProperty;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.inTx; import static org.neo4j.test.mockito.matcher.Neo4jMatchers.inTx;
Expand Down Expand Up @@ -65,25 +61,21 @@ private void newTx()
@Test @Test
public void canAddMultipleShortStringsToTheSameNode() public void canAddMultipleShortStringsToTheSameNode()
{ {
long recordCount = dynamicRecordsInUse();
Node node = graphdb.getGraphDatabaseAPI().createNode(); Node node = graphdb.getGraphDatabaseAPI().createNode();
node.setProperty( "key", "value" ); node.setProperty( "key", "value" );
node.setProperty( "reverse", "esrever" ); node.setProperty( "reverse", "esrever" );
commit(); commit();
assertEquals( recordCount, dynamicRecordsInUse() );
assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "value" ) ) ); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "value" ) ) );
assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "reverse" ).withValue( "esrever" ) ) ); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "reverse" ).withValue( "esrever" ) ) );
} }


@Test @Test
public void canAddShortStringToRelationship() public void canAddShortStringToRelationship()
{ {
long recordCount = dynamicRecordsInUse();
GraphDatabaseService db = graphdb.getGraphDatabaseAPI(); GraphDatabaseService db = graphdb.getGraphDatabaseAPI();
Relationship rel = db.createNode().createRelationshipTo( db.createNode(), withName( "REL_TYPE" ) ); Relationship rel = db.createNode().createRelationshipTo( db.createNode(), withName( "REL_TYPE" ) );
rel.setProperty( "type", "dimsedut" ); rel.setProperty( "type", "dimsedut" );
commit(); commit();
assertEquals( recordCount, dynamicRecordsInUse() );
assertThat( rel, inTx( db, hasProperty( "type" ).withValue( "dimsedut" ) ) ); assertThat( rel, inTx( db, hasProperty( "type" ).withValue( "dimsedut" ) ) );
} }


Expand All @@ -92,22 +84,16 @@ public void canUpdateShortStringInplace()
{ {
try try
{ {
long recordCount = dynamicRecordsInUse();
long propCount = propertyRecordsInUse();
Node node = graphdb.getGraphDatabaseAPI().createNode(); Node node = graphdb.getGraphDatabaseAPI().createNode();
node.setProperty( "key", "value" ); node.setProperty( "key", "value" );


newTx(); newTx();


assertEquals( recordCount, dynamicRecordsInUse() );
assertEquals( propCount + 1, propertyRecordsInUse() );
assertEquals( "value", node.getProperty( "key" ) ); assertEquals( "value", node.getProperty( "key" ) );


node.setProperty( "key", "other" ); node.setProperty( "key", "other" );
commit(); commit();


assertEquals( recordCount, dynamicRecordsInUse() );
assertEquals( propCount + 1, propertyRecordsInUse() );
assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "other" ) ) ); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "other" ) ) );
} }
catch ( Exception e ) catch ( Exception e )
Expand All @@ -120,216 +106,46 @@ public void canUpdateShortStringInplace()
@Test @Test
public void canReplaceLongStringWithShortString() public void canReplaceLongStringWithShortString()
{ {
long recordCount = dynamicRecordsInUse();
long propCount = propertyRecordsInUse();
Node node = graphdb.getGraphDatabaseAPI().createNode(); Node node = graphdb.getGraphDatabaseAPI().createNode();
node.setProperty( "key", LONG_STRING ); node.setProperty( "key", LONG_STRING );
newTx(); newTx();


assertEquals( recordCount + 1, dynamicRecordsInUse() );
assertEquals( propCount + 1, propertyRecordsInUse() );
assertEquals( LONG_STRING, node.getProperty( "key" ) ); assertEquals( LONG_STRING, node.getProperty( "key" ) );


node.setProperty( "key", "value" ); node.setProperty( "key", "value" );
commit(); commit();


assertEquals( recordCount, dynamicRecordsInUse() );
assertEquals( propCount + 1, propertyRecordsInUse() );
assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "value" ) ) ); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "value" ) ) );
} }


@Test @Test
public void canReplaceShortStringWithLongString() public void canReplaceShortStringWithLongString()
{ {
long recordCount = dynamicRecordsInUse();
long propCount = propertyRecordsInUse();
Node node = graphdb.getGraphDatabaseAPI().createNode(); Node node = graphdb.getGraphDatabaseAPI().createNode();
node.setProperty( "key", "value" ); node.setProperty( "key", "value" );
newTx(); newTx();


assertEquals( recordCount, dynamicRecordsInUse() );
assertEquals( propCount + 1, propertyRecordsInUse() );
assertEquals( "value", node.getProperty( "key" ) ); assertEquals( "value", node.getProperty( "key" ) );


node.setProperty( "key", LONG_STRING ); node.setProperty( "key", LONG_STRING );
commit(); commit();


assertEquals( recordCount + 1, dynamicRecordsInUse() );
assertEquals( propCount + 1, propertyRecordsInUse() );
assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( LONG_STRING ) ) ); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( LONG_STRING ) ) );
} }


@Test @Test
public void canRemoveShortStringProperty() public void canRemoveShortStringProperty()
{ {
long recordCount = dynamicRecordsInUse();
long propCount = propertyRecordsInUse();
GraphDatabaseService db = graphdb.getGraphDatabaseAPI(); GraphDatabaseService db = graphdb.getGraphDatabaseAPI();
Node node = db.createNode(); Node node = db.createNode();
node.setProperty( "key", "value" ); node.setProperty( "key", "value" );
newTx(); newTx();


assertEquals( recordCount, dynamicRecordsInUse() );
assertEquals( propCount + 1, propertyRecordsInUse() );
assertEquals( "value", node.getProperty( "key" ) ); assertEquals( "value", node.getProperty( "key" ) );


node.removeProperty( "key" ); node.removeProperty( "key" );
commit(); commit();


assertEquals( recordCount, dynamicRecordsInUse() );
assertEquals( propCount, propertyRecordsInUse() );
assertThat( node, inTx( db, not( hasProperty( "key" ) ) ) ); assertThat( node, inTx( db, not( hasProperty( "key" ) ) ) );
} }

@Test
public void canEncodeEmptyString()
{
assertCanEncode( "" );
}

@Test
public void canEncodeReallyLongString()
{
assertCanEncode( " " ); // 20 spaces
assertCanEncode( " " ); // 16 spaces
}

@Test
public void canEncodeFifteenSpaces()
{
assertCanEncode( " " );
}

@Test
public void canEncodeNumericalString()
{
assertCanEncode( "0123456789+,'.-" );
assertCanEncode( " ,'.-0123456789" );
assertCanEncode( "+ '.0123456789-" );
assertCanEncode( "+, 0123456789.-" );
assertCanEncode( "+,0123456789' -" );
assertCanEncode( "+0123456789,'. " );
// IP(v4) numbers
assertCanEncode( "192.168.0.1" );
assertCanEncode( "127.0.0.1" );
assertCanEncode( "255.255.255.255" );
}

@Test
public void canEncodeTooLongStringsWithCharsInDifferentTables()
{
assertCanEncode( "____________+" );
assertCanEncode( "_____+_____" );
assertCanEncode( "____+____" );
assertCanEncode( "HELLO world" );
assertCanEncode( "Hello_World" );
}

@Test
public void canEncodeUpToNineEuropeanChars()
{
// Shorter than 10 chars
assertCanEncode( "fågel" ); // "bird" in Swedish
assertCanEncode( "påfågel" ); // "peacock" in Swedish
assertCanEncode( "påfågelö" ); // "peacock island" in Swedish
assertCanEncode( "påfågelön" ); // "the peacock island" in Swedish
// 10 chars
assertCanEncode( "påfågelöar" ); // "peacock islands" in Swedish
}

@Test
public void canEncodeEuropeanCharsWithPunctuation()
{
assertCanEncode( "qHm7 pp3" );
assertCanEncode( "UKKY3t.gk" );
}

@Test
public void canEncodeAlphanumerical()
{
assertCanEncode( "1234567890" ); // Just a sanity check
assertCanEncodeInBothCasings( "HelloWor1d" ); // There is a number there
assertCanEncode( " " ); // Alphanum is the first that can encode 10 spaces
assertCanEncode( "_ _ _ _ _ " ); // The only available punctuation
assertCanEncode( "H3Lo_ or1D" ); // Mixed case + punctuation
assertCanEncode( "q1w2e3r4t+" ); // + is not in the charset
}

@Test
public void canEncodeHighUnicode()
{
assertCanEncode( "\u02FF" );
assertCanEncode( "hello\u02FF" );
}

@Test
public void canEncodeLatin1SpecialChars()
{
assertCanEncode( "#$#$#$#" );
assertCanEncode( "$hello#" );
}

@Test
public void canEncodeTooLongLatin1String()
{
assertCanEncode( "#$#$#$#$" );
}

@Test
public void canEncodeLowercaseAndUppercaseStringsUpTo12Chars()
{
assertCanEncodeInBothCasings( "hello world" );
assertCanEncode( "hello_world" );
assertCanEncode( "_hello_world" );
assertCanEncode( "hello::world" );
assertCanEncode( "hello//world" );
assertCanEncode( "hello world" );
assertCanEncode( "http://ok" );
assertCanEncode( "::::::::" );
assertCanEncode( " _.-:/ _.-:/" );
}

private void assertCanEncodeInBothCasings( String string )
{
assertCanEncode( string.toLowerCase() );
assertCanEncode( string.toUpperCase() );
}

private void assertCanEncode( String string )
{
encode( string, true );
}

private void encode( String string, boolean isShort )
{
long recordCount = dynamicRecordsInUse();
Node node = graphdb.getGraphDatabaseAPI().createNode();
node.setProperty( "key", string );
newTx();
if ( isShort )
{
assertEquals( recordCount, dynamicRecordsInUse() );
}
else
{
assertTrue( recordCount < dynamicRecordsInUse() );
}
assertEquals( string, node.getProperty( "key" ) );
}

private long propertyRecordsInUse()
{
return AbstractNeo4jTestCase.numberOfRecordsInUse( propertyStore() );
}

private long dynamicRecordsInUse()
{
return AbstractNeo4jTestCase.numberOfRecordsInUse( propertyStore().getStringStore() );
}

private PropertyStore propertyStore()
{
return graphdb.getGraphDatabaseAPI().getDependencyResolver().resolveDependency( RecordStorageEngine.class)
.testAccessNeoStores().getPropertyStore();
}
} }

0 comments on commit 4175f78

Please sign in to comment.