def test_task_pending(self, client, users, data_sets, authorized_john, task_properties_list): with client: task_id = 'task_id_pending' assert redis_client.get(task_id) is not None res = client.get(f'/api/v1/data_sets/generate/{task_id}', headers=HEADERS) assert res.status_code == 202 assert redis_client.get(task_id) is not None
def test_task_not_found(self, client, users, data_sets, authorized_john): with client: task_id = 'task_id_dummy' assert redis_client.get(task_id) is None res = client.get(f'/api/v1/data_sets/generate/{task_id}', headers=HEADERS) assert res.status_code == 404 assert res.get_json() == {'message': 'task not found'} assert redis_client.get(task_id) is None
def test_task_taro_success(self, client, users, data_sets, authorized_john, task_properties_list): with client: task_id = 'task_id_taro_success' assert redis_client.get(task_id) is not None res = client.get(f'/api/v1/data_sets/generate/{task_id}', headers=HEADERS) assert res.status_code == 404 assert res.get_json() == {'message': 'task not found'} assert redis_client.get(task_id) is not None
def test_task_failure(self, client, users, data_sets, authorized_john, task_properties_list): with client: task_id = 'task_id_failure' assert redis_client.get(task_id) is not None res = client.get(f'/api/v1/data_sets/generate/{task_id}', headers=HEADERS) assert res.status_code == 400 assert res.get_json() == {'message': 'file is invalid'} assert redis_client.get(task_id) is None
def test_task_started(self, client, users, data_sets, authorized_john, task_properties_list, mocker): process_mock = mocker.Mock() process_mock.returncode = 0 mocker.patch('dbcls.api.resources.data_set.subprocess.run' ).return_value = process_mock with client: task_id = 'task_id_started' assert redis_client.get(task_id) is not None res = client.get(f'/api/v1/data_sets/generate/{task_id}', headers=HEADERS) assert res.status_code == 204 assert redis_client.get(task_id) is not None
def test_task_unknown_error(self, client, users, data_sets, authorized_john, task_properties_list, mocker): process_mock = mocker.Mock() process_mock.returncode = 1 mocker.patch('dbcls.api.resources.data_set.subprocess.run' ).return_value = process_mock with client: task_id = 'task_id_started' assert redis_client.get(task_id) is not None res = client.get(f'/api/v1/data_sets/generate/{task_id}', headers=HEADERS) assert res.status_code == 400 assert res.get_json() == {'message': 'raised unknown error'} assert redis_client.get(task_id) is None
def get(self, task_id): task_properties = redis_client.get(task_id) if not task_properties: return {'message': 'task not found'}, 404 task_properties = json.loads(task_properties) if task_properties['user'] != g.user.id: return {'message': 'task not found'}, 404 if task_properties['state'] == UmakaparserState.PENDING.value: return '', 202 if task_properties['state'] == UmakaparserState.FAILURE.value: redis_client.delete(task_id) return {'message': task_properties['message']}, 400 if task_properties['state'] == UmakaparserState.STARTED.value: # プロセスが終了していたらキャッチできなかったエラーで終了している pid = task_properties['pid'] p = subprocess.run([f'ps -o command= {pid}'], stdout=subprocess.PIPE) if p.returncode != 0: redis_client.delete(task_id) return {'message': 'raised unknown error'}, 400 return '', 204 redis_client.delete(task_id) data_set = DataSet.query.get(task_properties['data_set_id']) return { 'id': data_set.id, 'title': data_set.title, 'path': data_set.path, 'upload_at': localize_as_jst(data_set.upload_at).isoformat(), 'is_public': data_set.is_public, }, 200
def test_task_success(self, client, users, data_sets, authorized_john, task_properties_list): with client: task_id = 'task_id_success' assert redis_client.get(task_id) is not None res = client.get(f'/api/v1/data_sets/generate/{task_id}', headers=HEADERS) assert res.status_code == 200 expected_data_set = data_sets[-1] assert res.get_json() == { 'id': expected_data_set.id, 'title': expected_data_set.title, 'path': expected_data_set.path, 'upload_at': localize_as_jst(expected_data_set.upload_at).isoformat(), 'is_public': expected_data_set.is_public, } assert redis_client.get(task_id) is None
def update_task_properties(task_id=None, **kwargs): if task_id is None: return task_properties = redis_client.get(task_id) if not task_properties: return task_properties = json.loads(task_properties) for key in kwargs: task_properties[key] = kwargs[key] pipe = redis_client.pipeline() pipe.set(task_id, json.dumps(task_properties)) pipe.expire(task_id, TASK_PROPERTIES_EXPIRE) pipe.execute()
def test_post(self, client, users, data_sets, authorized_john, ontology_path, sbm_path, mocker): mocker.patch('dbcls.api.resources.data_set.Process') with client: johns_data_set_query = DataSet.query.filter_by( user=authorized_john) previous_count = johns_data_set_query.count() data = { 'ontology': (BytesIO(Path(ontology_path).read_bytes()), 'ontology.ttl'), 'sbm': (BytesIO(Path(sbm_path).read_bytes()), 'sbm.ttl'), } res = client.post('/api/v1/data_sets/generate', data=data, headers=HEADERS, content_type='multipart/form-data') assert res.status_code == 201 data = res.get_json() assert 'task_id' in data task_properties = json.loads(redis_client.get(data['task_id'])) assert task_properties['state'] == UmakaparserState.PENDING.value