def test_get_result(project, app): for logs_limit in [None, -1, 0, 5, 7, 100]: for i in range(3): url = '/api/v1/projects/1/results/' + str(i + 1) if logs_limit is not None: url += '?logs_limit=' + str(logs_limit) resp = app.get(url) data = assert_json_api(resp) assert len(data) == 1 _assert_test_project_result(data['result'], project[0], None, logs_limit=logs_limit) assert data['result']['id'] == i + 1 # invalid project ID resp = app.get('/api/v1/projects/12345/results/1') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None # invalid result ID resp = app.get('/api/v1/projects/1/results/12345') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None
def test_post_project(func_dir, project, app): temp_dir = os.path.join(func_dir, 'test_post_project') os.mkdir(temp_dir) # success request_json = {'project': {'path_name': temp_dir}} resp = app.post('/api/v1/projects', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 _assert_test_project(data['project'], temp_dir, temp_dir) # fail, required param is lack request_json = {'project': {'name': 'name'}} resp = app.post('/api/v1/projects', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 2 # fail, the path is duplicated request_json = {'project': {'path_name': project[0]}} resp = app.post('/api/v1/projects', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 2
def test_get_assets(func_dir, project, app): project3_path = os.path.join(func_dir, 'test_project3') path = os.path.join(project3_path, '10004') os.makedirs(path) image_info = [{ "epoch": 1, "iteration": 600, "images": { "train": "iter_600_61b3a8fa.png", "train_reconstructed": "iter_600_c15c042b.png", } }] with open(os.path.join(path, '.chainerui_images'), 'w') as f: json.dump(image_info, f) open(os.path.join(path, 'iter_600_61b3a8fa.png'), 'w').close() open(os.path.join(path, 'iter_600_c15c042b.png'), 'w').close() with open(os.path.join(path, 'log'), 'w') as f: json.dump([], f) Project.create(project3_path, 'assets-test-project') url = '/api/v1/projects/2/results/4/assets' resp = app.get(url) data = assert_json_api(resp) assert 'assets' in data assert len(data['assets']) == 1 assert 'contents' in data['assets'][0] assert len(data['assets'][0]['contents']) == 2 assert 'train_info' in data['assets'][0] # invalid project ID resp = app.get('/api/v1/projects/12345/results/4/assets') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None # invalid result ID resp = app.get('/api/v1/projects/2/results/12345/assets') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None # empty assets resp = app.get('/api/v1/projects/1/results/1/assets') data = assert_json_api(resp) assert 'assets' in data assert len(data['assets']) == 0 # resource check resource_url = url + '/1' resp = app.get(resource_url) assert resp.status_code == 200 resource_url = url + '/3' resp = app.get(resource_url) data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['asset'] is None
def test_get_project(project, app): resp = app.get('/api/v1/projects/1') data = assert_json_api(resp) assert len(data) == 1 _assert_test_project(data['project'], *project) resp = app.get('/api/v1/projects/12345') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None
def test_delete_project(project, app): resp = app.delete('/api/v1/projects/1') data = assert_json_api(resp) assert len(data) == 1 _assert_test_project(data['project'], *project) resp = app.delete('/api/v1/projects/12345') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['projects'] is None # TODO(ofk): Is projects key correct?
def test_delete_result(project, app): for i in range(3): resp = app.delete('/api/v1/projects/1/results/' + str(i + 1)) data = assert_json_api(resp) assert len(data) == 1 _assert_test_project_result(data['result'], project[0], None) resp = app.delete('/api/v1/projects/1/results/12345') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None
def test_post_result(project, app): temp_dir = os.path.join(project[0], 'test_post_result') os.mkdir(temp_dir) # success test_time = os.path.getmtime(os.path.abspath(__file__)) request_json = { 'result': { 'pathName': temp_dir, 'name': 'post_test', 'crawlable': False, 'logModifiedAt': test_time } } resp = app.post('/api/v1/projects/1/results', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 # project ID 1 has already registered 3 results assert data['result']['id'] == 4 # fail, same path request_json['result']['name'] = 'post_test 2' resp = app.post('/api/v1/projects/1/results', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None # fail, required param is lack del request_json['result']['pathName'] resp = app.post('/api/v1/projects/1/results', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None # fail, invalid project ID resp = app.post('/api/v1/projects/12345/results', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None
def test_handle_invalid_usage(self): resp = self.app.get('/api/v1/projects') data = assert_json_api(resp, 400) assert len(data) == 1 assert len(data['error']) == 2 assert isinstance(data['error']['message'], string_types) assert 'DBOperationalError' == data['error']['type']
def test_get_project_list(project, app): path, name = project resp = app.get('/api/v1/projects') data = assert_json_api(resp) assert len(data) == 1 assert isinstance(data['projects'], list) _assert_test_project(data['projects'][0], path, name)
def test_get_result_list(self): resp = self.app.get('/api/v1/projects/1/results') data = assert_json_api(resp) assert len(data) == 1 assert len(data['results']) >= 3 for i in range(3): self.assert_test_project_result(data['results'][i])
def test_get_project_with_name(project, app): # success url = '/api/v1/projects?path_name=' + project[0] resp = app.get(url) data = assert_json_api(resp) assert len(data) == 1 _assert_test_project(data['project'], *project) # fail dummy_project_path = 'no-exist' url = '/api/v1/projects?path_name=' + dummy_project_path resp = app.get(url) data = assert_json_api(resp, 400) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None assert dummy_project_path in data['message']
def test_get_result_with_name(project, app): # success url = '/api/v1/projects/1/results?path_name=' + os.path.join( project[0], '10000') resp = app.get(url) data = assert_json_api(resp) assert len(data) == 1 _assert_test_project_result(data['result'], project[0], None) # fail dummy_result_path = os.path.join(project[0], 'no-exist') url = '/api/v1/projects/1/results?path_name=' + dummy_result_path resp = app.get(url) data = assert_json_api(resp, 400) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None assert dummy_result_path in data['message']
def test_get_result(self): for i in range(3): resp = self.app.get('/api/v1/projects/1/results/' + str(i + 1)) data = assert_json_api(resp) assert len(data) == 1 self.assert_test_project_result(data['result']) assert data['result']['id'] == i + 1 # invalid project ID resp = self.app.get('/api/v1/projects/12345/results/1') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None # invalid result ID resp = self.app.get('/api/v1/projects/1/results/12345') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None
def test_put_project(self): request_json = { 'project': { 'id': 1, 'name': 'new-name', } } resp = self.app.put('/api/v1/projects/1', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 self.assert_test_project(data['project'], name=request_json['project']['name']) resp = self.app.put('/api/v1/projects/12345') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None
def test_get_result_list(self): for logs_limit in [None, -1, 0, 5, 7, 100]: url = '/api/v1/projects/1/results' if logs_limit is not None: url += '?logs_limit=' + str(logs_limit) resp = self.app.get(url) data = assert_json_api(resp) assert len(data) == 1 assert len(data['results']) >= 3 for i in range(3): self.assert_test_project_result(data['results'][i], logs_limit=logs_limit)
def test_put_result(project, app): request_jsons = [ { 'result': { 'id': 1, 'name': 'new-name1', } }, { 'result': { 'id': 2, 'name': 'new-name2', 'isUnregistered': True, } }, { 'result': { 'id': 3, 'name': 'new-name3', 'isUnregistered': False, } }, ] for i in range(3): resp = app.put('/api/v1/projects/1/results/' + str(i + 1), data=json.dumps(request_jsons[i]), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 _assert_test_project_result(data['result'], project[0], request_jsons[i]['result']['name']) resp = app.put('/api/v1/projects/1/results/12345') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None
def test_post_argument(app): # success request_json = {'argument': {'key': 'value'}} resp = app.post('/api/v1/projects/1/results/1/args', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 assert data['argument']['resultId'] == 1 # success, empty args request_json['argument'] = {} resp = app.post('/api/v1/projects/1/results/1/args', data=json.dumps(request_json), content_type='application/json') data2 = assert_json_api(resp) assert len(data2) == 1 assert data2['argument']['id'] == data['argument']['id'] + 1 # fail, invalid project ID resp = app.post('/api/v1/projects/12345/results/1/args', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None # fail, invalid result ID resp = app.post('/api/v1/projects/1/results/12345/args', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None
def test_post_result_command(self): request_jsons = [ { 'name': 'adjust_hyperparams', 'body': { 'alpha': 0.0007, 'beta1': 0.8, 'beat2': 1.0, }, 'schedule': { 'value': 4, 'key': 'epoch', }, 'resultId': 1, }, { 'name': 'adjust_hyperparams', 'body': None, 'schedule': None, 'resultId': 2, }, { 'name': 'take_snapshot', 'schedule': { 'value': 4800, 'key': 'iteration', }, 'resultId': 3, }, ] # job has already started for i in range(3): resp = self.app.post('/api/v1/projects/1/results/4/commands', data=json.dumps(request_jsons[i]), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 assert len(data['commands']) > 0 command = data['commands'][0] assert isinstance(command['id'], int) assert isinstance(command['name'], string_types) assert len(command['request']) == 4 assert command['request']['schedule'] is None or isinstance( command['request']['schedule'], dict) assert command['request']['body'] is None or isinstance( command['request']['body'], dict) assert isinstance(command['request']['created_at'], string_types) assert isinstance(command['request']['status'], string_types) assert 'response' in command # not set extension resp = self.app.post('/api/v1/projects/1/results/2/commands', data=json.dumps(request_jsons[0]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) assert 'not set' in data['message'] # job run on v0.1.0 so .chainerui_commands is not created resp = self.app.post('/api/v1/projects/1/results/3/commands', data=json.dumps(request_jsons[0]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) assert 'stopped' in data['message'] # jos has stopped resp = self.app.post('/api/v1/projects/1/results/5/commands', data=json.dumps(request_jsons[0]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) assert 'stopped' in data['message'] # not set extension resp = self.app.post('/api/v1/projects/1/results/6/commands', data=json.dumps(request_jsons[0]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) assert 'not run' in data['message'] request_jsons = [ { 'name': 'invalid_schedule', 'schedule': { 'value': None, 'key': 'epoch', }, 'resultId': 1, }, { 'name': None, 'resultId': 2, }, ] for i in range(2): resp = self.app.post('/api/v1/projects/1/results/4/commands', data=json.dumps(request_jsons[i]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) resp = self.app.post('/api/v1/projects/1/results/3/commands') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) resp = self.app.post('/api/v1/projects/1/results/12345/commands') data = assert_json_api(resp, 404) assert isinstance(data['message'], string_types) assert data['result'] is None
def test_post_result_command(func_dir, project, app): project2_path = os.path.join(func_dir, 'test_project2') result_path = os.path.join(project2_path, '10003') os.makedirs(result_path) with open(os.path.join(result_path, 'log'), 'w') as f: json.dump([], f) Project.create(project2_path, 'command-test-project') request_jsons = [ { 'name': 'adjust_hyperparams', 'body': { 'alpha': 0.0007, 'beta1': 0.8, 'beat2': 1.0, }, 'schedule': { 'value': 4, 'key': 'epoch', }, 'resultId': 1, }, { 'name': 'adjust_hyperparams', 'body': None, 'schedule': None, 'resultId': 2, }, { 'name': 'take_snapshot', 'schedule': { 'value': 4800, 'key': 'iteration', }, 'resultId': 3, }, ] # not set extension resp = app.post('/api/v1/projects/2/results/4/commands', data=json.dumps(request_jsons[0]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) assert 'not set' in data['message'] # job run on v0.1.0 so .chainerui_commands is not created with open(os.path.join(result_path, 'commands'), 'w') as f: json.dump([], f) resp = app.post('/api/v1/projects/2/results/4/commands', data=json.dumps(request_jsons[0]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) assert 'stopped' in data['message'] # extension is set up but not run os.remove(os.path.join(result_path, '.chainerui_commands')) CommandsState._dump(result_path, CommandsState._load(result_path, initialize=True)) resp = app.post('/api/v1/projects/2/results/4/commands', data=json.dumps(request_jsons[0]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) assert 'not run' in data['message'] # job has already started CommandsState.run(result_path) for i in range(3): resp = app.post('/api/v1/projects/2/results/4/commands', data=json.dumps(request_jsons[i]), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 assert len(data['commands']) > 0 command = data['commands'][0] assert isinstance(command['id'], int) assert isinstance(command['name'], string_types) assert len(command['request']) == 4 assert command['request']['schedule'] is None or isinstance( command['request']['schedule'], dict) assert command['request']['body'] is None or isinstance( command['request']['body'], dict) assert isinstance(command['request']['created_at'], string_types) assert isinstance(command['request']['status'], string_types) assert 'response' in command # job has stopped CommandsState.stop(result_path) resp = app.post('/api/v1/projects/2/results/4/commands', data=json.dumps(request_jsons[0]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) assert 'stopped' in data['message'] request_jsons = [ { 'name': 'invalid_schedule', 'schedule': { 'value': None, 'key': 'epoch', }, 'resultId': 1, }, { 'name': None, 'resultId': 2, }, ] for i in range(2): resp = app.post('/api/v1/projects/2/results/4/commands', data=json.dumps(request_jsons[i]), content_type='application/json') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) resp = app.post('/api/v1/projects/2/results/4/commands') data = assert_json_api(resp, 400) assert len(data) == 1 assert isinstance(data['message'], string_types) resp = app.post('/api/v1/projects/2/results/12345/commands') data = assert_json_api(resp, 404) assert isinstance(data['message'], string_types) assert data['result'] is None
def test_post_log(app): # success test_time = os.path.getmtime(os.path.abspath(__file__)) request_json = { 'log': { 'values': [{ 'epoch': 8, 'loss': 0.1 }, { 'epoch': 9, 'loss': 0.1 }], 'modifiedAt': test_time } } resp = app.post('/api/v1/projects/1/results/1/logs', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 assert data['logs']['resultId'] == 1 assert data['logs']['insertedLogCount'] == 2 assert data['logs']['totalLogCount'] == 9 # success, reset request_json = { 'log': { 'values': [{ 'epoch': 1, 'loss': 0.1 }, { 'epoch': 2, 'loss': 0.1 }], 'reset': True } } resp = app.post('/api/v1/projects/1/results/1/logs', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 assert data['logs']['resultId'] == 1 assert data['logs']['insertedLogCount'] == 2 assert data['logs']['totalLogCount'] == 2 # success, empty log request_json['log']['values'] = [] request_json['log']['reset'] = False resp = app.post('/api/v1/projects/1/results/1/logs', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp) assert len(data) == 1 assert data['logs']['resultId'] == 1 assert data['logs']['insertedLogCount'] == 0 assert data['logs']['totalLogCount'] == 2 # fail, invalid project ID resp = app.post('/api/v1/projects/12345/results/1/logs', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None # fail, invalid result ID resp = app.post('/api/v1/projects/1/results/12345/logs', data=json.dumps(request_json), content_type='application/json') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None
def test_get_assets(func_dir, app): project3_path = os.path.join(func_dir, 'test_project3') path = os.path.join(project3_path, '10004') os.makedirs(path) assets_info = [{ "epoch": 1, "iteration": 600, "images": { "train": "iter_600_61b3a8fa.png", "train_reconstructed": "iter_600_c15c042b.jpg", }, "audios": { "sampling": "audio_0_755e0f8b17cd.wav", }, }] with open(os.path.join(path, summary.CHAINERUI_ASSETS_METAFILE_NAME), 'w') as f: json.dump(assets_info, f) open(os.path.join(path, 'iter_600_61b3a8fa.png'), 'w').close() open(os.path.join(path, 'iter_600_c15c042b.jpg'), 'w').close() open(os.path.join(path, 'audio_0_755e0f8b17cd.wav'), 'w').close() with open(os.path.join(path, 'log'), 'w') as f: json.dump([], f) Project.create(project3_path, 'assets-test-project') url = '/api/v1/projects/2/results/4/assets' resp = app.get(url) data = assert_json_api(resp) assert 'assets' in data assert len(data['assets']) == 1 assert 'contents' in data['assets'][0] assert len(data['assets'][0]['contents']) == 3 assert 'train_info' in data['assets'][0] # invalid project ID resp = app.get('/api/v1/projects/12345/results/4/assets') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['project'] is None # invalid result ID resp = app.get('/api/v1/projects/2/results/12345/assets') data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['result'] is None # empty assets resp = app.get('/api/v1/projects/1/results/1/assets') data = assert_json_api(resp) assert 'assets' in data assert len(data['assets']) == 0 # resource check resource_url_png = url + '/1' resp = app.get(resource_url_png) assert resp.status_code == 200 resource_url_jpg = url + '/2' resp = app.get(resource_url_jpg) assert resp.status_code == 200 resource_url_wav = url + '/3' resp = app.get(resource_url_wav) assert resp.status_code == 200 resource_url = url + '/4' resp = app.get(resource_url) data = assert_json_api(resp, 404) assert len(data) == 2 assert isinstance(data['message'], string_types) assert data['asset'] is None
def test_get_project_list(self): resp = self.app.get('/api/v1/projects') data = assert_json_api(resp) assert len(data) == 1 assert isinstance(data['projects'], list) self.assert_test_project(data['projects'][0])