在方法vs单元测试中获取django单元格属性的不同结果

时间:2019-03-28 02:17:06

标签: python django nose

使用Django的工作项目,该项目具有表,行,列和单元格。列有一个exclude()方法,该方法应该将列的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
        )

这是仅用于运行一个单元测试的unittest调用,请注意调试行显示了将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 << ----------------------

1 个答案:

答案 0 :(得分:1)

请确保A[:,1:] -= subtraction_matrix[1:]方法实际上将对列对象和关联单元格(即A[0,1:] -= subtraction_matrix[1:]reject的Python对象所做的更改都保存到了数据库中。 。然后,在测试中,一旦您对列column.save()的拒绝调用,您将需要通过再次从数据库中读取它来刷新单元Python对象。

cell.save()

您在拒绝函数中打印出的测试有效,因为您是在对象上调用拒绝,然后从那些相同的对象中打印字段。