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.
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.
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")