Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test Neo4j in python ?

I have an application backed by Neo4j database written in python.

Does anyone know what is the best approach to unit test my application ?

I have found this can be easily done in Java with usage of ImpermanentGraphDatabase. Is there a similar approach in python ?

Any tips are appreciated.

like image 314
Jakub Bartolomej Kosuth Avatar asked Mar 18 '18 14:03

Jakub Bartolomej Kosuth


People also ask

How do you visualize a Neo4j graph in Python?

If you are using Jupyter or Tkinter I'd suggest using the Networkx library to import your graph from Neo4j and output it using Matplotlib . But if you want interaction with the graph, you will have to go with a JS library like Vis .

Can I use Neo4j with Python?

The Neo4j Python driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal, while being idiomatic to Python.


2 Answers

More answers have been provided via Neo4j community: https://community.neo4j.com/t/is-there-a-library-to-unit-test-graphdb-cypher-queries-using-python/8227/5

(Easy) Check your cypher syntax using libcypher-parser

You can also put your queries into .cypher files and use a linter like libcypher-parser (https://github.com/cleishm/libcypher-parser) to check that your queries are valid.

(Advanced) Spin up a docker instance

Use a simple docker container to spin up another Neo4j instance, then you can test to your heart's content ... There you can (in this sandboxed environment):

  • just run your cypher queries to see if they work
  • populate/seed the sandboxed Neo4j database instance and run more substantial tests
like image 149
Williams Avatar answered Sep 18 '22 15:09

Williams


I'm going to say the easiest way would be to mock out Neo4j entirely.

What I like to do is to simplify your entity classes to as little pure-python functionality as possible.

Even if you're using some fancy ORM to access your data - for example in Django - I still prefer to create a pure-python class to represent my model and create a minimalistic dao method, which will be mocked-out during unit tests. That way I don't have to ever touch the database in my unit tests, which I think is how unit tests should behave.

That being said, if you really want to have neo4j functionality in your tests, have a look at the IntegrationTestCase of the official neo4j-python-driver package. It looks like it is providing a base class to inherit your integration tests (because if you're pulling the DB into your tests, you're essentially doing Integration Tests) from, as it takes care of starting up and shutting down the server between test runs.

like image 23
samu Avatar answered Sep 18 '22 15:09

samu