我有一个应用程序,它有一些非托管模型,它们从不同数据库中的表中提取。在
我为我所有的模特都创建了工厂,在大多数情况下,工厂男孩都工作得很好!在
我遇到的问题与在其中一个字段上使用管理器的特定非托管模型有关。在
class DailyRoll(StaffModel):
id = models.IntegerField(db_column='ID', primary_key=True)
location_id = models.IntegerField(db_column='locationID')
grade = models.CharField(db_column='grade', max_length=10)
end_year = models.IntegerField(db_column='endYear')
run_date = models.DateField(db_column='runDate')
person_count = models.IntegerField(db_column='personCount')
contained_count = models.IntegerField(db_column='containedCount')
double_count = models.IntegerField(db_column='doubleCount')
aide_count = models.IntegerField(db_column='aideCount')
one_count = models.IntegerField(db_column='oneCount')
bi_count = models.IntegerField(db_column='biCount')
last_run_date = managers.DailyRollManager()
class Meta(object):
managed = False
db_table = '[DATA].[position_dailyRoll]'
经理看起来像这样:
^{pr2}$我的工厂男孩设置如下(目前非常基本,因为我只是想让它工作):
class DailyRollFactory(factory.Factory):
class Meta:
model = staff.DailyRoll
id = 1
location_id = 1
grade = 1
end_year = 2019
run_date = factory.fuzzy.FuzzyDateTime(timezone.now())
person_count = 210
contained_count = 1
double_count = 2
aide_count = 3
one_count = 4
bi_count = 5
last_run_date = managers.DailyRollManager()
我敢肯定最后一次跑步的日期设置不正确-但我不知道如何最好地处理使用经理在工厂的男孩。在
我们无法在我的数据库上运行名为django的数据库,但我们无法在默认情况下使用django来管理数据库测试。为了完成这一点,我不得不创建一个变通办法,但它是有效的。在
我的设置如下:
if 'test' in sys.argv or 'test_coverage' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'defaultdb.sqlite3'),
},
'staff': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'staff.sqlite3'),
},
}
我的测试很简单:
def test_index_returns_200_and_correct_template(self):
"""
index page returns a 200
"""
# Load test data which index needs
# if this is not included the test fails with
# 'no such table: [DATA].[position_dailyRoll]'
f.DailyRollFactory()
response = self.client.get(reverse('position:index'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'position/index.html')
如果我没有在测试中包括f.DailyRollFactory()
,它将失败,并出现以下错误:
no such table: [DATA].[position_dailyRoll]
当我包含f.DailyRollFactory()
时,会出现以下错误:
TypeError: 'last_run_date' is an invalid keyword argument for this function
我认为,最终那个经理试图查看“员工”数据库——我创建了这个数据库,但它是空的,不包含任何数据。在
我想弄明白:
A)我如何用工厂男孩模型中的数据预填充staff
数据库?我想这也许能解决我遇到的问题。会吗?我不确定。在
B)这是在工厂里处理经理的正确方法吗?我不知道该怎么处理。在
我最后使用了
Pytest-Django
,因为这个测试框架实际上允许做我想要的事情。在使用
nomigrations
标志,它获取只由django在测试中管理的模型,并在测试数据库中为它们创建适当的表名(使用它们的db_table
属性)。然后我可以使用factory_boy
来创建模拟数据并对其进行测试!在相关问题 更多 >
编程相关推荐