def test_no_owner(self): """ get_case_location should return None when the case has no owner """ form, (case, ) = _create_case(domain=self.domain, case_id=uuid.uuid4().hex, owner_id=None) location = get_case_location(case) self.assertIsNone(location)
def test_owner_has_no_locations(self): """ get_case_location should return None when owner has no location """ self.owner = CommCareUser.create(self.domain, 'no_location', '***') form, (case, ) = _create_case(domain=self.domain, case_id=uuid.uuid4().hex, owner_id=self.owner.get_id) location = get_case_location(case) self.assertIsNone(location)
def test_owner_is_location(self): """ get_case_location should return case owner when owner is a location """ joburg = self.locations['Johannesburg'] form, (case, ) = _create_case(domain=self.domain, case_id=uuid.uuid4().hex, owner_id=joburg.location_id) location = get_case_location(case) self.assertEqual(location, joburg)
def test_get_case_location_ancestor_repeaters_none(self): """ get_case_location_ancestor_repeaters should not return repeaters if there are none at ancestor locations """ gardens = self.locations['Gardens'] form, (case, ) = _create_case(domain=self.domain, case_id=uuid.uuid4().hex, owner_id=gardens.location_id) repeaters = get_case_location_ancestor_repeaters(case) self.assertEqual(repeaters, [])
def test_allowed_to_forward(self): """ If all criteria pass, the payload should be allowed to forward :return: """ case_id = uuid.uuid4().hex form_payload, cases = _create_case(domain=DOMAIN, case_id=case_id, owner_id=self.owner.get_id) repeater = OpenmrsRepeater() self.assertTrue(repeater.allowed_to_forward(form_payload))
def test_owner_has_primary_location(self): """ get_case_location should return case owner's location when owner is a mobile worker """ gardens = self.locations['Gardens'] self.owner = CommCareUser.create(self.domain, 'gardens_user', '***', location=gardens) form, (case, ) = _create_case(domain=self.domain, case_id=uuid.uuid4().hex, owner_id=self.owner.get_id) location = get_case_location(case) self.assertEqual(location, gardens)
def test_openmrs_location_uuid_set(self): """ get_openmrs_location_uuid should return the OpenMRS location UUID that corresponds to a case's location """ openmrs_capetown_uuid = '50017a7f-296d-4ab9-8d3a-b9498bcbf385' cape_town = self.locations['Cape Town'] cape_town.metadata[LOCATION_OPENMRS_UUID] = openmrs_capetown_uuid with mock.patch( 'corehq.apps.locations.document_store.publish_location_saved', mock.Mock()): cape_town.save() case_id = uuid.uuid4().hex form, (case, ) = _create_case(domain=self.domain, case_id=case_id, owner_id=cape_town.location_id) self.assertEqual(get_openmrs_location_uuid(self.domain, case_id), openmrs_capetown_uuid)
def test_get_case_location_ancestor_repeaters_same(self): """ get_case_location_ancestor_repeaters should return the repeater at the same location as the case """ gardens = self.locations['Gardens'] form, (case, ) = _create_case(domain=self.domain, case_id=uuid.uuid4().hex, owner_id=gardens.location_id) gardens_repeater = OpenmrsRepeater.wrap({ 'doc_type': 'OpenmrsRepeater', 'domain': self.domain, 'location_id': gardens.location_id, }) gardens_repeater.save() repeaters = get_case_location_ancestor_repeaters(case) self.assertEqual(repeaters, [gardens_repeater])
def test_excluded_case_type(self): """ If the repeater has white-listed case types, excluded case types should not be forwarded """ case_id = uuid.uuid4().hex form_payload, cases = _create_case( domain=DOMAIN, case_id=case_id, case_type='notpatient', owner_id=self.owner.get_id ) repeater = OpenmrsRepeater() repeater.white_listed_case_types = ['patient'] self.assertFalse(repeater.allowed_to_forward(form_payload))
def test_openmrs_location_uuid_set(self): """ get_ancestor_location_openmrs_uuid should return the OpenMRS location UUID that corresponds to a case's location """ cape_town = self.locations['Cape Town'] case_id = uuid.uuid4().hex form, (case, ) = _create_case(domain=self.domain, case_id=case_id, owner_id=cape_town.location_id) self.assertEqual( get_ancestor_location_openmrs_uuid(self.domain, case_id), self.openmrs_capetown_uuid )
def test_openmrs_location_uuid_none(self): """ get_ancestor_location_openmrs_uuid should return None if a case's location and its ancestors do not have an OpenMRS location UUID """ joburg = self.locations['Johannesburg'] self.assertIsNone(joburg.metadata.get(LOCATION_OPENMRS_UUID)) case_id = uuid.uuid4().hex form, (case, ) = _create_case(domain=self.domain, case_id=case_id, owner_id=joburg.location_id) self.assertIsNone(get_ancestor_location_openmrs_uuid(self.domain, case_id))
def test_openmrs_location_uuid_ancestor(self): """ get_ancestor_location_openmrs_uuid should return the OpenMRS location UUID that corresponds to a case's location's ancestor """ gardens = self.locations['Gardens'] self.assertIsNone(gardens.metadata.get(LOCATION_OPENMRS_UUID)) case_id = uuid.uuid4().hex form, (case, ) = _create_case(domain=self.domain, case_id=case_id, owner_id=gardens.location_id) self.assertEqual( get_ancestor_location_openmrs_uuid(self.domain, case_id), self.openmrs_capetown_uuid )
def test_get_case_location_ancestor_repeaters_multi(self): """ get_case_location_ancestor_repeaters should return the repeater at the closest ancestor location """ form, (gardens_case, ) = _create_case( domain=self.domain, case_id=uuid.uuid4().hex, owner_id=self.locations['Gardens'].location_id ) cape_town_repeater = OpenmrsRepeater.wrap({ 'doc_type': 'OpenmrsRepeater', 'domain': self.domain, 'location_id': self.locations['Cape Town'].location_id, }) cape_town_repeater.save() western_cape_repeater = OpenmrsRepeater.wrap({ 'doc_type': 'OpenmrsRepeater', 'domain': self.domain, 'location_id': self.locations['Western Cape'].location_id, }) western_cape_repeater.save() repeaters = get_case_location_ancestor_repeaters(gardens_case) self.assertEqual(repeaters, [cape_town_repeater])