def assert_person_case_is_under_n_years_old(self, n_years, criteria_name): rule = _create_empty_rule(self.domain, case_type='person') rule.add_criteria(CustomMatchDefinition, name=criteria_name) with _with_case(self.domain, 'person', datetime.utcnow()) as case: dob = datetime(2018, 2, 22, 12, 0) # No value for dob yet self.assertFalse(rule.criteria_match(case, dob)) # Bad value for dob case = self._set_dob(case, 'x') self.assertFalse(rule.criteria_match(case, dob)) # Set dob case = self._set_dob(case, '2018-02-22') for assert_, age in [ # Test over N years old (self.assertFalse, relativedelta(years=n_years)), (self.assertFalse, relativedelta(years=n_years, days=90)), # Test under N years old (self.assertTrue, relativedelta(years=n_years, days=-1)), (self.assertTrue, relativedelta(days=90)), ]: assert_(rule.criteria_match(case, dob + age), age) # Test wrong case type rule = _create_empty_rule(self.domain, case_type='x') rule.add_criteria(CustomMatchDefinition, name=criteria_name) with _with_case(self.domain, 'x', datetime.utcnow()) as case: case = self._set_dob(case, '2018-02-22') self.assertFalse( rule.criteria_match(case, dob + relativedelta(days=90)))
def test_child_health_case_that_is_availing_services(self): rule = _create_empty_rule(self.domain, case_type='child_health') rule.add_criteria( CustomMatchDefinition, name='ICDS_CHILD_HEALTH_CASE_CHILD_AVAILING_SERVICES') with _with_case(self.domain, 'person', datetime.utcnow()) as child: with _with_case(self.domain, 'child_health', datetime.utcnow()) as child_health: set_parent_case(self.domain, child_health, child, relationship='extension') self._set_case_props(child, {"registered_status": "registered"}) self._set_case_props(child, {"migration_status": "not_migrated"}) self.assertTrue( rule.criteria_match(child_health, datetime.utcnow())) with _with_case(self.domain, 'person', datetime.utcnow()) as child: with _with_case(self.domain, 'child_health', datetime.utcnow()) as child_health: self._set_case_props(child, {"registered_status": "not_registered"}) self._set_case_props(child, {"migration_status": "not_migrated"}) set_parent_case(self.domain, child_health, child, relationship='extension') self.assertFalse( rule.criteria_match(child_health, datetime.utcnow()))
def test_ccs_record_mother_case_availing_services_has_phone_number(self): with _with_case(self.domain, 'person', datetime.utcnow()) as mother: with _with_case(self.domain, 'ccs_record', datetime.utcnow()) as ccs_record: set_parent_case(self.domain, ccs_record, mother, identifier='parent') self.assertFalse( ccs_record_mother_case_availing_services_has_phone_number( ccs_record, datetime.utcnow())) # refresh case to avoid cache fetch for parent ccs_record = CaseAccessors(self.domain).get_case( ccs_record.case_id) self._set_case_props(mother, {'contact_phone_number': '9999999999'}) self.assertTrue( ccs_record_mother_case_availing_services_has_phone_number( ccs_record, datetime.utcnow())) # refresh case to avoid cache fetch for parent ccs_record = CaseAccessors(self.domain).get_case( ccs_record.case_id) self._set_case_props(mother, {'migration_status': 'migrated'}) self.assertFalse( ccs_record_mother_case_availing_services_has_phone_number( ccs_record, datetime.utcnow()))
def test_child_health_case_that_is_availing_services(self): with _with_case(self.domain, 'person', datetime.utcnow()) as child: with _with_case(self.domain, 'child_health', datetime.utcnow()) as child_health: set_parent_case(self.domain, child_health, child, relationship='extension') self._set_case_props(child, {"registered_status": "registered"}) self._set_case_props(child, {"migration_status": "not_migrated"}) self.assertTrue( child_health_case_is_availing_services( child_health, datetime.utcnow())) with _with_case(self.domain, 'person', datetime.utcnow()) as child: with _with_case(self.domain, 'child_health', datetime.utcnow()) as child_health: self._set_case_props(child, {"registered_status": "not_registered"}) self._set_case_props(child, {"migration_status": "not_migrated"}) set_parent_case(self.domain, child_health, child, relationship='extension') self.assertFalse( child_health_case_is_availing_services( child_health, datetime.utcnow()))
def assert_person_case_is_under_n_years_old(self, n_years, criteria): with _with_case(self.domain, 'person', datetime.utcnow()) as case: dob = datetime(2018, 2, 22, 12, 0) # No value for dob yet self.assertFalse(criteria(case, dob)) # Bad value for dob case = self._set_dob(case, 'x') self.assertFalse(criteria(case, dob)) # Set dob case = self._set_dob(case, '2018-02-22') for assert_, age in [ # Test over N years old (self.assertFalse, relativedelta(years=n_years)), (self.assertFalse, relativedelta(years=n_years, days=90)), # Test under N years old (self.assertTrue, relativedelta(years=n_years, days=-1)), (self.assertTrue, relativedelta(days=90)), ]: assert_(criteria(case, dob + age), age) # Test wrong case type with _with_case(self.domain, 'x', datetime.utcnow()) as case: case = self._set_dob(case, '2018-02-22') self.assertFalse(criteria(case, dob + relativedelta(days=90)))
def test_is_usercase_of_ls(self): rule = _create_empty_rule(self.domain, case_type=USERCASE_TYPE) rule.add_criteria(CustomMatchDefinition, name='ICDS_IS_USERCASE_OF_LS') with _with_case(self.domain, USERCASE_TYPE, datetime.utcnow(), owner_id=self.aww.get_id) as aww_uc,\ _with_case(self.domain, USERCASE_TYPE, datetime.utcnow(), owner_id=self.ls.get_id) as ls_uc: self.assertFalse(rule.criteria_match(aww_uc, datetime.utcnow())) self.assertTrue(rule.criteria_match(ls_uc, datetime.utcnow()))
def test_person_case_is_under_6_years_old(self): rule = _create_empty_rule(self.domain, case_type='person') rule.add_criteria(CustomMatchDefinition, name='ICDS_PERSON_CASE_IS_UNDER_6_YEARS_OLD') with _with_case(self.domain, 'person', datetime.utcnow()) as case: # No value for dob yet self.assertFalse( rule.criteria_match(case, datetime(2018, 2, 22, 12, 0))) # Bad value for dob case = self._set_dob(case, 'x') self.assertFalse( rule.criteria_match(case, datetime(2018, 2, 22, 12, 0))) # Set dob case = self._set_dob(case, '2018-02-22') # Test over 6 years old self.assertFalse( rule.criteria_match(case, datetime(2024, 2, 22, 12, 0))) self.assertFalse( rule.criteria_match(case, datetime(2024, 5, 22, 12, 0))) # Test under 6 years old self.assertTrue( rule.criteria_match(case, datetime(2024, 2, 21, 12, 0))) self.assertTrue( rule.criteria_match(case, datetime(2018, 5, 22, 12, 0))) # Test wrong case type rule = _create_empty_rule(self.domain, case_type='x') rule.add_criteria(CustomMatchDefinition, name='ICDS_PERSON_CASE_IS_UNDER_6_YEARS_OLD') with _with_case(self.domain, 'x', datetime.utcnow()) as case: case = self._set_dob(case, '2018-02-22') self.assertFalse( rule.criteria_match(case, datetime(2018, 5, 22, 12, 0)))
rule = _create_empty_rule(self.domain, case_type='ccs_record') rule.add_criteria(CustomMatchDefinition, name='ICDS_CCS_RECORD_CASE_HAS_FUTURE_EDD') def check(case, edd, match): case = self._set_case_props(case, {"edd": edd}) (self.assertTrue if match else self.assertFalse)( rule.criteria_match(case, now), "%s case with edd=%s should%s match" % ( case.type, edd, "" if match else " not", )) now = datetime(2018, 2, 22, 12, 0) with _with_case(self.domain, 'ccs_record', datetime.utcnow()) as case: for match, edd in [ (False, None), # edd not set (False, '2018-01-22'), # past (False, '2018-02-22'), # past (True, '2018-02-23'), # future (True, '2018-03-22'), # future ]: check(case, edd, match) # rule should not match person case with _with_case(self.domain, 'person', datetime.utcnow()) as person: check(person, '2018-03-22', False) def test_is_usercase_of_aww(self): rule = _create_empty_rule(self.domain, case_type=USERCASE_TYPE)