def test_household_member_clone(self): survey_schedule = site_surveys.get_survey_schedules()[0] household_structure = self.member_helper.make_household_ready_for_enumeration( make_hoh=False, survey_schedule=survey_schedule) self.member_helper.add_household_member( household_structure=household_structure) self.member_helper.add_household_member( household_structure=household_structure) self.member_helper.add_household_member( household_structure=household_structure) HouseholdStructure.objects.create( household=household_structure.household, survey_schedule=site_surveys.get_survey_schedules()[1].field_value) next_household_structure = self.member_helper.get_next_household_structure_ready( household_structure, make_hoh=None) previous_members = HouseholdMember.objects.filter( household_structure=household_structure).order_by( 'report_datetime') for obj in previous_members: new_obj = obj.clone(household_structure=next_household_structure, report_datetime=next_household_structure. survey_schedule_object.start, user_created='erikvw') self.assertEqual(obj.internal_identifier, new_obj.internal_identifier) self.assertEqual(obj.subject_identifier, new_obj.subject_identifier) new_obj.save() new_obj = HouseholdMember.objects.get(pk=new_obj.pk) self.assertEqual(obj.internal_identifier, new_obj.internal_identifier) self.assertEqual(obj.subject_identifier, new_obj.subject_identifier)
def test_household_member_survey_schedule_set_correctly(self): survey_schedules = site_surveys.get_survey_schedules( group_name='test_survey') if not survey_schedules: raise AssertionError('survey_schedules is unexpectedly None') for index, survey_schedule in enumerate( site_surveys.get_survey_schedules(group_name='test_survey')): with self.subTest(index=index, survey_schedule=survey_schedule): household_structure = self.member_helper.make_household_ready_for_enumeration( survey_schedule=survey_schedule) household_member = self.member_helper.add_household_member( household_structure) self.assertEqual( household_member.survey_schedule, 'test_survey.year-{}.test_community'.format(index + 1)) self.assertEqual( household_member.survey_schedule_object.field_value, 'test_survey.year-{}.test_community'.format(index + 1)) self.assertEqual( household_member.survey_schedule_object.name, 'year-{}'.format(index + 1)) self.assertEqual( household_member.survey_schedule_object.group_name, 'test_survey') self.assertEqual( household_member.survey_schedule_object.short_name, 'test_survey.year-{}'.format(index + 1))
def test_clone_members_none(self): survey_schedule = site_surveys.get_survey_schedules()[0] household_structure = self.member_helper.make_household_ready_for_enumeration( make_hoh=False, survey_schedule=survey_schedule) next_household_structure = HouseholdStructure.objects.create( household=household_structure.household, survey_schedule=site_surveys.get_survey_schedules()[1].field_value) clone = Clone(household_structure=next_household_structure, report_datetime=household_structure.report_datetime) self.assertEqual(clone.members.all().count(), 0)
def test_eligible_member_present_saves(self): household_structure = self.household_helper.make_household_structure() self.household_helper.add_enumeration_attempt( household_structure, report_datetime=self.member_helper.get_utcnow()) mommy.make_recipe('member.representativeeligibility', household_structure=household_structure) mommy.make_recipe( 'member.householdmember', household_structure=household_structure, ) household_log = HouseholdLog.objects.get( household_structure=household_structure) options = { 'household_status': ELIGIBLE_REPRESENTATIVE_PRESENT, 'household_log': household_log.id, 'report_datetime': self.member_helper.get_utcnow() + relativedelta(hours=3), 'survey_schedule': site_surveys.get_survey_schedules(current=True)[0] } form = HouseholdLogEntryForm(data=options) self.assertTrue(form.is_valid())
def __next__(self): """Returns the next object or raises StopIteration. Skips over a None in the sequence, e.g. returns "3" in sequence 1, None, 3. """ if not self.model_obj and self.n == 0: for survey_schedule in site_surveys.get_survey_schedules(): self.model_obj = self.model_cls.objects.get( survey_schedule=survey_schedule, **self.filter_options) if self.model_obj: break elif self.model_obj: survey_schedule_object = self.model_obj.survey_schedule_object while True: survey_schedule_object = survey_schedule_object.next try: self.model_obj = self.model_cls.objects.get( survey_schedule=survey_schedule_object.field_value, **self.filter_options) except ObjectDoesNotExist: continue except AttributeError: self.model_obj = None break else: break if not self.model_obj: raise StopIteration self.n += 1 return self.model_obj
def test_refused_enumeration_fails_members_exist(self): household_structure = self.household_helper.make_household_structure() self.household_helper.add_enumeration_attempt( household_structure, report_datetime=self.member_helper.get_utcnow()) mommy.make_recipe('member.representativeeligibility', household_structure=household_structure) mommy.make_recipe( 'member.householdmember', household_structure=household_structure, ) household_log = HouseholdLog.objects.get( household_structure=household_structure) options = { 'household_status': REFUSED_ENUMERATION, 'household_log': household_log.id, 'report_datetime': self.member_helper.get_utcnow() + relativedelta(hours=3), 'survey_schedule': site_surveys.get_survey_schedules(current=True)[0] } form = HouseholdLogEntryForm(data=options) self.assertFalse(form.is_valid())
def setUp(self): self.survey_helper.load_test_surveys(load_all=True) self.household = Household.objects.create() for survey_schedule in site_surveys.get_survey_schedules(): HouseholdStructure.objects.create( household=self.household, survey_schedule=survey_schedule) self.first_household_structure = HouseholdStructure.objects.get( household=self.household, survey_schedule=survey_one.field_value) for _ in range(0, 3): subject_identifier = fake.credit_card_number() subject_identifier_as_pk = uuid4().hex internal_identifier = uuid4().hex RegisteredSubject.objects.create( subject_identifier=subject_identifier, registration_identifier=internal_identifier) mommy.make_recipe( 'member_clone.tests.householdmember', household_structure=self.first_household_structure, subject_identifier=subject_identifier, subject_identifier_as_pk=subject_identifier_as_pk, internal_identifier=internal_identifier, report_datetime=survey_one.start)
def setUp(self): self.survey_helper.load_test_surveys() django_apps.app_configs['edc_device'].device_id = '99' site_mappers.registry = {} site_mappers.loaded = False site_mappers.register(TestMapper) self.survey_schedule_object = site_surveys.get_survey_schedules()[0]
def test_make_consent_first_survey(self): first_household_structure = self.make_household_ready_for_enumeration( survey_schedule=site_surveys.get_survey_schedules()[0], make_hoh=False) first_household_member = self.add_household_member( household_structure=first_household_structure) obj = self.add_subject_consent(first_household_member) self.assertEqual(obj.survey_schedule_object.field_value, 'bcpp-survey.bcpp-year-1.test_community')
def test_creates_household_structure(self): """Asserts household structure instances are created when households are created. """ plot = self.household_helper.make_confirmed_plot(household_count=3) survey_count = len(site_surveys.get_survey_schedules(current=True)) self.assertGreater(survey_count, 0) self.assertEqual(HouseholdStructure.objects.filter( household__plot=plot).count(), survey_count * 3)
def setUp(self): self.survey_helper.load_test_surveys() django_apps.app_configs['edc_device'].device_id = '99' site_mappers.registry = {} site_mappers.loaded = False site_mappers.register(TestMapper) self.survey_schedule_object = site_surveys.get_survey_schedules()[0] self.household_structure = self.member_helper.make_household_ready_for_enumeration( make_hoh=False, report_datetime=self.survey_schedule_object.start)
def test_household_structure(self): survey_schedule = site_surveys.get_survey_schedules()[0] household_structure = self.household_helper.make_household_structure( survey_schedule=survey_schedule) self.assertIsNotNone(household_structure.survey_schedule) self.assertIsNotNone(household_structure.survey_schedule_object) self.assertRaises(AttributeError, getattr, household_structure, 'survey') self.assertRaises(AttributeError, getattr, household_structure, 'survey_object')
def setUp(self): self.user = User.objects.create_user(username='******', email='erik@…', password='******') self.client.force_login(self.user) household_structure = self.make_household_ready_for_enumeration( survey_schedule=site_surveys.get_survey_schedules()[0], make_hoh=False) first_household_member = self.add_household_member( household_structure=household_structure) self.subject_consent = self.add_subject_consent(first_household_member)
def setUp(self): self.survey_helper.load_test_surveys(load_all=True) self.household = Household.objects.create() for survey_schedule in site_surveys.get_survey_schedules(): try: HouseholdStructure.objects.get( household=self.household, survey_schedule=survey_schedule.field_value) except HouseholdStructure.DoesNotExist: HouseholdStructure.objects.create( household=self.household, survey_schedule=survey_schedule.field_value)
def test_household_member(self): survey_schedule = site_surveys.get_survey_schedules( current=True)[0] household_structure = self.member_helper.make_household_ready_for_enumeration( survey_schedule=survey_schedule) household_member = self.member_helper.add_household_member( household_structure) self.assertIsNotNone(household_member.survey_schedule) self.assertIsNotNone(household_member.survey_schedule_object) self.assertRaises( AttributeError, getattr, household_member, 'survey') self.assertRaises( AttributeError, getattr, household_member, 'survey_object')
def make_household_structure(self, survey_schedule=None, attempts=None, create=None, **options): """Returns a household_structure instance by making a new plot with households. Does not create a household structure! * attempts: add HouseholdLogEntry x <attempts>. Default: 0 * survey_schedule: Default: first current survey_schedule Adds as many HouseholdLogEntry instances as `attempts` to the Household of the given survey_schedule. """ if create is None: create = True plot = self.make_plot(**options) survey_schedule = (survey_schedule or site_surveys.get_survey_schedules(current=True)[0]) for household in plot.household_set.all(): try: household_structure = HouseholdStructure.objects.get( household=household, survey_schedule=survey_schedule.field_value) except HouseholdStructure.DoesNotExist: if create: household_structure = HouseholdStructure.objects.create( household=household, survey_schedule=survey_schedule.field_value) else: raise HouseholdTestHelperError( f'No household structure for survey schedule ' f'{survey_schedule.field_value}!!') else: # if attempts > 0 add them now self.add_attempts(household_structure, survey_schedule=survey_schedule, attempts=attempts, **options) # requery household_structure = HouseholdStructure.objects.get( pk=household_structure.pk) return household_structure
def setUp(self): self.survey_helper.load_test_surveys() django_apps.app_configs['edc_device'].device_id = '99' site_mappers.registry = {} site_mappers.loaded = False site_mappers.register(TestMapper) self.survey_schedule_object = site_surveys.get_survey_schedules()[0] self.household_structure = self.member_helper.make_household_ready_for_enumeration( make_hoh=False, report_datetime=self.survey_schedule_object.start) self.defaults = dict( household_structure=self.household_structure, report_datetime=self.household_structure.report_datetime, first_name='NOAM', initials='NC', inability_to_participate=ABLE_TO_PARTICIPATE, survival_status=ALIVE, age_in_years=25, study_resident=YES, gender=FEMALE, relation=HEAD_OF_HOUSEHOLD)
def make_household_ready_for_enumeration(self, make_hoh=None, survey_schedule=None, attempts=None, **options): """Returns household_structure after adding representative eligibility. By default returns the household_structure of the first survey_schedule. * survey_schedule: a survey schedule object. Default: first survey_schedule from `site_surveys.get_survey_schedules`. """ attempts = attempts or 1 if 'report_datetime' not in options: options['report_datetime'] = ( site_surveys.get_survey_schedules()[0].start) household_structure = self.household_helper.make_household_structure( survey_schedule=survey_schedule, attempts=1, **options) return self._make_ready(household_structure, make_hoh=make_hoh, attempts=attempts, **options)
def test_household_structure_survey_schedule_set_correctly(self): survey_schedules = site_surveys.get_survey_schedules( group_name='test_survey', current=True) if not survey_schedules: raise AssertionError('survey_schedules is unexpectedly None') for index, survey_schedule in enumerate(survey_schedules): household_structure = self.household_helper.make_household_structure( survey_schedule=survey_schedule) self.assertEqual(household_structure.survey_schedule, f'test_survey.year-{index + 1}.test_community') self.assertEqual( household_structure.survey_schedule_object.field_value, f'test_survey.year-{index + 1}.test_community') self.assertEqual(household_structure.survey_schedule_object.name, f'year-{index + 1}') self.assertEqual( household_structure.survey_schedule_object.group_name, 'test_survey') self.assertEqual( household_structure.survey_schedule_object.short_name, f'test_survey.year-{index + 1}')
def test_deletes_household_structure(self): """Asserts deletes household structure instances households are deleted. """ plot = self.household_helper.make_confirmed_plot(household_count=3) survey_count = len(site_surveys.get_survey_schedules(current=True)) self.assertGreater(survey_count, 0) # created 3 households as expected self.assertEqual(Household.objects.filter(plot=plot).count(), 3) for household in Household.objects.filter(plot=plot): self.assertEqual(HouseholdStructure.objects.filter( household=household).count(), survey_count) plot.household_count = 1 plot.save() # deleted two of the three households self.assertEqual(Household.objects.filter(plot=plot).count(), 1) for household in Household.objects.filter(plot=plot): self.assertEqual(HouseholdStructure.objects.filter( household=household).count(), survey_count)
def test_mixin_returns_household_structure_for_survey(self): for survey_schedule in site_surveys.get_survey_schedules(current=True): household_structure = self.member_helper.make_household_ready_for_enumeration( make_hoh=False, survey_schedule=survey_schedule) self.assertEqual( household_structure.survey_schedule, survey_schedule.field_value)
def setUp(self): django_apps.app_configs['edc_device'].device_id = '99' self.survey_schedule_object = site_surveys.get_survey_schedules()[0] self.household_structure = self.member_helper.make_household_ready_for_enumeration( make_hoh=False, report_datetime=self.survey_schedule_object.start) household_status = ELIGIBLE_REPRESENTATIVE_PRESENT mommy.make_recipe('household.householdlogentry', report_datetime=get_utcnow(), household_log=self.household_structure.householdlog, household_status=household_status) # add another member, OK! household_member = self.member_helper.add_household_member( self.household_structure, relation='Mother') enrollment_checklist = mommy.make_recipe( 'member.enrollmentchecklist', household_member=household_member, report_datetime=self.household_structure.report_datetime, dob=(self.household_structure.report_datetime - relativedelta(years=household_member.age_in_years)).date(), gender=household_member.gender, initials=household_member.initials, ) # fake values fake_identity = fake.credit_card_number() last_name = fake.last_name().upper() initials = enrollment_checklist.initials last_name = initials[1] + last_name options = { 'identity': '317115159', 'confirm_identity': '317115159', } try: registered_subject = RegisteredSubject.objects.get( registration_identifier=household_member.internal_identifier) except RegisteredSubject.DoesNotExist: identity = options.get('identity', fake_identity) confirm_identity = options.get('confirm_identity', fake_identity) dob = options.get('dob', enrollment_checklist.dob) else: identity = options.get('identity', registered_subject.identity) confirm_identity = options.get('confirm_identity', registered_subject.identity) dob = registered_subject.dob self.report_datetime = self.get_utcnow() consent_object = site_consents.get_consent( report_datetime=self.report_datetime, consent_model='bcpp_subject.subjectconsent') consent_options = dict( first_name=household_member.first_name, last_name=last_name, consent_datetime=self.report_datetime, version=consent_object.version, dob=dob, gender=options.get('gender', enrollment_checklist.gender), initials=initials, is_literate=options.get('is_literate', enrollment_checklist.literacy), witness_name=options.get( 'witness_name', fake.last_name() if enrollment_checklist.literacy == NO else None), legal_marriage=options.get('legal_marriage', enrollment_checklist.legal_marriage), marriage_certificate=options.get( 'marriage_certificate', enrollment_checklist.marriage_certificate), guardian_name=options.get( 'guardian_name', fake.name() if enrollment_checklist.guardian == YES else None), identity=identity, confirm_identity=confirm_identity) # add subject consent self.subject_consent = SubjectConsent.objects.create( household_member=household_member, survey_schedule=household_member.survey_schedule_object. field_value, **consent_options) SubjectLocator.objects.create( subject_identifier=self.subject_consent.subject_identifier, report_datetime=self.report_datetime, alt_contact_cell_number='72200111', has_alt_contact=NO, alt_contact_name=None, alt_contact_rel=None, alt_contact_cell=None, other_alt_contact_cell='760000111', alt_contact_tel=None)