Пример #1
0
    def setUp(self):

        from bhp066.apps.bcpp.app_configuration.classes import BcppAppConfiguration

        self.household_structure = None
        self.registered_subject = None
        self.representative_eligibility = None
        self.study_site = None
        self.intervention = None

        try:
            site_lab_profiles.register(BcppSubjectProfile())
        except AlreadyRegisteredLabProfile:
            pass
        site_mappers.autodiscover()
        # set current community manually (not from settings)
        site_mappers.current_community = 'test_community'
        BcppAppConfiguration.study_start_datetime = datetime.today(
        ) + relativedelta(days=-2)
        BcppAppConfiguration.study_end_datetime = datetime.today(
        ) + relativedelta(days=45)
        bcpp_app_configuration = BcppAppConfiguration()
        # bypass any check of site_code and community against settings
        bcpp_app_configuration.confirm_site_code_in_settings = False
        bcpp_app_configuration.confirm_community_in_settings = False
        bcpp_app_configuration.prepare()
        site_lab_tracker.autodiscover()
        BcppSubjectVisitSchedule().build()

        self.survey1 = Survey.objects.get(
            survey_name='BCPP Year 1')  # see app_configuration
        plot = PlotFactory(community='test_community',
                           household_count=1,
                           status='residential_habitable')
        household = Household.objects.get(plot=plot)
        self.household_structure = HouseholdStructure.objects.get(
            household=household, survey=self.survey1)
        self.representative_eligibility = RepresentativeEligibilityFactory(
            household_structure=self.household_structure)
        self.study_site = StudySite.objects.get(
            site_code=site_mappers.get_current_mapper().map_code)
        self.intervention = site_mappers.get_current_mapper().intervention
