@contextmanager
def mock_fetch_case_attachment(case_id, attachments):

    class MockCachedObject(object):
        def __init__(self, attachment_file):
            self.attachment_file = attachment_file

        def get(self, **kwargs):
            return None, open(self.attachment_file)

    def get_cached_case_attachment(domain, _case_id, attachment_id):
        if case_id == _case_id and attachment_id in attachments:
            return MockCachedObject(attachments[attachment_id])
        else:
            raise ResourceNotFound()

    with patch('corehq.apps.hqcase.utils.get_cached_case_attachment', get_cached_case_attachment):
        yield
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/hqcase/tests/test_explode_cases.py/mock_fetch_case_attachment/original