def setUpClass(cls): super(DbaccessorsTest, cls).setUpClass() cls.domain_obj = create_domain('test-case-importer-dbaccessors') cls.domain = cls.domain_obj.name cls.user = WebUser.create(cls.domain, 'username1', 'password', None, None) cls.meta1 = CaseUploadFileMeta( identifier="123", filename="one.xlsx", length=1000, ) cls.meta1.save() cls.case_upload_1 = CaseUploadRecord( upload_id=UUID('7ca20e75-8ba3-4d0d-9c9c-66371e8895dc'), task_id=UUID('a2ebc913-11e6-4b6b-b909-355a863e0682'), domain=cls.domain, comment='This is the first upload', upload_file_meta=cls.meta1, ) cls.meta2 = CaseUploadFileMeta( identifier="234", filename="two.xlsx", length=1500, ) cls.meta2.save() cls.case_upload_2 = CaseUploadRecord( upload_id=UUID('63d07615-7f89-458e-863d-5f9b5f4f4b7b'), task_id=UUID('fe47f168-0632-40d9-b01a-79612e98298b'), domain=cls.domain, comment='This is the second upload', upload_file_meta=cls.meta2, ) cls.case_upload_1.save() cls.case_upload_2.save()
def testImportFileMissing(self, update_state): # by using a made up upload_id, we ensure it's not referencing any real file case_upload = CaseUploadRecord(upload_id=str(uuid.uuid4()), task_id=str(uuid.uuid4())) case_upload.save() res = bulk_import_async.delay(self._config(['anything']), self.domain, case_upload.upload_id) self.assertIsInstance(res.result, Ignore) update_state.assert_called_with( state=states.FAILURE, meta=get_interned_exception('Sorry, your session has expired. Please start over and try again.')) self.assertEqual(0, len(get_case_ids_in_domain(self.domain)))
def setUpClass(cls): super(DbaccessorsTest, cls).setUpClass() cls.case_upload_1 = CaseUploadRecord( upload_id=UUID('7ca20e75-8ba3-4d0d-9c9c-66371e8895dc'), task_id=UUID('a2ebc913-11e6-4b6b-b909-355a863e0682'), domain=cls.domain, ) cls.case_upload_2 = CaseUploadRecord( upload_id=UUID('63d07615-7f89-458e-863d-5f9b5f4f4b7b'), task_id=UUID('fe47f168-0632-40d9-b01a-79612e98298b'), domain=cls.domain, ) cls.case_upload_1.save() cls.case_upload_2.save()
def setUpClass(cls): super(DbaccessorsTest, cls).setUpClass() cls.domain_obj = create_domain('test-case-importer-dbaccessors') cls.domain = cls.domain_obj.name cls.user = WebUser.create(cls.domain, 'username', 'password') cls.case_upload_1 = CaseUploadRecord( upload_id=UUID('7ca20e75-8ba3-4d0d-9c9c-66371e8895dc'), task_id=UUID('a2ebc913-11e6-4b6b-b909-355a863e0682'), domain=cls.domain, ) cls.case_upload_2 = CaseUploadRecord( upload_id=UUID('63d07615-7f89-458e-863d-5f9b5f4f4b7b'), task_id=UUID('fe47f168-0632-40d9-b01a-79612e98298b'), domain=cls.domain, ) cls.case_upload_1.save() cls.case_upload_2.save()
def testImportFileMissing(self, update_state): # by using a made up upload_id, we ensure it's not referencing any real file case_upload = CaseUploadRecord(upload_id=str(uuid.uuid4()), task_id=str(uuid.uuid4())) case_upload.save() res = bulk_import_async.delay( self._config(['anything']).to_json(), self.domain, case_upload.upload_id) self.assertIsInstance(res.result, Ignore) update_state.assert_called_with( state=states.FAILURE, meta=get_interned_exception( 'There was an unexpected error retrieving the file you uploaded. ' 'Please try again and contact support if the problem persists.' )) self.assertEqual( 0, len(CommCareCase.objects.get_case_ids_in_domain(self.domain)))
def _import_rows(self, rows, get_by_user_id): get_by_user_id.return_value = WebUser( _id=self.couch_user_id, domain=self.domain, username='******') case_upload_record = CaseUploadRecord( upload_id=uuid.uuid4(), task_id=uuid.uuid4(), domain=self.domain, ) case_upload_record.save() self.addCleanup(case_upload_record.delete) tracker = CaseUpload(case_upload_record.upload_id) # mock internals to have record_cases use our case_upload_record tracker.__dict__['_case_upload_record'] = case_upload_record config = self._get_config(rows[0]) xls_file = make_worksheet_wrapper(*rows) do_import(xls_file, config, self.domain, record_form_callback=tracker.record_form) return case_upload_record
def _import_rows(self, rows, get_by_user_id): get_by_user_id.return_value = WebUser( _id=self.couch_user_id, domain=self.domain, username='******') case_upload_record = CaseUploadRecord( upload_id=uuid.uuid4(), task_id=uuid.uuid4(), domain=self.domain, ) case_upload_record.save() self.addCleanup(case_upload_record.delete) tracker = CaseUpload(case_upload_record.upload_id) # mock internals to have record_cases use our case_upload_record tracker.__dict__['_case_upload_record'] = case_upload_record config = self._get_config(rows[0]) xls_file = make_worksheet_wrapper(*rows) do_import(xls_file, config, self.domain, record_form_callback=tracker.record_form) return case_upload_record
def trigger_upload(self, domain, config): from corehq.apps.case_importer.tasks import bulk_import_async task = bulk_import_async.delay(config, domain, self.upload_id) original_filename = transient_file_store.get_filename(self.upload_id) with open(self.get_tempfile()) as f: case_upload_file_meta = persistent_file_store.write_file(f, original_filename) CaseUploadRecord( domain=domain, upload_id=self.upload_id, task_id=task.task_id, couch_user_id=config.couch_user_id, case_type=config.case_type, upload_file_meta=case_upload_file_meta, ).save()
def trigger_upload(self, domain, config, comment=None): """ Save a CaseUploadRecord and trigger a task that runs the upload The task triggered by this must call case_upload.wait_for_case_upload_record() before using it to avoid a race condition. """ from corehq.apps.case_importer.tasks import bulk_import_async original_filename = transient_file_store.get_filename(self.upload_id) with open(self.get_tempfile(), 'rb') as f: case_upload_file_meta = persistent_file_store.write_file( f, original_filename, domain) task = bulk_import_async.delay(config, domain, self.upload_id) CaseUploadRecord( domain=domain, comment=comment, upload_id=self.upload_id, task_id=task.task_id, couch_user_id=config.couch_user_id, case_type=config.case_type, upload_file_meta=case_upload_file_meta, ).save()