问题
I am trying to create unit tests for a python project that will interface with a Neo4j Graph database.
Currently, I am implementing the embedded graph database, but will likely migrate to a REST interface if I choose to deploy this to a web application.
I have intstalled v1.9rc2 of the embedded neo4j project, installed via pip
on a virtual environment.
There are mentionings of a java class org.neo4j.test.TestGraphDatabaseFactory
, here, which sounds perfect for what I have in mind. I am currently reading and writing to a database on file, which is ok, but I am having trouble properly cleaning up after ech test that doesn't include a call to shutil.rmtree
... or is that how it should be done?
Another possible method is to create and shutdown the database for each test, via the setUp
and tearDown
methods of my TestCase
.
>>> import neo4j
>>> print neo4j.__version__
'1.9.c2'
回答1:
The best practice is to create and shutdown the database individually for each test using setUp/tearDown - exactly as you've mentioned.
side note: 1.9rc2 is rather outdated, consider upgrading to latest stable since couple of bugs have been fixed since then.
回答2:
This is the way they do it at the official Python Neo4j Driver, it should probably be considered "a good example" considering where it's coming from.
class ServerTestCase(TestCase):
""" Base class for test cases that use a remote server.
"""
known_hosts = KNOWN_HOSTS
known_hosts_backup = known_hosts + ".backup"
def setUp(self):
if isfile(self.known_hosts):
if isfile(self.known_hosts_backup):
remove(self.known_hosts_backup)
rename(self.known_hosts, self.known_hosts_backup)
def tearDown(self):
if isfile(self.known_hosts_backup):
if isfile(self.known_hosts):
remove(self.known_hosts)
rename(self.known_hosts_backup, self.known_hosts)
Here's the full source file: https://github.com/neo4j/neo4j-python-driver/blob/1.1/test/util.py
来源:https://stackoverflow.com/questions/19623949/neo4j-impermanentdatabase-in-python-unittests