Example #1
0
    def setUp(self):
        now = datetime.utcnow()
        case_kwargs = {
            'owner_id': uuid4().hex,
            'modified_on': now,
            'server_modified_on': now,
        }
        self.host_case_id = uuid4().hex
        case = CommCareCaseSQL(
            case_id=self.host_case_id,
            domain=self.domain,
            type='person',
            name='Ted',
            **case_kwargs,
        )
        self.host_case = create_case(case)

        case = CommCareCaseSQL(
            case_id=uuid4().hex,
            domain=self.domain,
            type='person_name',
            name='Theodore',
            case_json={
                'given_names': 'Theodore John',
                'family_name': 'Kaczynski',
            },
            **case_kwargs,
        )
        index = CommCareCaseIndexSQL(
            domain=self.domain,
            identifier='host',
            referenced_type='person',
            referenced_id=self.host_case_id,
            relationship_id=CommCareCaseIndexSQL.EXTENSION,
        )
        self.ext_case_1 = create_case_with_index(case, index)

        case = CommCareCaseSQL(
            case_id=uuid4().hex,
            domain=self.domain,
            type='person_name',
            name='Unabomber',
            case_json={
                'given_names': 'Unabomber',
            },
            **case_kwargs,
        )
        index = CommCareCaseIndexSQL(
            domain=self.domain,
            identifier='host',
            referenced_type='person',
            referenced_id=self.host_case_id,
            relationship_id=CommCareCaseIndexSQL.EXTENSION,
        )
        self.ext_case_2 = create_case_with_index(case, index)
Example #2
0
    def setUp(self):
        now = datetime.utcnow()
        yesterday = now - timedelta(days=1)
        owner_id = uuid4().hex
        self.parent_case_id = uuid4().hex
        case = CommCareCaseSQL(
            case_id=self.parent_case_id,
            domain=self.domain,
            type='person',
            name='Joe',
            owner_id=owner_id,
            modified_on=yesterday,
            server_modified_on=yesterday,
        )
        self.parent_case = create_case(case)

        case = CommCareCaseSQL(
            case_id=uuid4().hex,
            domain=self.domain,
            type='temperature',
            case_json={
                'value': '36.2',
            },
            owner_id=owner_id,
            modified_on=yesterday,
            server_modified_on=yesterday,
        )
        index = CommCareCaseIndexSQL(
            domain=self.domain,
            identifier='parent',
            referenced_type='person',
            referenced_id=self.parent_case_id,
            relationship_id=CommCareCaseIndexSQL.CHILD,
        )
        self.child_case_1 = create_case_with_index(case, index)

        case = CommCareCaseSQL(
            case_id=uuid4().hex,
            domain=self.domain,
            type='temperature',
            case_json={
                'value': '36.6',
            },
            owner_id=owner_id,
            modified_on=now,
            server_modified_on=now,
        )
        index = CommCareCaseIndexSQL(
            domain=self.domain,
            identifier='parent',
            referenced_type='person',
            referenced_id=self.parent_case_id,
            relationship_id=CommCareCaseIndexSQL.CHILD,
        )
        self.child_case_2 = create_case_with_index(case, index)
Example #3
0
    def _apply_index_action(self, action):
        if not action.indices:
            return

        for index_update in action.indices:
            if self.case.has_index(index_update.identifier):
                if not index_update.referenced_id:
                    # empty ID = delete
                    index = self.case.get_index(index_update.identifier)
                    self.case.track_delete(index)
                else:
                    # update
                    index = self.case.get_index(index_update.identifier)
                    index.referenced_type = index_update.referenced_type
                    index.referenced_id = index_update.referenced_id
                    index.relationship = index_update.relationship
                    self.case.track_update(index)
            else:
                # no id, no index
                if index_update.referenced_id:
                    index = CommCareCaseIndexSQL(
                        domain=self.case.domain,
                        case=self.case,
                        identifier=index_update.identifier,
                        referenced_type=index_update.referenced_type,
                        referenced_id=index_update.referenced_id,
                        relationship=index_update.relationship
                    )
                    self.case.track_create(index)
Example #4
0
 def test_patch_missing_case_index(self):
     self.submit_form(make_test_form("form-1", case_id="case-1"))
     self.do_migration(case_diff="none")
     CommCareCaseIndexSQL(
         domain=self.domain_name,
         case_id="case-1",
         identifier="parent",
         referenced_id="a53346d5",
         referenced_type="household",
         relationship_id=CommCareCaseIndexSQL.CHILD,
     ).save()
     with self.diff_without_rebuild():
         self.do_case_diffs()
     index = {
         "case_id": "case-1",
         "identifier": "parent",
         "referenced_id": "a53346d5",
         "referenced_type": "household",
         "relationship": "child",
     }
     self.compare_diffs([
         Diff('case-1',
              'missing', ['indices', '[*]'],
              old=MISSING,
              new=index),
     ])
     with self.diff_without_rebuild():
         self.do_case_patch()
     self.compare_diffs()
     self.assert_patched_cases(["case-1"])