To run your tests, you must have a Neo4j server running (ideally a different server than the development database on a different port). One quick way to get a test database up and running is to use the built in rake task:
rake neo4j:install[community-latest,test] # or a specific version rake neo4j:install[community-3.1.0,test]
You can configure it to respond on a different port like so:
If you are using Rails, you can edit the test configuration
config/environments/test.rb or the
config/neo4j.yml file (see Setup)
How to clear the database¶
This is the most reliable way to clear your database in Neo4j
// For version of Neo4j before 2.3.0 // Nodes cannot be deleted without first deleting their relationships MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r // For version of Neo4j after 2.3.0 // DETACH DELETE takes care of removing relationships for you MATCH (n) DETACH DELETE n
# Just using the `neo4j-core` gem: neo4j_session.query('MATCH (n) DETACH DELETE n') # When using the `neo4j` gem: Neo4j::ActiveBase.current_session.query('MATCH (n) DETACH DELETE n')
If you are using
ActiveRel from the
neo4j gem you will no doubt have
SchemaMigration nodes in the database. If you delete these nodes the gem will complain that your migrations haven’t been run. To get around this you could modify the query to exclude those nodes:
MATCH (n) WHERE NOT n:`Neo4j::Migrations::SchemaMigration` DETACH DELETE n
database_cleaner gem is a popular and useful tool for abstracting away the cleaning of databases in tests. There is support for Neo4j in the
database_cleaner gem, but there are a couple of problems with it:
- Neo4j does not currently support truncation (wiping of the entire database designed to be faster than a
- Neo4j supports transactions, but nested transactions do not work the same as in relational databases. (see below)
Because of this, all strategies in the
database_cleaner gem amount to it’s “Deletion” strategy. Therefore, while you are welcome to use the
database_cleaner gem, is is generally simpler to execute one of the above Cypher queries.
Delete data files¶
Completely delete the database files (slower, by removeds schema). If you installed Neo4j via the
neo4j-rake_tasks gem, you can run:
If you are using embedded Neo4j, stop embedded db, delete the db path, start embedded db.
RSpec Transaction Rollback¶
If you are using RSpec you can perform tests in a transaction as you would using ActiveRecord. Just add the following to your rspec configuration in
# For the `neo4j-core` gem config.around do |example| session.transaction do |tx| example.run tx.mark_failed end end # For the `neo4j` gem config.around do |example| Neo4j::ActiveBase.run_transaction do |tx| example.run tx.mark_failed end end
There is one big disadvantage to this approach though: In Neo4j, nested transactions still act as one big transaction. If the code you are testing has a transaction which, for example, gets marked as failed, then the transaction around the RSpec example will be marked as failed.
If you’re using the Rack::Test <https://github.com/rack-test/rack-test> gem to test your Neo4j-enabled web application from the outside, be aware that the Rack::Test::Methods mixin won’t work with this driver. Instead, use the Rack::Test::Session approach as described in the Sinatra documentation <http://sinatrarb.com/testing.html>.