Django validation of neo4j database

I am using django with neo4j as a database and noemodel as OGM. How to check it?

When I run python3 manage.py testall the changes, my tests run.

And also how to create two databases, one for testing, the other for work in production and indicating which one to use?

+1
source share
2 answers

I assume that all your changes are saved due to using the same neo4j database for testing that you use during development. Since neomodel is not tightly integrated with Django, it does not act the same as Django ORM when testing. Django will do some useful things when you run tests using your ORM, for example, creating a test database that will be destroyed upon completion.

With neo4j and neomodel, I would recommend doing the following:

Create custom tester

Django allows you to define a custom test runner by setting a configuration variable TEST_RUNNER. An extremely simple version of this for you:

from time import sleep
from subprocess import call

from django.test.runner import DiscoverRunner


class MyTestRunner(DiscoverRunner):
    def setup_databases(self, *args, **kwargs):
        # Stop your development instance
        call("sudo service neo4j-service stop", shell=True)
        # Sleep to ensure the service has completely stopped
        sleep(1)
        # Start your test instance (see section below for more details)
        success = call("/path/to/test/db/neo4j-community-2.2.2/bin/neo4j"
                       " start-no-wait", shell=True)
        # Need to sleep to wait for the test instance to completely come up
        sleep(10)
        if success != 0:
            return False
        try:
            # For neo4j 2.2.x you'll need to set a password or deactivate auth
            # Nigel Small py2neo gives us an easy way to accomplish this
            call("source /path/to/virtualenv/bin/activate && "
                 "/path/to/virtualenv/bin/neoauth "
                 "neo4j neo4j my-p4ssword")
        except OSError:
            pass
        # Don't import neomodel until we get here because we need to wait 
        # for the new db to be spawned
        from neomodel import db
        # Delete all previous entries in the db prior to running tests
        query = "match (n)-[r]-() delete n,r"
        db.cypher_query(query)
        super(MyTestRunner, self).__init__(*args, **kwargs)

    def teardown_databases(self, old_config, **kwargs):
        from neomodel import db
        # Delete all previous entries in the db after running tests
        query = "match (n)-[r]-() delete n,r"
        db.cypher_query(query)
        sleep(1)
        # Shut down test neo4j instance
        success = call("/path/to/test/db/neo4j-community-2.2.2/bin/neo4j"
                       " stop", shell=True)
        if success != 0:
            return False
        sleep(1)
        # start back up development instance
        call("sudo service neo4j-service start", shell=True)

Add secondary neo4j database

, , neo4j website. , , , call .

, Linux-, . Django Test Runner Docs, , .

+3

neomodel, neo4j .

NEO4J_REST_URL

export NEO4J_REST_URL = http://localhost:7473/db/data python3 manage.py test

+2

Source: https://habr.com/ru/post/1668643/


All Articles