def setup_test_db(): create_db() upgrade_db() # insert test data Project.create(TEST_PROJECT_PATH, TEST_PROJECT_NAME)
def test_post_result_command(self): project2_path = os.path.join(self._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 = self.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 = self.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 = self.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 = self.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 = self.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 = self.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 = self.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 = self.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_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 setup_test_db(project_path, project_name): create_db() upgrade_db() # insert test data Project.create(project_path, project_name)
def _setup_test_db(project_path, project_name): db.upgrade() # insert test data Project.create(project_path, project_name)