@run_with_all_backends
    def testOtherUserReassignsIndexed(self):
        # create a parent and child case (with index) from one user
        parent_id = "other_reassigns_index_parent"
        case_id = "other_reassigns_index_child"
        self.factory.create_or_update_cases([
            CaseStructure(
                case_id=case_id,
                attrs={'create': True},
                indices=[CaseIndex(
                    CaseStructure(case_id=parent_id, attrs={'create': True}),
                    relationship=CHILD_RELATIONSHIP,
                    related_type=PARENT_TYPE,
                )],
            )
        ])

        # assign the parent case away from the same user
        parent_update = CaseBlock(
            create=False,
            case_id=parent_id,
            user_id=USER_ID,
            owner_id=OTHER_USER_ID,
            update={"greeting": "hello"}).as_xml()
        self._postFakeWithSyncToken(parent_update, self.sync_log.get_id)

        # sync cases to second user
        other_sync_log = synclog_from_restore_payload(
            generate_restore_payload(self.project, self.other_user)
        )

        # change the child's owner from another user
        child_reassignment = CaseBlock(
            create=False,
            case_id=case_id,
            user_id=OTHER_USER_ID,
            owner_id=OTHER_USER_ID,
            update={"childgreeting": "hi!"},
        ).as_xml()
        self._postFakeWithSyncToken(child_reassignment, other_sync_log.get_id)

        # also change the parent from the second user
        other_parent_update = CaseBlock(
            create=False,
            case_id=parent_id,
            user_id=OTHER_USER_ID,
            owner_id=OTHER_USER_ID,
            update={"other_greeting": "something new"}).as_xml()
        self._postFakeWithSyncToken(other_parent_update, other_sync_log.get_id)

        # original user syncs again
        latest_sync_log = SyncLog.last_for_user(self.user.user_id)

        # at this point both cases are assigned to the other user so the original user
        # should not have them. however, the first sync should send them down (with new ownership)
        # so that they can be purged.
        assert_user_has_case(self, self.user, case_id, restore_id=latest_sync_log.get_id)
        assert_user_has_case(self, self.user, parent_id, restore_id=latest_sync_log.get_id)

        # Ghetto
        payload = generate_restore_payload(self.project, self.user, latest_sync_log.get_id, version=V2)
        self.assertTrue("something new" in payload)
        self.assertTrue("hi!" in payload)
        # also check that the latest sync log knows those cases are no longer relevant to the phone
        log = synclog_from_restore_payload(payload)
        self.assertFalse(log.phone_is_holding_case(case_id))
        self.assertFalse(log.phone_is_holding_case(parent_id))

        # change the parent again from the second user
        other_parent_update = CaseBlock(
            create=False,
            case_id=parent_id,
            user_id=OTHER_USER_ID,
            owner_id=OTHER_USER_ID,
            update={"other_greeting": "something different"}).as_xml()
        self._postFakeWithSyncToken(other_parent_update, other_sync_log.get_id)

        # original user syncs again
        latest_sync_log = SyncLog.last_for_user(self.user.user_id)
        # should be no changes
        assert_user_doesnt_have_case(self, self.user, case_id, restore_id=latest_sync_log.get_id)
        assert_user_doesnt_have_case(self, self.user, parent_id, restore_id=latest_sync_log.get_id)

        # change the child again from the second user
        other_child_update = CaseBlock(
            create=False,
            case_id=case_id,
            user_id=OTHER_USER_ID,
            owner_id=OTHER_USER_ID,
            update={"childgreeting": "hi changed!"},
        ).as_xml()
        self._postFakeWithSyncToken(other_child_update, other_sync_log.get_id)

        # original user syncs again
        latest_sync_log = SyncLog.last_for_user(self.user.user_id)
        # should be no changes
        assert_user_doesnt_have_case(self, self.user, case_id, restore_id=latest_sync_log.get_id)
        assert_user_doesnt_have_case(self, self.user, parent_id, restore_id=latest_sync_log.get_id)

        # change owner of child back to orginal user from second user
        child_reassignment = CaseBlock(
            create=False,
            case_id=case_id,
            user_id=OTHER_USER_ID,
            owner_id=USER_ID,
        ).as_xml()
        self._postFakeWithSyncToken(child_reassignment, other_sync_log.get_id)

        # original user syncs again
        latest_sync_log = SyncLog.last_for_user(self.user.user_id)
        # both cases should now sync
        assert_user_has_case(self, self.user, case_id, restore_id=latest_sync_log.get_id)
        assert_user_has_case(self, self.user, parent_id, restore_id=latest_sync_log.get_id)

        # ghetto
        payload = generate_restore_payload(
            self.project, self.user, latest_sync_log.get_id, version=V2
        )
        self.assertTrue("something different" in payload)
        self.assertTrue(payload in "hi changed!")
dataset/ETHPy150Open dimagi/commcare-hq/corehq/ex-submodules/casexml/apps/phone/tests/test_sync_mode.py/MultiUserSyncTest.testOtherUserReassignsIndexed/swapped operands of `in`