def test_generator_update_create_and_remove_index(): builder = IntentCaseBuilder() \ .create_index("case2", "host_type", "extension") \ .remove_index("parent_case_id", "parent_c") def _get_case(case_id, domain=None): assert case_id == "case2" return Mock(domain=TARGET_DOMAIN, type="host_type") with patch.object(CommCareCase.objects, 'get_case', new=_get_case): _test_payload_generator(intent_case=builder.get_case(), expected_indices={ "1": { "host": IndexAttrs("host_type", "case2", "extension"), "parent_c": IndexAttrs("parent_type", None, "child") }})
def test_case_block_index_omit_child(self): """ CaseBlock index relationship omit relationship attribute if set to "child" """ case_block = CaseBlock( case_id='123456', case_type='newborn', date_modified='2015-07-24', date_opened='2015-07-24', index={ 'parent': IndexAttrs(case_type='mother', case_id='789abc', relationship='child') }, ) self.assertEqual( ElementTree.tostring(case_block.as_xml()).decode('utf-8'), re.sub( r'(\n| {2,})', '', """ <case case_id="123456" date_modified="2015-07-24" xmlns="http://commcarehq.org/case/transaction/v2"> <update> <case_type>newborn</case_type> <date_opened>2015-07-24</date_opened> </update> <index> <parent case_type="mother">789abc</parent> </index> </case> """))
def get_case_block_for_indexed_case(mapping, external_data, parent_case_id, parent_case_type, default_owner_id): relationship = mapping.indexed_case_mapping.relationship case_block_kwargs = { "index": { mapping.indexed_case_mapping.identifier: IndexAttrs( parent_case_type, parent_case_id, relationship, ) }, "update": {} } for value_source in mapping.indexed_case_mapping.case_properties: value = value_source.get_import_value(external_data) if value_source.case_property in CASE_BLOCK_ARGS: case_block_kwargs[value_source.case_property] = value else: case_block_kwargs["update"][value_source.case_property] = value case_id = uuid.uuid4().hex case_type = mapping.indexed_case_mapping.case_type case_block_kwargs.setdefault("owner_id", default_owner_id) if not case_block_kwargs["owner_id"]: raise ConfigurationError( _(f'Unable to determine mobile worker to own new "{case_type}" ' f'{relationship} case or parent case "{parent_case_id}"')) case_block = CaseBlock(create=True, case_id=case_id, case_type=case_type, **case_block_kwargs) return case_block
def test_case_block_index_supports_relationship(self): """ CaseBlock index should allow the relationship to be set """ case_block = CaseBlock( case_id='abcdef', case_type='at_risk', date_modified='2015-07-24', date_opened='2015-07-24', index={ 'host': IndexAttrs(case_type='newborn', case_id='123456', relationship='extension') }, ) self.assertEqual( ElementTree.tostring(case_block.as_xml()).decode('utf-8'), re.sub( r'(\n| {2,})', '', """ <case case_id="abcdef" date_modified="2015-07-24" xmlns="http://commcarehq.org/case/transaction/v2"> <update> <case_type>at_risk</case_type> <date_opened>2015-07-24</date_opened> </update> <index> <host case_type="newborn" relationship="extension">123456</host> </index> </case> """))
def test_generator_create_case_with_index(): builder = IntentCaseBuilder().create_case("123").create_index("case2", "parent_type", "child") def _get_case(case_id, domain=None): assert case_id == "case2" return Mock(domain=TARGET_DOMAIN, type="parent_type") with patch.object(CommCareCase.objects, 'get_case', new=_get_case): _test_payload_generator( intent_case=builder.get_case(), registry_mock_cases={}, expected_creates={"1": {"case_type": "patient", "owner_id": "123"}}, expected_indices={"1": {"parent": IndexAttrs("parent_type", "case2", "child")}})
def setUpClass(cls): super().setUpClass() cls.user = create_user("admin", "123") CaseSearchConfig.objects.create(domain=cls.domain, enabled=True) household_1 = str(uuid.uuid4()) case_blocks = [ CaseBlock( case_id=household_1, case_type='household', case_name="Villanueva", create=True, ) ] case_blocks.extend([ CaseBlock( case_id=str(uuid.uuid4()), case_type='person', case_name=name, create=True, update=properties, index={ 'parent': IndexAttrs('household', household_id, 'child') } if household_id else None, ) for name, properties, household_id in [ ("Jane", { "family": "Villanueva" }, household_1), ("Xiomara", { "family": "Villanueva" }, household_1), ("Alba", { "family": "Villanueva" }, household_1), ("Rogelio", { "family": "de la Vega" }, household_1), ("Jane", { "family": "Ramos" }, None), ] ]) case_search_es_setup(cls.domain, case_blocks) cls.factory = AppFactory(domain=cls.domain) module, form = cls.factory.new_basic_module('person', 'person') module.search_config = CaseSearch( properties=[CaseSearchProperty(name='name')]) module.case_details.short.columns = [ DetailColumn(format='plain', field=field, header={'en': field}, model='person') for field in ['name', 'parent/name'] ]
def _mk_cases(cls): case_blocks = [] good_id = str(uuid.uuid4()) bad_id = str(uuid.uuid4()) for team_id, name in [(good_id, 'good_guys'), (bad_id, 'bad_guys')]: case_blocks.append( CaseBlock( case_id=team_id, case_type='team', case_name=name, external_id=name, owner_id='team_owner', create=True, )) date_opened = datetime.datetime(1878, 2, 17, 12) for external_id, name, properties, team_id in [ ('mattie', "Mattie Ross", {}, good_id), ('rooster', "Reuben Cogburn", { "alias": "Rooster" }, good_id), ('laboeuf', "LaBoeuf", { "alias": "" }, good_id), ('chaney', "Tom Chaney", { "alias": "The Coward" }, bad_id), ('ned', "Ned Pepper", { "alias": "Lucky Ned" }, bad_id), ]: case_blocks.append( CaseBlock( case_id=str(uuid.uuid4()), case_type='person', case_name=name, external_id=external_id, owner_id='person_owner', date_opened=date_opened, create=True, update=properties, index={'parent': IndexAttrs('team', team_id, 'child')})) date_opened += datetime.timedelta(days=1) case_blocks[-1].close = True # close Ned Pepper _, cases = submit_case_blocks([cb.as_text() for cb in case_blocks], domain=cls.domain) # preserve ordering so inserted_at date lines up right in ES order = {cb.external_id: index for index, cb in enumerate(case_blocks)} return sorted(cases, key=lambda case: order[case.external_id])
def test_case_block_index_valid_relationship(self): """ CaseBlock index relationship should only allow valid values """ with self.assertRaisesRegexp(CaseBlockError, 'Valid values for an index relationship are "child" and "extension"'): CaseBlock( case_id='abcdef', case_type='at_risk', date_modified='2015-07-24', index={ 'host': IndexAttrs(case_type='newborn', case_id='123456', relationship='parent') }, )
def setUpClass(cls): super().setUpClass() cls.domain_obj = create_domain(cls.domain) cls.case_accessor = CaseAccessors(cls.domain) cls.parent_case_id = str(uuid.uuid4()) case_id = str(uuid.uuid4()) xform, cases = submit_case_blocks([ CaseBlock(case_id=cls.parent_case_id, case_type='player', case_name='Elizabeth Harmon', external_id='1', owner_id='methuen_home', create=True, update={ 'sport': 'chess', 'rank': '1600', 'dob': '1948-11-02', }).as_text(), CaseBlock( case_id=case_id, case_type='match', case_name='Harmon/Luchenko', owner_id='harmon', external_id='14', create=True, update={ 'winner': 'Harmon', 'accuracy': '84.3', }, index={ 'parent': IndexAttrs(case_type='player', case_id=cls.parent_case_id, relationship='child') }, ).as_text() ], domain=cls.domain) cls.parent_case = cls.case_accessor.get_case(cls.parent_case_id) cls.case = cls.case_accessor.get_case(case_id) for case in [cls.case, cls.parent_case]: # Patch datetimes for test consistency case.opened_on = datetime(2021, 2, 18, 10, 59) case.modified_on = datetime(2021, 2, 18, 10, 59) case.server_modified_on = datetime(2021, 2, 18, 10, 59)
def parent_and_child_cases(parent_name, child_name): parent_id = str(uuid.uuid4()) return [ CaseBlock( case_id=parent_id, case_type='creator', case_name=parent_name, create=True, ), CaseBlock( case_id=str(uuid.uuid4()), case_type='creative_work', case_name=child_name, create=True, index={'parent': IndexAttrs('creator', parent_id, 'child')}, ) ]
def _get_case_blocks(): case_blocks = [] for team_id, name in [(GOOD_GUYS_ID, 'good_guys'), (BAD_GUYS_ID, 'bad_guys')]: case_blocks.append( CaseBlock( case_id=team_id, case_type='team', case_name=name, external_id=name, owner_id='team_owner', create=True, )) date_opened = datetime.datetime(1878, 2, 17, 12) for external_id, name, properties, team_id in [ ('mattie', "Mattie Ross", {}, GOOD_GUYS_ID), ('rooster', "Reuben Cogburn", { "alias": "Rooster" }, GOOD_GUYS_ID), ('laboeuf', "LaBoeuf", { "alias": "" }, GOOD_GUYS_ID), ('chaney', "Tom Chaney", { "alias": "The Coward" }, BAD_GUYS_ID), ('ned', "Ned Pepper", { "alias": "Lucky Ned" }, BAD_GUYS_ID), ]: case_blocks.append( CaseBlock( case_id=str(uuid.uuid4()), case_type='person', case_name=name, external_id=external_id, owner_id='person_owner', date_opened=date_opened, create=True, update=properties, index={'parent': IndexAttrs('team', team_id, 'child')})) date_opened += datetime.timedelta(days=1) case_blocks[-1].close = True # close Ned Pepper return case_blocks
def get_caseblock(self): def _if_specified(value): return value if value is not None else CaseBlock.undefined return CaseBlock( case_id=self.get_case_id(), user_id=self.user_id, case_type=_if_specified(self.case_type), case_name=_if_specified(self.case_name), external_id=_if_specified(self.external_id), owner_id=_if_specified(self.owner_id), create=self._is_case_creation, update=dict(self.properties), index={ name: IndexAttrs(index.case_type, index.case_id, index.relationship) for name, index in self.indices.items() }, ).as_text()
def test_get_first_claims_index_not_host(self): # create a claim case with the incorrect index identifier # method still find the case and recognise it as a claim case_block = CaseBlock(create=True, case_id=uuid4().hex, case_name="claim", case_type=CLAIM_CASE_TYPE, owner_id=self.user.user_id, index={ "not_host": IndexAttrs( case_type=self.host_case_type, case_id=self.host_case_id, relationship=CASE_INDEX_EXTENSION, ) }).as_xml() post_case_blocks([case_block], {'domain': DOMAIN}) first_claim = get_first_claims(DOMAIN, self.user.user_id, [self.host_case_id]) self.assertEqual(first_claim, {self.host_case_id})
def test_generator_create_case_with_index_to_another_case_being_created(): create_parent = IntentCaseBuilder()\ .target_case(case_id="1")\ .create_case(owner_id="123", case_type="patient") create_child = ( IntentCaseBuilder() .target_case(case_id="sub1") .create_case(owner_id="123", case_type="child") .create_index(case_id="1", case_type="patient") .get_case() ) create_parent.set_subcases([create_child]) _test_payload_generator( intent_case=create_parent.get_case(), registry_mock_cases={}, expected_creates={ "1": {"case_type": "patient", "owner_id": "123"}, "sub1": {"case_type": "child", "owner_id": "123"} }, expected_indices={"sub1": {"parent": IndexAttrs("patient", "1", "child")}})
def test_default_relationship(self): parent_case_id = uuid.uuid4().hex post_case_blocks([ CaseBlock.deprecated_init(create=True, case_id=parent_case_id, user_id=self.user.user_id).as_xml() ], domain=self.project.name) create_index = CaseBlock.deprecated_init( create=True, case_id=self.CASE_ID, user_id=self.user.user_id, owner_id=self.user.user_id, ) # set outside constructor to skip validation create_index.index = { 'parent': IndexAttrs(case_type='parent', case_id=parent_case_id, relationship='') } form, cases = post_case_blocks([create_index.as_xml()], domain=self.project.name) self.assertEqual(cases[0].indices[0].relationship, 'child')
claim_id = uuid4().hex if not (host_type and host_name): case = CaseAccessors(domain).get_case(host_id) host_type = case.type host_name = case.name identifier = DEFAULT_CASE_INDEX_IDENTIFIERS[CASE_INDEX_EXTENSION] claim_case_block = CaseBlock( create=True, case_id=claim_id, case_name=host_name, case_type=CLAIM_CASE_TYPE, owner_id=owner_id, index={ identifier: IndexAttrs( case_type=host_type, case_id=host_id, relationship=CASE_INDEX_EXTENSION, ) } ).as_xml() post_case_blocks([claim_case_block], {'domain': domain}, device_id=device_id) return claim_id def get_first_claim(domain, user_id, case_id): """ Returns the first claim by user_id of case_id, or None """ case = CaseAccessors(domain).get_case(case_id) identifier = DEFAULT_CASE_INDEX_IDENTIFIERS[CASE_INDEX_EXTENSION] try:
def test_generator_update_remove_index_extension(): builder = IntentCaseBuilder().remove_index("host_case_id", "host_c") _test_payload_generator(intent_case=builder.get_case(), expected_indices={ "1": {"host_c": IndexAttrs("host_type", None, "extension")}})