[Neo4j] unit testing
Andreas Kollegger
andreas.kollegger at neotechnology.com
Tue Oct 19 16:25:10 CEST 2010
Hi Nikola,
Below is a baseline for unit testing neo4j. Please let me know if you have any ideas for helpers that would make writing tests easier. It might be useful to pull together a package of base classes and maybe hamcrest matchers that are common to all testing.
Cheers,
Andreas
package org.neo4j.examples;
import org.junit.*;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import java.io.File;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* An example of unit testing with Neo4j.
*/
public class Neo4jBaseTest
{
/**
* Base directory for temporary database.
*/
protected File testDirectory = new File( "target/var" );
/**
* Full path to the temporary database.
*/
protected File testDatabasePath = new File( testDirectory, "testdb" );
protected GraphDatabaseService graphDb;
/**
* Create temporary database for each unit test.
* <p/>
* This will delete any existing database prior to creating a new one.
*/
@Before
public void prepareTestDatabase()
{
deleteFileOrDirectory( testDatabasePath );
graphDb = new EmbeddedGraphDatabase( testDatabasePath.getAbsolutePath() );
}
/**
* Shutdown the database.
*/
@After
public void destroyTestDatabase()
{
graphDb.shutdown();
}
protected void deleteFileOrDirectory( File path )
{
if ( path.exists() )
{
if ( path.isDirectory() )
{
for ( File child : path.listFiles() )
{
deleteFileOrDirectory( child );
}
}
path.delete();
}
}
@Test
public void shouldCreateNode()
{
Transaction tx = graphDb.beginTx();
Node n = null;
try
{
n = graphDb.createNode();
n.setProperty( "name", "Nancy" );
tx.success();
} catch ( Exception e )
{
tx.failure();
} finally
{
tx.finish();
}
// The node should have an id greater than 0, which is the id of the reference node.
assertThat( n.getId(), is( greaterThan( 0l ) ) );
// Retrieve a node by using the id of the created node. The id's and property should match.
Node foundNode = graphDb.getNodeById( n.getId() );
assertThat( foundNode.getId(), is( n.getId() ) );
assertThat( (String) foundNode.getProperty( "name" ), is( "Nancy" ) );
}
}
On Oct 19, 2010, at 2:39 PM, nikola sijakinjic wrote:
> Hello,
>
> I am interested in neo4j unit testing. I founded this conversation in
> archive, and this looks interesting, but I failed to find function setup()
> to override.
> What class should i look for ?
>
> best,
> Nikola
>
> You can also create the graph on a temp folder and remove it
> overriding setup() on a base class.
> That's how we do it for our unit tests.
>
> 2010/2/10 Thomas Andersson <greddbul... at gmail.com>:
>> Hi,
>>
>> Is it possible to run neo4j in memory only, that is, without a
>> directory in the file system?
>>
> _______________________________________________
> Neo4j mailing list
> User at lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
More information about the User
mailing list