使用Django的工作项目,它有表,行,列和单元格。 Column有一个reject()方法,该方法应该为列设置is_rejected bool属性为True,然后将与该列关联的所有单元格设置为True。 reject()方法中的调试行看起来像是在工作,但是单元测试中的断言查找关联单元格的is_rejected为False。
我认为可能存在竞争条件,其中断言发生在查找和更改单元格完成的过程之前,因此我在column.reject()之后但在断言之前等待了五秒钟。仍然失败了。
我以为我可能在单元测试中检查了错误的单元格,因此我验证了单元格值与调试行中报告的单元格值的比较,将值更改几次以查看它们在调试输出中发生的变化。
我一遍又一遍地变量名称,以确保我没有犯下愚蠢的错误,如果我这样做,我仍然看不到它。
这是Column中的reject方法(包括调试行):
class Column(BaseModel):
def reject(self):
super().reject()
#reject all cells associated with this Column
for cell in self.cell_set.all():
print("cell: ", cell)
print("rejected: ", cell.is_rejected)
cell.reject()
print("rejected: ", cell.is_rejected)
这是相关的测试:
def test_reject_column_rejects_associated_cells(self):
self.tb2 = Table.create(
team_name='FooBarTeam',
app_name='FooBarApp',
config_name='FooBarConfig2',
app_description='FooBarTeamDescription',
created_by=self.user
)
self.co3 = Column.create(
table_id=self.tb2.id,
column_name="Bar",
created_by=self.user,
created_reason="Foo reason",
column_default="abc",
regex_ids=[],
column_type="str"
)
self.rw4 = Row.create(
created_by=self.user,
table=self.tb2,
columns=['Bar'],
row_data=['123'],
reason='FooReason'
)
self.cl3 = Cell.create(
created_by = self.user,
value = "Foo!",
row = self.rw4,
column = self.co3,
reason = "FooReason"
)
self.assertEqual(
self.cl3.is_rejected,
False
)
self.assertEqual(
self.co3.is_rejected,
False
)
self.co3.reject()
self.assertEqual(
self.co3.is_rejected,
True
)
#this is the only assertion that fails
self.assertEqual(
self.cl3.is_rejected,
True
)
这是运行单元测试的单元测试调用,注意调试行显示正确的行为,cell.is_rejected属性翻转为True:
>python manage.py test tests/model_tests/test_config_models.py:ConfigReviewModelTest.test_reject_column_rejects_associated_cells
nosetests tests/model_tests/test_config_models.py:ConfigReviewModelTest.test_reject_column_rejects_associated_cells --with-coverage --cover-package=audit,config,config_importer,login,peer_review,tools,ui --exclude-dir=tests/functional_tests/test_ui --exclude-dir=tests/unit_tests --cover-html --cover-erase --cover-branches --verbosity=1
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_reject_column_rejects_associated_cells (tests.model_tests.test_config_models.ConfigReviewModelTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\dev_env\config-manager-env\config-manager\tests\model_tests\test_config_models.py", line 1000, in test_reject_column_rejects_associated_cells
True
AssertionError: False != True
-------------------- >> begin captured stdout << ---------------------
cell: Foo! - Bar
cell column: Bar
rejected: False
rejected: True
cell: 123 - Bar
cell column: Bar
rejected: False
rejected: True
--------------------- >> end captured stdout << ----------------------
确保reject
方法实际上将对Python对象所做的更改保存到数据库,对于列和关联的单元格,即column.save()
和cell.save()
。然后在测试中,一旦在列self.co3.reject()
上调用reject,您将需要通过再次从数据库中读取它来刷新单元格Python对象。
self.cl3.refresh_from_db()
self.assertEqual(
self.cl3.is_rejected,
True
)
您在拒绝功能中打印出的测试是可行的,因为您在对象上调用reject,然后从这些对象打印字段。