class TestSubjectConsentForm(TestCase):

    app_label = 'bcpp_subject'
    community = 'test_community'

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def setUp(self):
        site_mappers.autodiscover()
        from bhp066.apps.bcpp_subject.visit_schedule import BcppSubjectVisitSchedule
        try:
            site_lab_profiles.register(BcppSubjectProfile())
        except AlreadyRegisteredLabProfile:
            pass
        self.app_config = BcppAppConfiguration()
        self.app_config.prepare()
        self.app_config.prep_survey_for_tests()
        site_lab_tracker.autodiscover()
        BcppSubjectVisitSchedule().build()
        site_rule_groups.autodiscover()

        plot = PlotFactory(community=self.community,
                           household_count=1,
                           status='residential_habitable')

        self.survey_bhs = Survey.objects.get(survey_slug='bcpp-year-1')

        self.survey_ahs = Survey.objects.get(survey_slug='bcpp-year-2')

        self.study_site = StudySite.objects.get(site_code='01')

        self.household_structure_bhs = HouseholdStructure.objects.get(
            household__plot=plot, survey=self.survey_bhs)
        self.household_structure_ahs = HouseholdStructure.objects.get(
            household__plot=plot, survey=self.survey_ahs)
        self.create_household_log_entry(self.household_structure_bhs)

        RepresentativeEligibilityFactory(
            household_structure=self.household_structure_bhs)

        self.male_dob = date.today() - relativedelta(years=25)
        self.male_age_in_years = 25
        self.male_first_name = 'ERIK'
        self.male_last_name = 'HIEWAI'
        self.male_initials = "EW"

        self.household_member_male_T0 = HouseholdMemberFactory(
            household_structure=self.household_structure_bhs,
            gender='M',
            age_in_years=self.male_age_in_years,
            first_name=self.male_first_name,
            initials=self.male_initials)
        HeadHouseholdEligibilityFactory(
            household_member=self.household_member_male_T0,
            household_structure=self.household_structure_bhs)
        self.household_member_male_T0.eligible_hoh = True
        self.household_member_male_T0.save()

        HouseholdInfoFactory(household_member=self.household_member_male_T0,
                             household_structure=self.household_structure_bhs,
                             registered_subject=self.household_member_male_T0.
                             registered_subject)

        self.enrollment = EnrollmentChecklistFactory(
            household_member=self.household_member_male_T0,
            gender='M',
            citizen='Yes',
            dob=self.male_dob,
            guardian='No',
            initials=self.household_member_male_T0.initials,
            part_time_resident='Yes')
        self.subject_consent_male = SubjectConsentFactory(
            household_member=self.household_member_male_T0,
            confirm_identity='101119811',
            identity='101119811',
            study_site=self.study_site,
            gender='M',
            dob=self.male_dob,
            first_name=self.male_first_name,
            initials=self.male_initials)
        self.data = {
            'last_name':
            'WIZZY',
            'is_minor':
            'No',
            'witness_name':
            None,
            'is_literate':
            'Yes',
            'subject_type':
            'subject',
            'consent_copy':
            'Yes',
            'is_verified':
            False,
            'consent_signature':
            None,
            'first_name':
            'ERIK',
            'dm_comment':
            None,
            'is_dob_estimated':
            None,
            'verified_by':
            None,
            'user_modified':
            u'',
            'is_signed':
            True,
            'is_verified_datetime':
            None,
            'subject_identifier_aka':
            None,
            'version':
            u'4',
            'citizen':
            'Yes',
            'legal_marriage':
            u'N/A',
            'assessment_score':
            'Yes',
            'is_incarcerated':
            'No',
            'consent_reviewed':
            'Yes',
            'study_questions':
            'Yes',
            'sid':
            None,
            'study_site_id':
            self.study_site.id,
            'may_store_samples':
            YES,
            'consent_version_recent':
            None,
            'subject_identifier':
            self.household_member_male_T0.registered_subject.
            subject_identifier,
            'subject_identifier_as_pk':
            self.household_member_male_T0.registered_subject.id,
            'community':
            u'test_community',
            'using':
            'default',
            'marriage_certificate_no':
            None,
            'identity':
            '317918515',
            'confirm_identity':
            '317918515',
            'registered_subject_id':
            self.household_member_male_T0.registered_subject.id,
            'identity_type':
            'OMANG',
            'language':
            u'not specified',
            'guardian_name':
            None,
            'gender':
            'M',
            'household_member':
            self.household_member_male_T0.id,
            'marriage_certificate':
            u'N/A',
            'dob':
            self.male_dob,
            'study_site':
            self.study_site.id,
            'initials':
            'EW',
            'language':
            'en',
            'is_dob_estimated':
            '-',
            'consent_signature':
            YES,
            'consent_datetime':
            datetime.today(),
            'version':
            1
        }

    def create_household_log_entry(self, household_structure):
        household_log = HouseholdLog.objects.filter(
            household_structure=household_structure).last()
        HouseholdLogEntry.objects.all().delete()
        HouseholdLogEntryFactory(household_log=household_log)

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_subject_consent_age_above_64_at_bhs(self):
        """ For all participants who are above 64 at bhs are not eligible."""
        from bhp066.apps.bcpp_subject.forms.subject_consent_form import SubjectConsentForm
        self.male_dob = date.today() - relativedelta(years=65)
        self.data['dob'] = self.male_dob
        self.data['household_member'] = self.household_member_male_T0.id
        consent_form = SubjectConsentForm(data=self.data)
        self.assertIn(
            u"Subject's age is 65y. Subject is not eligible for consent.",
            consent_form.errors.get("__all__"))

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-1',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_subject_consent_age_within_65_at_bhs(self):
        """ For all participants within 65 at bhs are eligible."""
        self.app_config.prep_survey_for_tests()
        self.subject_consent_male.version = 2
        self.subject_consent_male.save_base()
        from bhp066.apps.bcpp_subject.forms.subject_consent_form import SubjectConsentForm
        dob = date.today() - relativedelta(years=64)
        self.household_member_male_T0.age_in_years = 64
        self.household_member_male_T0.save()
        self.enrollment.dob = dob
        self.enrollment.save_base()
        self.data['dob'] = dob
        self.data['identity'] = '317918514'
        self.data['confirm_identity'] = '317918514'
        consent_form = SubjectConsentForm(data=self.data)
        print consent_form.errors
        consent_form.save()
        self.assertEqual(
            RegisteredSubject.objects.filter(
                identity=self.data['identity']).count(), 1)

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_subject_consent_age_above_64_at_ahs(self):
        """ Test identity on
        """

        dob = date.today() - relativedelta(years=65)
        self.household_member_male_T0.age_in_years = 65
        self.household_member_male_T0.save_base()
        self.subject_consent_male.dob = dob
        self.enrollment.dob = dob
        self.enrollment.save_base()
        self.subject_consent_male.save_base()

        self.app_config.prep_survey_for_tests()
        from bhp066.apps.bcpp_subject.forms.subject_consent_form import SubjectConsentForm

        enumeration_helper = EnumerationHelper(
            self.household_structure_ahs.household, self.survey_bhs,
            self.survey_ahs)
        enumeration_helper.add_members_from_survey()
        self.household_member = HouseholdMember.objects.get(
            registered_subject__identity='101119811',
            household_structure__survey=self.survey_ahs,
        )
        dob = date.today() - relativedelta(years=65)
        self.household_member.age_in_years = 65
        self.household_member.save_base()

        HeadHouseholdEligibilityFactory(
            household_member=self.household_member,
            household_structure=self.household_member.household_structure)
        self.enrollment.dob = dob
        self.enrollment.save_base()

        self.subject_consent = SubjectConsent.objects.get(
            household_member=self.household_member_male_T0)
        self.subject_consent.version = 2
        self.subject_consent.save_base()

        self.create_household_log_entry(self.household_structure_ahs)

        self.data['dob'] = dob
        self.data['household_member'] = self.household_member.id
        self.data['identity'] = '101119811'
        self.data['confirm_identity'] = '101119811'
        consent_form = SubjectConsentForm(data=self.data)
        print consent_form.errors
        self.assertTrue(consent_form.is_valid())

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_subject_consent_age_within_65_at_ahs(self):
        """ Test identity on
        """
        self.app_config.prep_survey_for_tests()
        from bhp066.apps.bcpp_subject.forms.subject_consent_form import SubjectConsentForm

        enumeration_helper = EnumerationHelper(
            self.household_structure_ahs.household, self.survey_bhs,
            self.survey_ahs)
        enumeration_helper.add_members_from_survey()
        self.household_member = HouseholdMember.objects.get(
            registered_subject__identity='101119811',
            household_structure__survey=self.survey_ahs)
        HeadHouseholdEligibilityFactory(
            household_member=self.household_member,
            household_structure=self.household_member.household_structure)
        self.subject_consent = SubjectConsent.objects.get(
            household_member=self.household_member_male_T0)
        self.subject_consent.version = 2
        self.subject_consent.save_base()

        self.data['household_member'] = self.household_member.id
        self.data['identity'] = '101119811'
        self.data['confirm_identity'] = '101119811'

        self.create_household_log_entry(self.household_structure_ahs)

        consent_form = SubjectConsentForm(data=self.data)
        self.assertTrue(consent_form.is_valid())

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_surname_marrige_at_ahs(self):
        """ Test identity on
        """
        self.app_config.prep_survey_for_tests()
        from bhp066.apps.bcpp_subject.forms.subject_consent_form import SubjectConsentForm
        enumeration_helper = EnumerationHelper(
            self.household_structure_ahs.household, self.survey_bhs,
            self.survey_ahs)
        enumeration_helper.add_members_from_survey()
        self.household_member = HouseholdMember.objects.get(
            registered_subject__identity='101119811',
            household_structure__survey=self.survey_ahs)
        self.household_member.personal_details_changed = YES
        self.household_member.details_change_reason = 'married'
        self.household_member.last_name = 'SETSIBA'
        self.household_member.initials = 'ES'
        self.household_member.save()

        self.create_household_log_entry(self.household_structure_ahs)

        HeadHouseholdEligibilityFactory(
            household_member=self.household_member,
            household_structure=self.household_member.household_structure)

        self.subject_consent = SubjectConsent.objects.get(
            household_member=self.household_member_male_T0)
        self.subject_consent.version = 2
        self.subject_consent.save_base()
        self.data['household_member'] = self.household_member.id
        self.data['identity'] = '101119811'
        self.data['confirm_identity'] = '101119811'
        self.data['last_name'] = 'SETSIBA'
        self.data['initials'] = 'ES'
        consent_form = SubjectConsentForm(data=self.data)
        self.assertTrue(consent_form.is_valid())
        consent_form.save()
        consent = SubjectConsent.objects.get(
            household_member=self.household_member)
        consent_form = SubjectConsentForm(data=self.data, instance=consent)
        consent_form.save()

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-1',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_validate_legal_marriage_at_bhs(self):
        self.data['citizen'] = NO
        self.data['identity'] = '101119811'
        self.data['confirm_identity'] = '101119811'
        from bhp066.apps.bcpp_subject.forms.subject_consent_form import SubjectConsentForm
        self.data['legal_marriage'] = YES
        self.data['marriage_certificate'] = YES
        self.data['marriage_certificate_no'] = '12421'
        self.enrollment.citizen = NO
        self.enrollment.save_base()
        consent_form = SubjectConsentForm(data=self.data)
        print consent_form.errors
        self.assertTrue(consent_form.is_valid())

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-1',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_validate_legal_marriage_at_bhs_not_valid(self):
        self.data['citizen'] = NO
        self.data['identity'] = '101119811'
        self.data['confirm_identity'] = '101119811'
        from bhp066.apps.bcpp_subject.forms.subject_consent_form import SubjectConsentForm
        self.data['legal_marriage'] = YES
        self.data['marriage_certificate'] = YES
        self.enrollment.citizen = NO
        self.enrollment.save_base()
        consent_form = SubjectConsentForm(data=self.data)
        self.assertIn(
            u'You wrote subject is NOT a citizen and has marriage certificate. Please provide certificate number.',
            consent_form.errors.get("__all__"))
        self.assertFalse(consent_form.is_valid())
