django 联系neo4j,Django测试neo4j数据库

本文介绍如何使用Django结合Neo4j数据库进行有效的集成测试,包括创建定制的测试运行器,设置测试数据库,以及确保开发和测试环境分离的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值