Permalink
Browse files

Added a Django TestCase for running django tests

  • Loading branch information...
Seth Madison
Seth Madison committed Apr 9, 2012
1 parent 73eaba3 commit 53560ed1a03ef440ff03328dc8ef20bc7268991c
Showing with 74 additions and 1 deletion.
  1. +26 −1 README.rst
  2. +20 −0 neo4django/neo4jclient.py
  3. +28 −0 neo4django/testutils.py
View
@@ -181,6 +181,32 @@ Concurrency
Because of the difficulty of transactionality over the REST API, using neo4django from multiple threads, or connecting to the same Neo4j instance from multiple servers, is not recommended. That said, we do, in fact, do this in testing environments. Hotspots like type hierarchy management are transactional, so as long as you can separate the entities being manipulated in the graph, concurrent use of neo4django is possible.
+Writing Django Tests
+====================
+
+There is a custom test case included, which you can use to write Django tests
+that need access to NodeModels. If properly configured, it will wipe out the
+neo4j database in between each test. To configure it, you must set up a neo4j
+instance with the cleandb_ extension installed. If your neo4j instance were
+configured at port 7475, and your cleandb install were pointing to
+``/cleandb/secret-key``, then you would put the following into your ``settings.py``::
+
+ NEO4J_TEST_DATABASES = {
+ 'default': {
+ 'HOST': 'localhost",
+ 'PORT': 7475,
+ 'ENDPOINT': '/db/data',
+ 'OPTIONS': {
+ 'CLEANDB_URI': '/cleandb/secret-key',
+ }
+ }
+ }
+
+With that set up, you can start writing test cases that inherit from
+``neo4django.testutils.NodeModelTestCase`` and run them as you normally would
+through your Django test suite.
+
+
Multiple Databases
==================
@@ -202,7 +228,6 @@ When possible, neo4django follows Django ORM, and thus allows some introspection
Running the Test Suite
======================
-
The test suite requires that Neo4j be running on localhost:7474, and that you have the cleandb_ extension installed at ``/cleandb``.
We test with nose_. To run the suite, set ``test_settings.py`` as your ``DJANGO_SETTINGS_MODULE`` and run ``nosetests``. In bash, that's simply::
View
@@ -1,6 +1,8 @@
+from urlparse import urlparse
from neo4jrestclient.client import GraphDatabase, RAW as RETURNS_RAW
from neo4jrestclient.request import Request
from django.conf import settings as _settings
+from django.core import exceptions
from pkg_resources import resource_stream as _pkg_resource_stream
from collections import namedtuple
@@ -19,6 +21,17 @@
other_libraries = {}
class EnhancedGraphDatabase(GraphDatabase):
+
+ def __init__(self, *args, **kwargs):
+ cleandb_uri = kwargs.pop('CLEANDB_URI', None)
+ super(EnhancedGraphDatabase, self).__init__(*args, **kwargs)
+ if cleandb_uri:
+ parsed_url = urlparse(self.url)
+ cleandb_uri = "%s://%s%s" % (parsed_url.scheme,
+ parsed_url.netloc, cleandb_uri)
+
+ self._cleandb_uri = cleandb_uri
+
def new_request(self):
# Newer versions of neo4jrestclient support auth more robustly
# the older versions do not support at all, so we have to check here
@@ -28,6 +41,13 @@ def new_request(self):
auth = {}
return Request(**auth)
+ def cleandb(self):
+ request = self.new_request()
+ response, content = request.delete(self._cleandb_uri)
+ if response.status != 200:
+ error_msg = 'The CLEANDB_URI you specified is invalid: %s'
+ raise exceptions.ImproperlyConfigured(error_msg % self._cleandb_uri)
+
def gremlin(self, script, tx=False, raw=False, **params):
"""
Execute a Gremlin script server-side and return the results.
View
@@ -0,0 +1,28 @@
+from django import test as django_test
+from django.conf import settings as _settings
+
+from neo4django.db import connections
+from utils import ConnectionHandler
+
+
+class NodeModelTestCase(django_test.TestCase):
+ """Cleans up the graph database in between each run"""
+ def __init__(self, *args, **kwargs):
+ ## TODO: This really belongs in ``setup_environment`` on a custom
+ ## runner, but we don't want to ask our users to use a custom
+ ## TestCase and a custom runner?
+ super(NodeModelTestCase, self).__init__(*args, **kwargs)
+ _settings.NEO4J_DATABASES = _settings.NEO4J_TEST_DATABASES
+ new_connections = ConnectionHandler(_settings.NEO4J_TEST_DATABASES)
+ for alias in new_connections:
+ connections[alias] = new_connections[alias]
+
+ def _pre_setup(self, *args, **kwargs):
+ for alias in connections:
+ connections[alias].cleandb()
+ return super(NodeModelTestCase, self)._pre_setup(*args, **kwargs)
+
+ def _post_teardown(self, *args, **kwargs):
+ for alias in connections:
+ connections[alias].cleandb()
+ return super(NodeModelTestCase, self)._post_teardown(*args, **kwargs)

0 comments on commit 53560ed

Please sign in to comment.