Пример #3
0
class TestUpdateCallList(TestCase):

    @override_settings(
        SITE_CODE='01', CURRENT_COMMUNITY='test_community', CURRENT_SURVEY='bcpp-year-1',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def setUp(self):
        site_mappers.autodiscover()
        try:
            site_lab_profiles.register(BcppSubjectProfile())
        except AlreadyRegisteredLabProfile:
            pass
        self.app_config = BcppAppConfiguration()
        self.app_config.prepare()
        site_lab_tracker.autodiscover()
        BcppSubjectVisitSchedule().build()
        site_rule_groups.autodiscover()
        self.mapper = site_mappers.get_current_mapper()
#         self.app_config.prep_survey_for_tests()

        self.community = 'test_community'
        plot = PlotFactory(community=self.community, household_count=1, status='residential_habitable')

        survey = Survey.objects.all().order_by('datetime_start')[0]

        self.study_site = StudySite.objects.get(site_code='01')

        self.household_structure = HouseholdStructure.objects.get(household__plot=plot, survey=survey)
        RepresentativeEligibilityFactory(household_structure=self.household_structure)

        self.male_dob = date.today() - relativedelta(years=25)
        self.male_age_in_years = 25
        self.male_first_name = 'ERIK'
        self.male_initials = "EW"

        self.household_member_male_T0 = HouseholdMemberFactory(
            household_structure=self.household_structure, gender='M',
            age_in_years=self.male_age_in_years, first_name=self.male_first_name,
            initials=self.male_initials
        )

        EnrollmentChecklistFactory(
            household_member=self.household_member_male_T0,
            gender='M',
            citizen='Yes',
            dob=self.male_dob,
            guardian='No',
            initials=self.household_member_male_T0.initials,
            part_time_resident='Yes'
        )

        self.subject_consent_male = SubjectConsentFactory(household_member=self.household_member_male_T0, study_site=self.study_site, gender='M', dob=self.male_dob, first_name=self.male_first_name, initials=self.male_initials)

        self.registered_subject_male = RegisteredSubject.objects.get(subject_identifier=self.subject_consent_male.subject_identifier)

        self.appointment_male_T0 = Appointment.objects.get(registered_subject=self.registered_subject_male, visit_definition__code='T0')

        self.subject_visit_male_T0 = SubjectVisitFactory(appointment=self.appointment_male_T0, household_member=self.household_member_male_T0)
        SubjectLocatorFactory(registered_subject=self.subject_visit_male_T0.appointment.registered_subject, subject_visit=self.subject_visit_male_T0)
        self.household_member_T1 = None
        self.update_call_list_class = UpdateCallList()

    @override_settings(
        SITE_CODE='01', CURRENT_COMMUNITY='test_community', CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_update_call_list1(self):
        """Test creation of a year 2 household member who is consented and their member status has to be annual."""

        self.update_call_list_class.update_call_list('test_community', 'bcpp-year-1', 't1-prep')
        self.assertEqual(HouseholdMember.objects.filter(
            household_structure__survey__survey_slug='bcpp-year-2').count(), 1)

        household_member = HouseholdMember.objects.get(household_structure__survey__survey_slug='bcpp-year-2', internal_identifier=self.household_member_male_T0.internal_identifier)
        self.assertEqual(household_member.member_status, ANNUAL)
        self.assertEqual(CallList.objects.filter(household_member=household_member).count(), 1)

    @override_settings(
        SITE_CODE='01', CURRENT_COMMUNITY='test_community', CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def overiding_settings_file(self):
        self.update_call_list_class.update_call_list('test_community', 'bcpp-year-1', 't1-prep')
        return HouseholdMember.objects.get(household_structure__survey__survey_slug='bcpp-year-2')

    @override_settings(
        SITE_CODE='01', CURRENT_COMMUNITY='test_community', CURRENT_SURVEY='bcpp-year-3',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_update_call_list2(self):
        """Test creation of a year 3 household member who is consented and their member status has to be annual."""

        self.household_member_T1 = self.overiding_settings_file()
        registered_subject = self.household_member_T1.registered_subject

        self.update_call_list_class.update_call_list('test_community', 'bcpp-year-2', 't2-prep')

        self.assertEqual(HouseholdMember.objects.filter(
            household_structure__survey__survey_slug='bcpp-year-3').count(), 1)

        household_member_year3 = HouseholdMember.objects.get(household_structure__survey__survey_slug='bcpp-year-3')

        self.assertEqual(CallList.objects.filter(household_member=household_member_year3).count(), 1)
        self.assertEqual(household_member_year3.member_status, ANNUAL)
        self.assertEqual(CallList.objects.filter(household_member=household_member_year3).count(), 1)
        self.assertEqual(HouseholdMember.objects.filter(registered_subject=registered_subject).count(), 3)

    @override_settings(
        SITE_CODE='01', CURRENT_COMMUNITY='test_community', CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_annual_member_absentee(self):
        """Test if the member status of an annual member who is absent is calculated correctly."""
        self.update_call_list_class.update_call_list('test_community', 'bcpp-year-1', 't1-prep')
        self.assertEqual(HouseholdMember.objects.filter(
            household_structure__survey__survey_slug='bcpp-year-2').count(), 1)

        household_member = HouseholdMember.objects.get(household_structure__survey__survey_slug='bcpp-year-2', internal_identifier=self.household_member_male_T0.internal_identifier)
        self.assertEqual(household_member.member_status, ANNUAL)
        self.assertEqual(CallList.objects.filter(household_member=household_member).count(), 1)
        household_member.member_status = ABSENT
        household_member.save()
        absentee = SubjectAbsenteeFactory(household_member=household_member)
        SubjectAbsenteeEntryFactory(subject_absentee=absentee)
        self.assertEquals(SubjectAbsentee.objects.filter(household_member=household_member).count(), 1)
        self.assertEquals(SubjectAbsenteeEntry.objects.filter(subject_absentee=absentee).count(), 1)
        SubjectAbsenteeEntryFactory(subject_absentee=absentee)
        self.assertEquals(SubjectAbsenteeEntry.objects.filter(subject_absentee=absentee).count(), 2)
        SubjectAbsenteeEntryFactory(subject_absentee=absentee)
        self.assertEquals(SubjectAbsenteeEntry.objects.filter(subject_absentee=absentee).count(), 3)
        household_member = HouseholdMember.objects.get(household_structure__survey__survey_slug='bcpp-year-2', internal_identifier=self.household_member_male_T0.internal_identifier)
        self.assertEqual(household_member.member_status, ABSENT)

    @override_settings(
        SITE_CODE='01', CURRENT_COMMUNITY='test_community', CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_annual_member_refusal(self):
        """Test if the member status of an annual member who has refused is calculated correctly."""
        self.update_call_list_class.update_call_list('test_community', 'bcpp-year-1', 't1-prep')
        self.assertEqual(HouseholdMember.objects.filter(
            household_structure__survey__survey_slug='bcpp-year-2').count(), 1)

        household_member = HouseholdMember.objects.get(household_structure__survey__survey_slug='bcpp-year-2', internal_identifier=self.household_member_male_T0.internal_identifier)
        self.assertEqual(household_member.member_status, ANNUAL)
        self.assertEqual(CallList.objects.filter(household_member=household_member).count(), 1)

        household_member.member_status = REFUSED
        household_member.save(update_fields=['member_status'])
        self.assertFalse(household_member.refused)

        household_member = HouseholdMember.objects.get(pk=household_member.pk)
        SubjectRefusalFactory(household_member=household_member)
        self.assertEqual(household_member.member_status, REFUSED)
        self.assertTrue(household_member.refused)
#         if self.mapper.intervention:
#             self.assertEqual(household_member.member_status, REFUSED)
#             self.assertTrue(household_member.refused)
#         else:
#             self.assertEqual(household_member.member_status, HTC_ELIGIBLE)
#             self.assertTrue(household_member.refused)
#         self.assertEqual(household_member.member_status, REFUSED)

    @override_settings(
        SITE_CODE='01', CURRENT_COMMUNITY='test_community', CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_annual_member_absent(self):
        """Test if the member status of an annual member who is absent is calculated correctly."""
        self.update_call_list_class.update_call_list('test_community', 'bcpp-year-1', 't1-prep')
        self.assertEqual(HouseholdMember.objects.filter(
            household_structure__survey__survey_slug='bcpp-year-2').count(), 1)

        household_member = HouseholdMember.objects.get(household_structure__survey__survey_slug='bcpp-year-2', internal_identifier=self.household_member_male_T0.internal_identifier)
        self.assertEqual(household_member.member_status, ANNUAL)
        self.assertEqual(CallList.objects.filter(household_member=household_member).count(), 1)

        household_member.present_today = NO
        household_member.save(update_fields=['present_today'])

        household_member = HouseholdMember.objects.get(pk=household_member.pk)
        self.assertTrue(household_member.absent)
        self.assertEqual(household_member.member_status, ABSENT)
class TestMostRecentForm(TestCase):

    app_label = 'bcpp_subject'
    community = 'test_community'

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-2',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def setUp(self):
        site_mappers.autodiscover()
        from bhp066.apps.bcpp_subject.visit_schedule import BcppSubjectVisitSchedule
        try:
            site_lab_profiles.register(BcppSubjectProfile())
        except AlreadyRegisteredLabProfile:
            pass
        self.app_config = BcppAppConfiguration()
        self.app_config.prepare()
        self.app_config.prep_survey_for_tests()
        site_lab_tracker.autodiscover()
        BcppSubjectVisitSchedule().build()
        site_rule_groups.autodiscover()

        plot = PlotFactory(community=self.community,
                           household_count=1,
                           status='residential_habitable')

        self.survey_bhs = Survey.objects.get(survey_slug='bcpp-year-1')

        self.survey_ahs = Survey.objects.get(survey_slug='bcpp-year-2')

        self.study_site = StudySite.objects.get(site_code='01')

        self.household_structure_bhs = HouseholdStructure.objects.get(
            household__plot=plot, survey=self.survey_bhs)
        self.household_structure_ahs = HouseholdStructure.objects.get(
            household__plot=plot, survey=self.survey_ahs)
        self.create_household_log_entry(self.household_structure_bhs)

        RepresentativeEligibilityFactory(
            household_structure=self.household_structure_bhs)

        self.male_dob = date.today() - relativedelta(years=25)
        self.male_age_in_years = 25
        self.male_first_name = 'ERIK'
        self.male_last_name = 'HIEWAI'
        self.male_initials = "EW"

        self.household_member_male_T0 = HouseholdMemberFactory(
            household_structure=self.household_structure_bhs,
            gender='M',
            age_in_years=self.male_age_in_years,
            first_name=self.male_first_name,
            initials=self.male_initials)
        HeadHouseholdEligibilityFactory(
            household_member=self.household_member_male_T0,
            household_structure=self.household_structure_bhs)
        self.household_member_male_T0.eligible_hoh = True
        self.household_member_male_T0.save()

        HouseholdInfoFactory(household_member=self.household_member_male_T0,
                             household_structure=self.household_structure_bhs,
                             registered_subject=self.household_member_male_T0.
                             registered_subject)

        self.enrollment = EnrollmentChecklistFactory(
            household_member=self.household_member_male_T0,
            gender='M',
            citizen='Yes',
            dob=self.male_dob,
            guardian='No',
            initials=self.household_member_male_T0.initials,
            part_time_resident='Yes')
        self.subject_consent_male = SubjectConsentFactory(
            household_member=self.household_member_male_T0,
            confirm_identity='101119811',
            identity='101119811',
            study_site=self.study_site,
            gender='M',
            dob=self.male_dob,
            first_name=self.male_first_name,
            initials=self.male_initials)
        self.appointment = Appointment.objects.get(
            registered_subject=self.household_member_male_T0.
            registered_subject,
            visit_definition__code='T0')

        self.subject_visit = SubjectVisitFactory(
            household_member=self.household_member_male_T0,
            appointment=self.appointment)

        self.registered_subject = RegisteredSubject.objects.get(
            subject_identifier=self.subject_consent_male.subject_identifier)

        self.sexual_data = {
            'subject_visit': self.subject_visit.id,
            'alcohol_sex': 'Myself',
            'report_datetime': datetime.today(),
            'condom': NO,
            'more_sex': NO,
            'ever_sex': YES,
            'first_sex': 20,
            'last_year_partners': None,
            'lifetime_sex_partners': 1,
            'more_sex': None,
            'subject_visit_id': None,
            'user_modified': u'',
        }
        self.data = {
            'subject_visit': self.subject_visit.id,
            'concurrent': NO,
            'first_partner_live': None,
            'first_condom_freq': u'Sometimes',
            'first_disclose': u'Yes',
            'first_exchange': u'30-39',
            'first_first_sex': u'Years',
            'first_first_sex_calc': 3L,
            'first_haart': None,
            'first_partner_arm': None,
            'first_partner_cp': u'No',
            'first_partner_hiv': u'not_sure',
            'first_relationship': u'Long-term partner',
            'first_sex_current': u'Yes',
            'first_sex_freq': None,
            'goods_exchange': u'No',
            'partner_hiv_test': u'not_sure',
            'past_year_sex_freq': u'About once a month',
            'report_datetime': datetime.today(),
            'sex_partner_community': u'N/A',
            'subject_visit_id': None,
            'third_last_sex': u'Days',
            'third_last_sex_calc': 7L,
            'user_modified': u''
        }

    def create_household_log_entry(self, household_structure):
        household_log = HouseholdLog.objects.filter(
            household_structure=household_structure).last()
        HouseholdLogEntry.objects.all().delete()
        HouseholdLogEntryFactory(household_log=household_log)

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-1',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_subject_recent_form_valid(self):
        """ For all participants who are above 64 at bhs are not eligible."""
        from bhp066.apps.bcpp_subject.forms.months_partner_form import MonthsRecentPartnerForm
        from bhp066.apps.bcpp_subject.forms.sexual_behaviour_form import SexualBehaviourForm
        sexual_behaviour_form = SexualBehaviourForm(data=self.sexual_data)
        sexual_behaviour_form.save()
        self.assertTrue(sexual_behaviour_form.is_valid())
        from bhp066.apps.bcpp_list.models import PartnerResidency
        parnter_residency = PartnerResidency.objects.create(
            name='In this community.')
        self.data['first_partner_live'] = [parnter_residency.id]
        recent_form = MonthsRecentPartnerForm(data=self.data)

        self.assertTrue(recent_form.is_valid())

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-1',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_subject_recent_form_valid1(self):
        """ For all participants who are above 64 at bhs are not eligible."""
        from bhp066.apps.bcpp_subject.forms.months_partner_form import MonthsRecentPartnerForm
        from bhp066.apps.bcpp_subject.forms.sexual_behaviour_form import SexualBehaviourForm
        sexual_behaviour_form = SexualBehaviourForm(data=self.sexual_data)
        sexual_behaviour_form.save()
        self.assertTrue(sexual_behaviour_form.is_valid())
        from bhp066.apps.bcpp_list.models import PartnerResidency
        parnter_residency = PartnerResidency.objects.create(
            name='In this community.')
        self.data['first_partner_live'] = [parnter_residency.id]
        self.data['concurrent'] = 'DWTA'
        recent_form = MonthsRecentPartnerForm(data=self.data)

        self.assertTrue(recent_form.is_valid())

    @override_settings(
        SITE_CODE='01',
        CURRENT_COMMUNITY='test_community',
        CURRENT_SURVEY='bcpp-year-1',
        CURRENT_COMMUNITY_CHECK=False,
        LIMIT_EDIT_TO_CURRENT_SURVEY=True,
        LIMIT_EDIT_TO_CURRENT_COMMUNITY=True,
        FILTERED_DEFAULT_SEARCH=True,
    )
    def test_subject_recent_form_not_valid(self):
        """ For all participants who are above 64 at bhs are not eligible."""
        from bhp066.apps.bcpp_subject.forms.months_partner_form import MonthsRecentPartnerForm
        from bhp066.apps.bcpp_subject.forms.sexual_behaviour_form import SexualBehaviourForm
        sexual_behaviour_form = SexualBehaviourForm(data=self.sexual_data)
        sexual_behaviour_form.save()
        print sexual_behaviour_form.errors
        self.assertTrue(sexual_behaviour_form.is_valid())
        from bhp066.apps.bcpp_list.models import PartnerResidency
        parnter_residency = PartnerResidency.objects.create(
            name='In this community.')
        self.data['concurrent'] = YES
        self.data['first_partner_live'] = [parnter_residency.id]
        recent_form = MonthsRecentPartnerForm(data=self.data)
        msg = "Please correct if you have sex with other partners"
        err_msg = u"You wrote that you have only one partner ever in sexual behavior form. {}".format(
            msg)
        #self.assertIn(u"sex", recent_form.errors.get("__all__"))
        self.assertFalse(recent_form.is_valid())