def test_edit_form(self): form_data = self.form_data.copy() form = forms.MeasurementsCreationForm(new_lab_type=self.ua, pt=self.pt, data=form_data) new_pH_value = 100 if form.is_valid(): form.save() form_data['pH'] = new_pH_value assert form_data['pH'] == new_pH_value lab = Lab.objects.get(lab_type=self.ua) edited_form = forms.MeasurementsCreationForm(new_lab_type=self.ua, pt=self.pt, lab_pk=lab.id, data=form_data) if edited_form.is_valid(): edited_form.save(lab_pk=lab.id) else: self.fail('Edited form not valid') assert len(Lab.objects.all()) == 1 assert len( ContinuousMeasurement.objects.filter( measurement_type=self.ua_pH)) == 1 new_pH = ContinuousMeasurement.objects.get(measurement_type=self.ua_pH) assert new_pH.value, new_pH_value
def test_save_form(self): """Create lab & measurement objects when form is saved""" form_data = self.form_data.copy() form = forms.MeasurementsCreationForm(new_lab_type=self.ua, pt=self.pt, data=form_data) # Before submitting forms, objects don't exist assert not Lab.objects.filter(lab_type=self.ua).exists() assert not ContinuousMeasurement.objects.filter( measurement_type=self.ua_pH).exists() assert not ContinuousMeasurement.objects.filter( measurement_type=self.ua_glucose).exists() assert not DiscreteMeasurement.objects.filter( measurement_type=self.ua_blood).exists() if form.is_valid(): form.save() assert Lab.objects.filter(lab_type=self.ua).exists(), True lab = Lab.objects.get(lab_type=self.ua) assert ContinuousMeasurement.objects.get( measurement_type=self.ua_pH, lab=lab).value == form_data['pH'] assert ContinuousMeasurement.objects.get( measurement_type=self.ua_glucose, lab=lab).value == form_data['glucose'] assert DiscreteMeasurement.objects.get( measurement_type=self.ua_blood, lab=lab).value == form_data['blood'] else: self.fail('Measurement Creation Form not valid')
def test_submit_form(self): """Complete form can be submitted""" form_data = self.form_data.copy() form = forms.MeasurementsCreationForm(new_lab_type=self.ua, pt=self.pt, data=form_data) assert len(form.errors) == 0
def test_submit_form_incomplete(self): """Can't submit incomplete form""" form_data = self.form_data.copy() form_data['blood'] = None form = forms.MeasurementsCreationForm(new_lab_type=self.ua, pt=self.pt, data=form_data) assert len(form.errors) > 0
def test_save_form_incomplete(self): """Don't create any lab & measurement objects when the form is incomplete (even when the info for some objects are complete)""" form_data = self.form_data.copy() form_data['blood'] = None form = forms.MeasurementsCreationForm(new_lab_type=self.ua, pt=self.pt, data=form_data) # Form shouldn't be valid because one measurement value is missing if form.is_valid(): form.save() assert not form.is_valid() assert not Lab.objects.filter(lab_type=self.ua).exists() assert not ContinuousMeasurement.objects.filter( measurement_type=self.ua_pH).exists() assert not ContinuousMeasurement.objects.filter( measurement_type=self.ua_glucose).exists() assert not DiscreteMeasurement.objects.filter( measurement_type=self.ua_blood).exists()