重新加载django模型以在单元测试中处理@override_settings

时间:2022-09-25 11:59:03

One of the two tests below is doomed to fail because @override_settings(....) only gets executed ONCE ever for tests. The database is rewound so that it is consistent, but the models are not reconfigured based on the settings. So if the default setting is True, the second testCaseFalse will fail, and if the default setting is False, testCaseTrue will fail.

下面的两个测试之一注定要失败,因为@override_settings(....)只会在测试时执行ONCE。数据库重绕以使其一致,但不会根据设置重新配置模型。因此,如果默认设置为True,则第二个testCaseFalse将失败,如果默认设置为False,则testCaseTrue将失败。

How in the following code do I force a reload of the Patient model so that both the unit test cases will work. I'd like this to be something I only have to do in one place instead of the 2 commented out possibilities below, should be some way to be DRY and still get this accomplished.

如何在以下代码中强制重新加载Patient模型,以便两个单元测试用例都能正常工作。我希望这是我在一个地方只需要做的事情而不是下面注释的两个可能性,应该是某种方式干燥并且仍然可以完成。

class Patient(models.Model):
    x = models.IntegerField(null=True, blank=not settings.REQUIRE_X_FOR_PATIENT)

class PatientForm(forms.ModelForm):
    #x = forms.CharField(required=settings.REQUIRE_X_FOR_PATIENT)  Don't want to have to do this
    class Meta:
        model = Patient

    #def __init__(self, *args, **kwargs):                   #Don't want to have to do this either
        #super(PatientForm, self).__init__(*args, **kwargs)
        #self.fields['x'].required = settings.REQUIRE_X_FOR_PATIENT

@override_settings(REQUIRE_X_FOR_PATIENT=True)
def testCaseTrue...
    form = PatientForm()
    self.assertTrue(form.fields['x'].required, "X should be required")

@override_settings(REQUIRE_X_FOR_PATIENT=False)
def testCaseFalse...
    form = PatientForm()
    self.assertTrue(form.fields['x'].required, "X should NOT be required")

2 个解决方案

#1


1  

I would use the setting in the form only. It doesn't seem model related to me. In fact, it is stated in the docs that the blank option is for validation-only:

我只会在表单中使用该设置。它似乎与我无关。实际上,在文档中声明空白选项仅用于验证:

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

请注意,这与null不同。 null纯粹与数据库相关,而blank与验证相关。如果字段为空= True,则表单验证将允许输入空值。如果字段为空= False,则该字段将是必填字段。

class Patient(models.Model):
    x = models.CharField(max_length...., blank=True)

class PatientForm(forms.ModelForm):
    x = forms.CharField(required=settings.REQUIRE_X_FOR_PATIENT)  
    class Meta:
        model = Patient

If you're still decided on leaving the setting in the model, you could check the answer by Chad Braun-Duin to this question. In my opinion is not worth it for just some tests and it's likely to break the test running environment.

如果您仍然决定将模型中的设置保留下来,您可以查看Chad Braun-Duin对此问题的回答。在我看来,仅仅进行一些测试是不值得的,它可能会打破测试运行环境。

#2


0  

In the end I put the logic into the model and then had the test work differently depending on the configuration. So the test became cognizant of the setting, and the right path was tested. Not ideal, but I found no way of reloading the models because of the app cache.

最后,我将逻辑放入模型中,然后根据配置进行不同的测试。因此,测试开始认识到设置,并测试了正确的路径。不理想,但由于app缓存,我发现无法重新加载模型。

I also added validation in the models clean method, raising a validation error if the configuration required it.

我还在模型清理方法中添加了验证,如果配置需要它,则会引发验证错误。

#1


1  

I would use the setting in the form only. It doesn't seem model related to me. In fact, it is stated in the docs that the blank option is for validation-only:

我只会在表单中使用该设置。它似乎与我无关。实际上,在文档中声明空白选项仅用于验证:

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

请注意,这与null不同。 null纯粹与数据库相关,而blank与验证相关。如果字段为空= True,则表单验证将允许输入空值。如果字段为空= False,则该字段将是必填字段。

class Patient(models.Model):
    x = models.CharField(max_length...., blank=True)

class PatientForm(forms.ModelForm):
    x = forms.CharField(required=settings.REQUIRE_X_FOR_PATIENT)  
    class Meta:
        model = Patient

If you're still decided on leaving the setting in the model, you could check the answer by Chad Braun-Duin to this question. In my opinion is not worth it for just some tests and it's likely to break the test running environment.

如果您仍然决定将模型中的设置保留下来,您可以查看Chad Braun-Duin对此问题的回答。在我看来,仅仅进行一些测试是不值得的,它可能会打破测试运行环境。

#2


0  

In the end I put the logic into the model and then had the test work differently depending on the configuration. So the test became cognizant of the setting, and the right path was tested. Not ideal, but I found no way of reloading the models because of the app cache.

最后,我将逻辑放入模型中,然后根据配置进行不同的测试。因此,测试开始认识到设置,并测试了正确的路径。不理想,但由于app缓存,我发现无法重新加载模型。

I also added validation in the models clean method, raising a validation error if the configuration required it.

我还在模型清理方法中添加了验证,如果配置需要它,则会引发验证错误。