0
我正在嘗試對正在開發的應用程序運行測試,並遇到問題。該應用程序使用2個數據庫,一個用於配置和關係數據的MySQL數據庫,一個用於其他非關係數據記錄的MongoDB。它在我正常使用它時起作用,因爲Y只是syncdb關係數據庫......但是當我嘗試運行測試時,Django試圖同步兩個數據庫,然後崩潰,因爲它在模型中獲得了一些mongo錯誤。有什麼建議麼?使用多個數據庫(MySQL和MongoDB)在Django上執行測試
這是DB的配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'app',
'USER': 'test',
'PASSWORD': 'test',
'HOST': '127.0.0.1',
'PORT': '3306',
},
'nonsql': {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'app',
'HOST': '127.0.0.1',
'PORT': 27017,
},
}
路由器:
class AppRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label == 'nonsql':
return 'nonsql'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'nonsql':
return 'nonsql'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'nonsql' or \
obj2._meta.app_label == 'nonsql':
return True
return None
def allow_syncdb(self, db, model):
if db == 'auth_db':
return model._meta.app_label == 'nonsql'
elif model._meta.app_label == 'nonsql':
return False
return None
測試代碼:
import pymongo
from django.test import TestCase
from app import views
from app.models import Car
from bson.objectid import ObjectId
from django.test import Client
import json
class CarTest(TestCase):
def setUp(self):
Operation.objects.create(id = ObjectId("5396d352421aa93f63ca45d5"), name = "Test Car", description= "Test Description",
start_date="2011-03-26 13:54:37.355", end_date="2014-09-11 13:54:37.355")
def test_adding_operation(self):
c = Client()
response = c.post('/cars/',{"name":"Test car 002", "description":"Test description car 002", "start_date":"2014-09-05 13:54:37.355", "end_date":"2014-09-11 13:54:37.355"})
content = json.loads(response.content)
# we expect to receive status success and an id
self.assertEqual("ok",content['status'])
self.assertNotEqual("",content['result'])
感謝分享此信息,幫助我。 – 2014-09-30 15:31:49