def validate_dob(self, cleaned_data=None, model_obj=None): consent_datetime = cleaned_data.get('consent_datetime') consent_age = relativedelta(consent_datetime.date(), cleaned_data.get('dob')).years age_in_years = None try: consent_obj = self.subject_consent_cls.objects.get( screening_identifier=self.cleaned_data.get( 'screening_identifier'), version='1') except self.subject_consent_cls.DoesNotExist: age_in_years = model_obj.age_in_years if consent_age != age_in_years: message = { 'dob': 'In Subject Screening you indicated the ' 'participant is {age_in_years}, but age derived ' f'from the DOB is {consent_age}.' } self._errors.update(message) raise ValidationError(message) else: age_in_years = relativedelta(consent_datetime.date(), consent_obj.dob).years if consent_age != age_in_years: message = { 'dob': 'In previous consent the derived age of the ' f'participant is {age_in_years}, but age derived ' f'from the DOB is {consent_age}.' } self._errors.update(message) raise ValidationError(message)
def setUp(self): FlourishConsentVersion.objects.create(screening_identifier='ABC12345') self.subject_consent = SubjectConsent.objects.create( subject_identifier='11111111', screening_identifier='ABC12345', gender='M', dob=(get_utcnow() - relativedelta(years=25)).date(), consent_datetime=get_utcnow(), version='1') appointment = Appointment.objects.create( subject_identifier=self.subject_consent.subject_identifier, appt_datetime=get_utcnow(), visit_code='1000') maternal_visit = MaternalVisit.objects.create( appointment=appointment, subject_identifier=self.subject_consent.subject_identifier) self.maternal_arv_durg_preg = MaternalArvDuringPreg.objects.create( took_arv=YES, maternal_visit=maternal_visit) self.maternal_arv = MaternalArv.objects.create( maternal_arv_durg_preg=self.maternal_arv_durg_preg, arv_code='Tenoforvir', start_date=get_utcnow().date()) MaternalArv.objects.create( maternal_arv_durg_preg=self.maternal_arv_durg_preg, arv_code='Lamivudine', start_date=(get_utcnow() - relativedelta(days=2)).date()) self.ultrasound = UltraSound.objects.create( maternal_visit=maternal_visit, ga_confirmed=20)
def setUp(self): self.screening_identifier = 'ABC12345' self.study_child_identifier = '1234DCD' FlourishConsentVersion.objects.create(screening_identifier='ABC12345') self.subject_consent = SubjectConsent.objects.create( subject_identifier='11111111', screening_identifier=self.screening_identifier, gender='F', dob=(get_utcnow() - relativedelta(years=25)).date(), consent_datetime=get_utcnow(), version='1') self.consent_options = { 'subject_consent': self.subject_consent, 'consent_datetime': get_utcnow(), 'version': 1, 'child_dob': (get_utcnow() - relativedelta(years=5)).date(), 'first_name': 'TEST ONE', 'gender': FEMALE, 'child_preg_test': NOT_APPLICABLE, 'last_name': 'TEST', 'initials': 'TOT', 'identity': '123425678', 'confirm_identity': '123425678', 'citizen': YES } ChildDataset.objects.create(study_child_identifier='1112-9876', infant_sex='Female', dob=(get_utcnow() - relativedelta(years=5)).date())
def test_child_age_less_than_7years(self): self.caregiver_child_consent.child_dob = (get_utcnow() - relativedelta(years=6)).date() self.caregiver_child_consent.save_base(raw=True) self.child_assent_options.update( {'dob': (get_utcnow() - relativedelta(years=6)).date()}) form_validator = ChildAssentFormValidator( cleaned_data=self.child_assent_options) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('dob', form_validator._errors)
def setUp(self): subject_consent_model = 'flourish_child_validations.caregiverconsent' ChildAssentFormValidator.subject_consent_model = subject_consent_model prior_screening_model = 'flourish_child_validations.screeningpriorbhpparticipants' ChildAssentFormValidator.prior_screening_model = prior_screening_model child_dataset_model = 'flourish_child_validations.childdataset' ChildAssentFormValidator.child_dataset_model = child_dataset_model child_assent_model = 'flourish_child_validations.childassent' ChildAssentFormValidator.child_assent_model = child_assent_model caregiver_child_consent_model = 'flourish_child_validations.caregiverchildconsent' ChildAssentFormValidator.caregiver_child_consent_model = caregiver_child_consent_model self.screening_identifier = 'ABC12345' self.study_child_identifier = '1234DCD' ScreeningPriorBhpParticipants.objects.create( screening_identifier=self.screening_identifier, report_datetime=get_utcnow(), study_child_identifier=self.study_child_identifier) self.subject_consent = CaregiverConsent.objects.create( subject_identifier='11111111', screening_identifier=self.screening_identifier, consent_datetime=get_utcnow() - relativedelta(years=2), dob=get_utcnow() - relativedelta(years=25), version='1') self.caregiver_child_consent = CaregiverChildConsent.objects.create( subject_identifier='11111111-10', consent_datetime=get_utcnow() - relativedelta(years=2), child_dob=(get_utcnow() - relativedelta(years=8)).date(), gender=FEMALE, identity='123425678', identity_type='birth_cert', confirm_identity='123425678', version='1',) self.child_assent_options = { 'screening_identifier': self.screening_identifier, 'subject_identifier': self.caregiver_child_consent.subject_identifier, 'consent_datetime': get_utcnow(), 'version': '1', 'dob': (get_utcnow() - relativedelta(years=8)).date(), 'gender': FEMALE, 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'identity': '123425678', 'identity_type': 'birth_cert', 'confirm_identity': '123425678', 'preg_testing': YES, 'citizen': YES}
def setUp(self): MaternalLabDelFormValidator.maternal_consent_model = \ 'td_maternal_validators.subjectconsent' MaternalLabDelFormValidator.consent_version_model = \ 'td_maternal_validators.tdconsentversion' MaternalLabDelFormValidator.subject_screening_model = \ 'td_maternal_validators.subjectscreening' MaternalLabDelFormValidator.maternal_visit_model = \ 'td_maternal_validators.maternalvisit' MaternalLabDelFormValidator.maternal_arv_model = \ 'td_maternal_validators.maternalarv' MaternalLabDelFormValidator.maternal_ultrasound_init_model = \ 'td_maternal_validators.maternalultrasoundinitial' self.subject_consent = SubjectConsent.objects.create( subject_identifier='11111111', screening_identifier='ABC12345', gender='M', dob=(get_utcnow() - relativedelta(years=25)).date(), consent_datetime=get_utcnow(), version='3') self.subjectscreening = SubjectScreening.objects.create( subject_identifier=self.subject_consent.subject_identifier, screening_identifier='ABC12345', age_in_years=22) appointment = Appointment.objects.create( subject_identifier=self.subject_consent.subject_identifier, appt_datetime=get_utcnow(), visit_code='1000') maternal_visit = MaternalVisit.objects.create( appointment=appointment, subject_identifier=self.subject_consent.subject_identifier) self.maternal_arv_preg = MaternalArvPreg.objects.create( took_arv=YES, maternal_visit=maternal_visit) self.maternal_arv = MaternalArv.objects.create( maternal_arv_preg=self.maternal_arv_preg, arv_code='Tenoforvir', start_date=get_utcnow().date()) MaternalArv.objects.create(maternal_arv_preg=self.maternal_arv_preg, arv_code='Lamivudine', start_date=(get_utcnow() - relativedelta(days=2)).date()) self.td_consent_version = TdConsentVersion.objects.create( screening_identifier=self.subjectscreening.screening_identifier, version='3', report_datetime=get_utcnow()) self.maternal_ultrasound = MaternalUltraSoundInitial.objects.create( maternal_visit=maternal_visit, ga_confirmed=20)
def test_consent_dob_mismatch_consent_dob_years(self): SubjectConsent.objects.create( subject_identifier='11111111', screening_identifier=self.screening_identifier, consent_datetime=get_utcnow() - relativedelta(years=2), dob=get_utcnow() - relativedelta(years=20), version='1', ) form_validator = SubjectConsentFormValidator( cleaned_data=self.consent_options) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('dob', form_validator._errors)
def test_consent_dob_match_consent_dob_years(self): SubjectConsent.objects.create( subject_identifier='11111111', screening_identifier=self.screening_identifier, consent_datetime=get_utcnow() - relativedelta(years=2), dob=get_utcnow() - relativedelta(years=25), version='1', ) form_validator = SubjectConsentFormValidator( cleaned_data=self.consent_options) try: form_validator.validate() except ValidationError as e: self.fail(f'ValidationError unexpectedly raised. Got{e}')
def test_arv_init_date_does_not_match_start_date(self): cleaned_data = { 'report_datetime': get_utcnow(), 'subject_identifier': self.subject_consent.subject_identifier, 'arv_initiation_date': (get_utcnow() - relativedelta(days=2)).date(), 'delivery_datetime': get_utcnow() + relativedelta(weeks=5), 'valid_regiment_duration': YES, } maternal_status = MaternalStatusHelper(status=POS) MaternalLabDelFormValidator.maternal_status_helper = maternal_status form_validator = MaternalLabDelFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('arv_initiation_date', form_validator._errors)
def test_consent_dob_less_than_18years(self): self.consent_options.update( {'dob': (get_utcnow() - relativedelta(years=16)).date()}) form_validator = SubjectConsentFormValidator( cleaned_data=self.consent_options) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('dob', form_validator._errors)
def test_assent_dob_mismatch_consent_child_dob(self): self.caregiver_child_consent.child_dob = (get_utcnow() - relativedelta(years=9)).date() self.caregiver_child_consent.save_base(raw=True) form_validator = ChildAssentFormValidator( cleaned_data=self.child_assent_options) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('dob', form_validator._errors)
def setUp(self): AntenatalVisitMembershipFormValidator.maternal_consent_model = \ 'td_maternal_validators.subjectconsent' AntenatalVisitMembershipFormValidator.consent_version_model = \ 'td_maternal_validators.tdconsentversion' AntenatalVisitMembershipFormValidator.subject_screening_model = \ 'td_maternal_validators.subjectscreening' self.subject_consent = SubjectConsent.objects.create( subject_identifier='11111111', screening_identifier='ABC12345', gender='M', dob=(get_utcnow() - relativedelta(years=25)).date(), consent_datetime=get_utcnow(), version='3') self.subject_screening = SubjectScreening.objects.create( subject_identifier=self.subject_consent.subject_identifier, screening_identifier='ABC12345', age_in_years=22) self.td_consent_version = TdConsentVersion.objects.create( screening_identifier=self.subject_screening.screening_identifier, version='3', report_datetime=get_utcnow())
def test_child_dataset_dob_invalid(self): self.consent_options['study_child_identifier'] = '1112-9876' self.consent_options['child_dob'] = (get_utcnow() - relativedelta(years=6)).date() form_validator = CaregiverChildConsentFormValidator( cleaned_data=self.consent_options) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('study_child_identifier', form_validator._errors)
def test_consent_dob_mismatch_consent_dob_years(self): SubjectConsent.objects.create( subject_identifier='11111111', screening_identifier=self.screening_identifier, consent_datetime=get_utcnow() - relativedelta(years=2), dob=get_utcnow() - relativedelta(years=20), version='1') cleaned_data = { 'screening_identifier': self.screening_identifier, 'consent_datetime': get_utcnow(), 'dob': (get_utcnow() - relativedelta(years=22)).date(), 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'citizen': YES } form_validator = SubjectConsentFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('dob', form_validator._errors)
def validate_dob(self, cleaned_data=None): if self.caregiver_child_consent.child_dob != cleaned_data.get('dob'): msg = { 'dob': 'Child dob must match dob specified for the caregiver consent' f' on behalf of child {self.caregiver_child_consent.child_dob}.' } self._errors.update(msg) raise ValidationError(msg) consent_datetime = cleaned_data.get('consent_datetime') consent_age = relativedelta( consent_datetime.date(), cleaned_data.get('dob')).years if consent_datetime else None age_in_years = None try: consent_obj = self.childcontinued_consent_cls.objects.get( subject_identifier=self.cleaned_data.get( 'subject_identifier'), ) except self.childcontinued_consent_cls.DoesNotExist: if consent_age and consent_age < 18: msg = { 'dob': f'Participant is {consent_age} years of age. Child ' 'continued consent is not required.' } self._errors.update(msg) raise ValidationError(msg) else: age_in_years = relativedelta( consent_datetime.date(), consent_obj.dob).years if consent_datetime else None if age_in_years and consent_obj != age_in_years: message = { 'dob': 'In previous consent the derived age of the ' f'participant is {age_in_years}, but age derived ' f'from the DOB is {consent_obj}.' } self._errors.update(message) raise ValidationError(message)
def setUp(self): MaternalVisitFormValidator.maternal_consent_model = \ 'td_maternal_validators.subjectconsent' MaternalVisitFormValidator.consent_version_model = \ 'td_maternal_validators.tdconsentversion' MaternalVisitFormValidator.subject_screening_model = \ 'td_maternal_validators.subjectscreening' MaternalVisitFormValidator.antenatal_enrollment_model = \ 'td_maternal_validators.antenatalenrollment' MaternalVisitFormValidator.maternal_labour_del_model = \ 'td_maternal_validators.maternallabourdel' MaternalVisitFormValidator.karabo_subject_screening_model = \ 'td_maternal_validators.karabosubjectscreening' MaternalVisitFormValidator.karabo_subject_consent_model = \ 'td_maternal_validators.karabosubjectconsent' self.subject_identifier = '11111111' self.subject_screening = SubjectScreening.objects.create( subject_identifier=self.subject_identifier, screening_identifier='ABC12345', age_in_years=22) self.subject_consent = SubjectConsent.objects.create( subject_identifier=self.subject_identifier, screening_identifier='ABC12345', gender='M', dob=(get_utcnow() - relativedelta(years=25)).date(), consent_datetime=get_utcnow(), version='3') self.td_consent_version = TdConsentVersion.objects.create( screening_identifier=self.subject_screening.screening_identifier, version='3', report_datetime=get_utcnow()) self.appointment = Appointment.objects.create( subject_identifier=self.subject_consent.subject_identifier, appt_datetime=get_utcnow(), visit_code='1000M') self.appointment1 = Appointment.objects.create( subject_identifier=self.subject_consent.subject_identifier, appt_datetime=get_utcnow(), visit_code='1010M') self.appointment2 = Appointment.objects.create( subject_identifier=self.subject_consent.subject_identifier, appt_datetime=get_utcnow(), visit_code='1020M') self.appointment3 = Appointment.objects.create( subject_identifier=self.subject_consent.subject_identifier, appt_datetime=get_utcnow(), visit_code='2000M')
def test_consent_dob_match_consent_dob_years(self): SubjectConsent.objects.create( subject_identifier='11111111', screening_identifier=self.screening_identifier, consent_datetime=get_utcnow() - relativedelta(years=2), dob=get_utcnow() - relativedelta(years=20), version='1') cleaned_data = { 'screening_identifier': self.screening_identifier, 'consent_datetime': get_utcnow(), 'dob': (get_utcnow() - relativedelta(years=20)).date(), 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'citizen': YES } form_validator = SubjectConsentFormValidator(cleaned_data=cleaned_data) try: form_validator.validate() except ValidationError as e: self.fail(f'ValidationError unexpectedly raised. Got{e}')
def test_screening_age_mismatch_consent_dob_years(self): cleaned_data = { 'screening_identifier': self.screening_identifier, 'consent_datetime': get_utcnow(), 'dob': (get_utcnow() - relativedelta(years=20)).date(), 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'citizen': YES } form_validator = SubjectConsentFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('dob', form_validator._errors)
def test_citizen_does_not_match_has_omang(self): # N.B : has_omang in subject screening is set to YES cleaned_data = { 'screening_identifier': self.screening_identifier, 'consent_datetime': get_utcnow(), 'dob': (get_utcnow() - relativedelta(years=22)).date(), 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'citizen': NO } form_validator = SubjectConsentFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('citizen', form_validator._errors)
def test_current_consent_version_not_exist(self): self.td_consent_version.delete() cleaned_data = { 'screening_identifier': self.screening_identifier, 'consent_datetime': get_utcnow(), 'dob': (get_utcnow() - relativedelta(years=22)).date(), 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'citizen': YES } form_validator = SubjectConsentFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate)
def test_delivery_date_within_4wks_arv_init_date_invalid(self): cleaned_data = { 'report_datetime': get_utcnow(), 'subject_identifier': self.subject_consent.subject_identifier, 'valid_regiment_duration': YES, 'arv_initiation_date': get_utcnow().date(), 'delivery_datetime': get_utcnow() + relativedelta(weeks=2) } maternal_status = MaternalStatusHelper(status=POS) MaternalDeliveryFormValidator.maternal_status_helper = maternal_status form_validator = MaternalDeliveryFormValidator( cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('delivery_datetime', form_validator._errors)
def test_karabo_screening_invalid(self): MaternalLabourDel.objects.create( subject_identifier=self.appointment.subject_identifier, delivery_datetime=get_utcnow() - relativedelta(months=18)) cleaned_data = { 'report_datetime': get_utcnow(), 'survival_status': ALIVE, 'last_alive_date': get_utcnow().date(), 'study_status': ON_STUDY, 'appointment': self.appointment } form_validator = MaternalVisitFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('__all__', form_validator._errors)
def test_recruitment_clinic_OTHER_recruitment_clinic_other_required(self): cleaned_data = { 'screening_identifier': self.screening_identifier, 'consent_datetime': get_utcnow(), 'dob': (get_utcnow() - relativedelta(years=22)).date(), 'recruitment_clinic': OTHER, 'recruitment_clinic_other': None, 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'citizen': YES } form_validator = SubjectConsentFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('recruitment_clinic_other', form_validator._errors)
def test_still_births_one_live_births_zero_valid(self): cleaned_data = { 'report_datetime': get_utcnow(), 'subject_identifier': self.subject_consent.subject_identifier, 'arv_initiation_date': get_utcnow().date(), 'valid_regiment_duration': YES, 'delivery_datetime': get_utcnow() + relativedelta(weeks=5), 'still_births': 1, 'live_infants_to_register': 0 } form_validator = MaternalLabDelFormValidator(cleaned_data=cleaned_data) try: form_validator.validate() except ValidationError as e: self.fail(f'ValidationError unexpectedly raised. Got{e}')
def test_live_births_one_still_births_invalid(self): cleaned_data = { 'report_datetime': get_utcnow(), 'subject_identifier': self.subject_consent.subject_identifier, 'arv_initiation_date': get_utcnow().date(), 'valid_regiment_duration': YES, 'delivery_datetime': get_utcnow() + relativedelta(weeks=5), 'still_births': 1, 'live_infants_to_register': 1 } maternal_status = MaternalStatusHelper(status=POS) MaternalLabDelFormValidator.maternal_status_helper = maternal_status form_validator = MaternalLabDelFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('still_births', form_validator._errors)
def test_delivery_c_section_invalid(self): cleaned_data = { 'report_datetime': get_utcnow(), 'subject_identifier': self.subject_consent.subject_identifier, 'arv_initiation_date': get_utcnow().date(), 'delivery_datetime': get_utcnow() + relativedelta(weeks=5), 'valid_regiment_duration': YES, 'mode_delivery': 'elective c-section', 'csection_reason': None } maternal_status = MaternalStatusHelper(status=POS) MaternalLabDelFormValidator.maternal_status_helper = maternal_status form_validator = MaternalLabDelFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('csection_reason', form_validator._errors)
def test_arv_init_date_match_start_date(self): cleaned_data = { 'report_datetime': get_utcnow(), 'subject_identifier': self.subject_consent.subject_identifier, 'arv_initiation_date': get_utcnow().date(), 'delivery_datetime': get_utcnow() + relativedelta(weeks=5), 'valid_regiment_duration': YES, } maternal_status = MaternalStatusHelper(status=POS) MaternalLabDelFormValidator.maternal_status_helper = maternal_status form_validator = MaternalLabDelFormValidator(cleaned_data=cleaned_data) try: form_validator.validate() except ValidationError as e: self.fail(f'ValidationError unexpectedly raised. Got{e}')
def test_recruit_source_not_OTHER_source_other_invalid(self): cleaned_data = { 'screening_identifier': self.screening_identifier, 'consent_datetime': get_utcnow(), 'dob': (get_utcnow() - relativedelta(years=22)).date(), 'recruit_source': 'ANC clinic staff', 'recruit_source_other': 'family friend', 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'citizen': YES } form_validator = SubjectConsentFormValidator(cleaned_data=cleaned_data) self.assertRaises(ValidationError, form_validator.validate) self.assertIn('recruit_source_other', form_validator._errors)
def test_current_consent_version_valid(self): cleaned_data = { 'screening_identifier': self.screening_identifier, 'consent_datetime': get_utcnow(), 'dob': (get_utcnow() - relativedelta(years=22)).date(), 'first_name': 'TEST ONE', 'last_name': 'TEST', 'initials': 'TOT', 'citizen': YES } form_validator = SubjectConsentFormValidator(cleaned_data=cleaned_data) try: form_validator.validate() except ValidationError as e: self.fail(f'ValidationError unexpectedly raised. Got{e}')
def validate_valid_regime_hiv_pos_only(self, cleaned_data=None): if self.maternal_status_helper.hiv_status == POS: if cleaned_data.get('valid_regiment_duration') != YES: message = { 'valid_regiment_duration': 'Participant is HIV+ valid regimen duration ' 'should be YES. Please correct.' } self._errors.update(message) raise ValidationError(message) self.required_if( YES, field='valid_regiment_duration', field_required='arv_initiation_date', required_msg='You indicated participant was on valid regimen, ' 'please give a valid arv initiation date.') if (cleaned_data.get('valid_regiment_duration') == YES and (cleaned_data.get('delivery_datetime').date() - relativedelta( weeks=4) < cleaned_data.get('arv_initiation_date'))): message = { 'delivery_datetime': 'You indicated that the mother was on REGIMEN for a ' 'valid duration, but delivery date is within 4weeks ' 'of art initiation date. Please correct.' } self._errors.update(message) raise ValidationError(message) else: status = self.maternal_status_helper.hiv_status if cleaned_data.get('valid_regiment_duration') not in [ NOT_APPLICABLE ]: message = { 'valid_regiment_duration': f'Participant\'s HIV status is {status}, ' 'valid regimen duration should be Not Applicable.' } self._errors.update(message) raise ValidationError(message) if cleaned_data.get('arv_initiation_date'): message = { 'arv_initiation_date': f'Participant\'s HIV status is {status}, ' 'arv initiation date should not filled.' } self._errors.update(message) raise ValidationError(message)