【问题标题】:Django testing of neo4j databasedjango测试neo4j数据库
【发布时间】:2015-08-13 17:07:03
【问题描述】:

我使用 django,neo4j 作为数据库,noemodel 作为 OGM。如何测试?

当我运行 python3 manage.py test 时,所有更改都将保留。

还有我如何制作两个数据库,一个用于测试,另一个用于生产,并指定如何使用哪个数据库?

【问题讨论】:

  • 你能把你的django的设置文件,你使用的包,我正在努力将neo4j与django连接起来,任何资源都可以看看!

标签: django neo4j neomodel


【解决方案1】:

我认为保留所有更改的原因是由于使用与开发中相同的 neo4j 数据库进行测试。由于 neomodel 没有与 Django 紧密集成,因此它在测试时的行为方式与 Django 的 ORM 不同。当您使用其 ORM 运行测试时,Django 会做一些有用的事情,例如创建一个测试数据库,该数据库将在完成时被销毁。

对于 neo4j 和 neomodel,我建议执行以下操作:

创建自定义测试运行器

Django 允许您通过设置TEST_RUNNER 设置变量来定义custom test runner。一个非常简单的版本可以帮助您:

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'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")
        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)

添加辅助 neo4j 数据库

这可以通过多种方式完成,但要跟随上面的测试运行器,您可以从 neo4j's website 下载社区分发版。有了这个辅助实例,您现在可以利用测试运行器中calls 中使用的命令行语句在您想使用的数据库之间进行交换。

总结

此解决方案假设您使用的是 linux 机器,但应该可以移植到不同的操作系统,只需稍作修改。此外,我建议您查看Django's Test Runner Docs 以扩展测试运行器可以做什么。

【讨论】:

    【解决方案2】:

    目前在 neomodel 中没有使用测试数据库的机制,因为 neo4j 每个实例只有 1 个架构。

    但是,您可以在像这样运行测试时覆盖环境变量 NEO4J_REST_URL

    导出 NEO4J_REST_URL=http://localhost:7473/db/datapython3 manage.py 测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2018-10-01
      相关资源
      最近更新 更多