Python Neo4j和Django测试
我使用Django和Neo4j以及neomodel作为图形的OGM ORM。它工作得很好,但在测试方面,Neomodel不支持关系数据库中通常的Django行为。我的意思是,它不会创建一个在测试开始时创建并在测试结束后销毁的临时Neo4j实例 我一直在做一些研究,发现了两种可能的解决方案: 第一种方法是创建一个自定义的discover runner,在其中停止开发数据库,然后从另一个路径启动测试数据库,运行测试,最后停止测试实例并再次启动开发实例。这个解决方案是在线程中提出的。以下代码适用于3.1.1 Neo4j版本:Python Neo4j和Django测试,python,django,testing,neo4j,Python,Django,Testing,Neo4j,我使用Django和Neo4j以及neomodel作为图形的OGM ORM。它工作得很好,但在测试方面,Neomodel不支持关系数据库中通常的Django行为。我的意思是,它不会创建一个在测试开始时创建并在测试结束后销毁的临时Neo4j实例 我一直在做一些研究,发现了两种可能的解决方案: 第一种方法是创建一个自定义的discover runner,在其中停止开发数据库,然后从另一个路径启动测试数据库,运行测试,最后停止测试实例并再次启动开发实例。这个解决方案是在线程中提出的。以下代码适用于3.
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)
在尝试连接到测试数据库并进行查询之前,它工作正常,因为它使用加密连接并在“~/.neo4/known_hosts”中查找连接密钥,但它与开发数据库的连接密钥冲突,并因以下错误而崩溃:
neo4j.v1.exceptions.ProtocolError: Server certificate does not match known certificate for 'localhost'; check details in file '~/.neo4j/known_hosts'
那么,有没有办法避免这种认证检查呢
第二个解决方案是创建同一个discover runner,并在Neo4j中设置一个无常数据库。问题是,除了这个,我发现的所有信息都是用Java编写的,而且我不清楚如何用python实现它。有人知道如何在python中使用它吗?不管是使用neomodel、py2neo还是直接使用python驱动程序
事先非常感谢。经过一些研究,我用第一种方法想出了一个解决方案 Neo4j的官方python驱动程序在连接到驱动程序时有一个加密的可配置参数:
GraphDatabase.driver('bolt://' + hostname, auth=basic_auth(username, password), encrypted=True)
如果加密参数设置为False,则将忽略“~/.neo4/known_hosts”,并且不会出现错误。遗憾的是,neomodel框架还不支持该参数的tunning,但我已经在github中分叉了存储库,并在全局设置中添加了一个名为ENCRYPTED_CONNECTION的变量,可以覆盖该变量以实现此目标
我正在等待拉取请求被接受,我将通知是否被接受
顺便说一句,如果有人能给我们一些在问题中评论的第二种方法的启示,那将是非常棒的
谢谢