Neo4j和Django测试

6
我正在使用Django和Neo4j,同时使用neomodel作为图形数据的OGM(图形数据库的ORM)。它工作得很好,但是在测试方面,Neomodel不支持与关系型数据库一样的Django行为。我的意思是,在测试开始时,它不会创建一个临时的Neo4j实例,并在测试结束后销毁它。
我已经进行了一些研究,并找到了两个可能的解决方案:
  • The first, is creating a custom discover runner where you stop the development database, then start a test database from another path, run your tests and finally, stop the test instance and start the development instance again. This solution is proposed in the thread Django testing of neo4j database. The following code has been adapted for 3.1.1 Neo4j version:

    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's 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 query, because it uses an encrypt connection and looks up in '~/.neo4/known_hosts' for the connection key, but it conflicts with the one of the development database and crash 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 this certification checking?

  • The second solution is creating the same discover runner and set up an ImpermanentDatabase available in Neo4j. The problem is that all information I've found is in Java exceot this one, Neo4j ImpermanentDatabase in python unittests, and it doesn't make me clear how to implement this in python. Does someone has some notion of how to use it in python (it doesn't matter if it's with neomodel, py2neo or even directly with the python driver)

非常感谢您的提前帮助。
1个回答

2

经过一些研究,我采用了第一种方法来解决问题。

Neo4j的官方Python驱动程序在连接时有一个可配置的参数(encrypted):

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

如果加密参数设置为False,则“〜/ .neo4 / known_hosts”将被忽略,不会出现错误。遗憾的是,neomodel框架尚不支持调整该参数,但我已在github上分叉了存储库,并在全局设置中添加了一个名为ENCRYPTED_CONNECTION的变量,可以覆盖以实现此目标。
我正在等待拉取请求被接受,我会通知是否接受。
顺便说一句,如果有人能给我们一些关于问题中评论的第二种方法的提示,那就太棒了。
谢谢。

1
此选项现在已得到支持(自版本4.0.4起),通过ENCRYPTED配置值: https://neomodel.readthedocs.io/en/latest/configuration.html - Daniel Hawkins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接