在方法与单元测试中为 Django 单元格属性获取不同的结果

Getting different result for django cell attribute in method vs unittest

使用 Django 的工作项目,其中包含表格、行、列和单元格。 Column 有一个 reject() 方法,该方法应该将列的 is_rejected bool 属性设置为 True,然后将与该列关联的所有单元格设置为 True。 reject() 方法中的调试行看起来正常,但单元测试中的断言发现 is_rejected 对于关联的单元格为 False。

我认为可能存在竞争条件,断言发生在查找和更改单元格的过程完成之前,所以我在 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 函数中打印出的测试有效,因为您在对象上调用 reject,然后打印来自这些相同对象的字段。