Testing Neo4j and Django

I use Django and Neo4j along with neomodel as OGM (ORM for graphs). It works well, but when it comes to testing, Neomodel does not support the normal behavior of Django with relational databases. I mean, it does not create a temporary instance of Neo4j, which is created at the beginning of testing and destroyed after its completion.

I did some research and I found two possible solutions:

  • First, a custom search explorer is created in which you stop the development database, then start the test database from a different path, start your tests, and finally stop the test instance and start the development instance again. This solution is proposed in the neo4j database testing Django thread . The following code has been adapted for version 3.1.1 of Neo4j:

    from time import sleep
    from subprocess import call
    
    from django.test.runner import DiscoverRunner
    from py2neo import authenticate
    
    class DiscoverRunner(DiscoverRunner):
    def setup_databases(self, *args, **kwargs):
        # Stop your development instance
        call("sudo neo4j 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("neo4j_test/neo4j-community-3.1.1/bin/neo4j"
                       " start", shell=True)
        # Need to sleep to wait for the test instance to completely come up
        sleep(10)
        if success != 0:
            return False
    
        # These lines have been commented because I've set the configuration
        # dbms.security.auth_enabled=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")
    
        #    authenticate("localhost:7474", "neo4j", "my-password")
    
        #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(DiscoverRunner, 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("neo4j_test/neo4j-community-3.1.1/bin/neo4j"
                       " stop", shell=True)
        if success != 0:
            return False
        sleep(1)
        # start back up development instance
        call("sudo neo4j start", shell=True)
    

    It works fine until it tries to connect to the test database and make a request, because it uses an encryption connection and looks in '~/.neo4/known_hosts'for a connection key, but it conflicts with one of the development database and crashes with the following error:

    neo4j.v1.exceptions.ProtocolError: Server certificate does not match known certificate for 'localhost'; check details in file '~/.neo4j/known_hosts'
    

    So, is there a way to avoid certificate verification?

  • - ImpermanentDatabase, Neo4j. , , , Java exceot this, Neo4j ImpermanentDatabase unittests python, , . - - , python (, neomodel, py2neo python)

.

+4
1

, .

python Neo4j () :

GraphDatabase.driver('bolt://' + hostname, auth=basic_auth(username, password), encrypted=True)

False, '~/.neo4/known_hosts' . , neomodel , github ENCRYPTED_CONNECTION, .

, , , .

, - , , .

.

+1

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


All Articles