Django测试neo4j数据库 [英] Django testing of neo4j database

查看:1224
本文介绍了Django测试neo4j数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django与neo4j作为数据库和noemodel作为OGM。我如何测试?



当我运行 python3 manage.py test 所有的更改,我的测试是剩下的。



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

解决方案

我假定所有更改的保留原因是由于在开发过程中使用相同的neo4j数据库进行测试。由于新模型与Django并不紧密集成,所以Django的ORM在测试时并不会采取相同的方式。当您使用ORM运行测试时,Django会做一些有用的事情,例如创建一个完成后将被破坏的测试数据库。



使用neo4j和neomodel我建议执行以下操作:



创建自定义测试运行器



Django使您能够定义自定义测试运行器通过设置 TEST_RUNNER 设置变量。一个非常简单的版本可以让你去:

 从时间导入睡眠
从子进程导入调用

from django.test.runner import DiscoverRunner


class MyTestRunner(DiscoverRunner):
def setup_databases(self,* args,** kwargs):
#停止开发实例
调用(sudo service neo4j-service stop,shell = True)
#休眠以确保服务已完全停止
sleep(1)
#启动你的测试实例(详见下面的部分)
success = call(/ path / to / test / db / neo4j-community-2.2.2 / bin / neo4j
start-no-wait,shell = True)
#需要睡觉等待测试实例完全出现
sleep(10)
如果成功!= 0:
return False
try:
#对于neo4j 2.2.x,您需要设置密码或停用auth
#Nigel Small的py2neo给我们一个简单的方法来完成这个
调用(source / path / to / virtualenv / bin / activate&&
/ path / to / virtualenv / bin / neoauth
neo4j neo4j my-p4ssword)
除了OSError:
pass
#不要导入新模型,直到我们到达这里,因为我们需要等待
#才能从新模型导出新的数据库

#在运行测试之前删除db中的所有以前的条目
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
#运行测试后删除数据库中的所有以前的条目
query =match (n) - [r] - ()delete n,r
db.cypher_query(query)
sleep(1)
#关闭测试neo4j实例
success = call (/path/to/test/db/neo4j-community-2.2.2/bin/neo4j
停止,她ll = True)
如果成功!= 0:
返回False
睡眠(1)
#开始备份开发实例
调用(sudo service neo4j-service开始,shell = True)



添加辅助neo4j数据库



这可以通过几种方式完成,但是要跟上面的测试跑步者,您可以从 neo4j的网站。使用此次要实例,您现在可以使用在测试运行器中调用之间使用的命令行语句在您要使用的数据库之间进行交换。



包装



这个解决方案假设你在一个linux框上,但应该可移植到不同的操作系统稍作修改。此外,我建议您查看 Django的测试运行器文档来扩展测试运行程序可以做什么。


I'm using django with neo4j as database and noemodel as OGM. How do I test it?

When I run python3 manage.py test all the changes, my tests make are left.

And also how do I make two databases, one for testing, another for working in production and specify which one to use how?

解决方案

I assume the reason all of your changes are being retained is due to using the same neo4j database for testing as you are using in development. Since neomodel isn't integrated tightly with Django it doesn't act the same way Django's ORM does when testing. Django will do some helpful things when you run tests using its ORM, such as creating a test database that will be destroyed upon completion.

With neo4j and neomodel I'd recommend doing the following:

Create a Custom Test Runner

Django enables you to define a custom test runner by setting the TEST_RUNNER settings variable. An extremely simple version of this to get you going would be:

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)

Add a secondary neo4j database

This can be done in a couple ways but to follow along with the test runner above you can download a community distribution from neo4j's website. With this secondary instance you can now swap between which database you'd like to use utilizing the command line statements used in the calls within the test runner.

Wrap Up

This solution assume's you're on a linux box but should be portable to a different OS with minor modifications. Also I'd recommend checking out the Django's Test Runner Docs to expand upon what the test runner can do.

这篇关于Django测试neo4j数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