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.
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 .
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.
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):
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With