问题:
如何在Django
中添加多个数据库进行测试?
当我运行测试服时,出现此错误:
AssertionError:此测试中不允许对“ mig”的数据库查询。将“ mig”添加到path_to_test_suit.MigrationServiceTest.databases中,以确保适当的测试隔离并消除此故障。
这是我的PostgreSQL设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db_1_name',
'USER': 'db_1_user',
'PASSWORD': 'db_1_passwd',
'HOST': 'db_1_host',
'PORT': 'db_1_port',
},
'mig': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': db_2_name,
'USER': db_2_user,
'PASSWORD': 'db_2_passwd',
'HOST': 'db_2_host',
'PORT': 'db_2_port',
},
}
我使用
Django-nose
来运行测试服,并使用Django 2.2
答案 0 :(得分:1)
您可以在测试中使用 useEffect(() => {
const initialArray = [];
return dataRef
.get()
.then((querySnapshot) => {
const promises = [];
querySnapshot.forEach((doc) => {
const { name, uri } = doc.data();
let imageRef = fileRef.child('/' + uri);
promises.push(imageRef.getDownloadURL());
initialArray.push({
id: doc.id,
name,
});
});
return Promise.all(promises);
})
.then((urlsArray) => {
// urlsArray has the same number of elements than initialArray
const fullArray = [];
urlsArray.forEach((url, index) => {
const initialObj = initialArray[index];
const finalObj = Object.assign(initialObj, url);
fullArray.push(finalObj);
});
setItemData(fullArray);
if (loading) {
setLoading(false);
}
})
.catch(function (error) {
console.log('Error getting documents: ', error);
alert('Error: no document found.'); // It doesnt goes here if collection is empty
});
}, []);
参数。
例如:
database
有关更多信息,请参见Multi-database support。
P.S。在Django 2.2之前的最早版本中,请使用class TestYourClass(TestCase):
databases = {'default', 'mig'}
def test_some_method(self):
call_some_method()
multi_db = True
答案 1 :(得分:-1)
您需要授予DJANGO用户写权限。 然后,要进行测试并保留数据,您必须使用test_作为前缀来复制架构,并使用--keepdb键盘。