def get_project_data(n_projects): return { 'results': [{ 'id': str(uuid4()), 'name': get_random_string() } for i in range(n_projects)], }
def test_init(runner, logged_in): name = get_random_string() dir = get_project_directory() with open(os.path.join(dir, 'my_script.py'), 'w') as script_fp: script_fp.write('# Hello') with requests_mock.mock() as m: m.post('https://app.valohai.com/api/v0/projects/', json={ 'id': str(uuid4()), 'name': name, }) result = runner.invoke(init, input='\n'.join([ 'y', # correct directory 'echo hello', # command 'y', # yes, that's right '1', # image number 1 'n', # no, actually '', # erm what 'docker', # image called docker, yes 'y', # okay, that's better 'y', # confirm write 'c', # create new name, # that's a nice name ]), catch_exceptions=False) assert result.exit_code == 0 assert 'my_script.py' in result.output assert 'Happy (machine) learning!' in result.output assert os.path.exists(os.path.join(dir, 'valohai.yaml')) assert get_project(dir)
def test_create_linked(runner, logged_in_and_linked, input): name = get_random_string() with get_project_mock(name): result = runner.invoke(create, ['-n', name], input=input) if input == 'y': assert (f'{name} created') in result.output assert 'Linked' in result.output
def test_create(runner, logged_in, link): name = get_random_string() with get_project_mock(name): result = runner.invoke( create, ['-n', name, ('--link' if link else '--no-link')]) assert (f'{name} created') in result.output if link: assert 'Linked' in result.output
def get_project_mock(create_project_name=None, existing_projects=None): username = get_random_string() m = requests_mock.mock() if isinstance(existing_projects, int): existing_projects = get_project_list_data([get_random_string() for x in range(existing_projects)]) if existing_projects is not None: m.get('https://app.valohai.com/api/v0/projects/', json=existing_projects) if create_project_name: m.post('https://app.valohai.com/api/v0/projects/', json=lambda request, context: { 'id': str(uuid4()), 'name': create_project_name, 'owner': { 'id': 8, 'username': username, } }) m.get('https://app.valohai.com/api/v0/projects/ownership_options/', json=[username]) return m
def __init__(self, username='******', password='******'): super().__init__() self.username = username self.password = password self.token = get_random_string() self.post('https://app.valohai.com/api/v0/get-token/', json=self.handle_get_token) self.get('https://app.valohai.com/api/v0/users/me/', json=self.handle_get_me)
def test_link_no_projs(runner, logged_in, create): name = get_random_string() with get_project_mock(existing_projects=0, create_project_name=(name if create else None)): result = runner.invoke(link, input=(f'y\n{name}\n' if create else 'n\n')) assert 'Create one instead?' in result.output if create: assert (f'{name} created') in result.output
def handle_create_commit(self, request, context): assert request.body commit_id = "~%s" % get_random_string() self.commit_id = commit_id # Only accept the new commit return { 'repository': '8', 'identifier': commit_id, 'ref': 'adhoc', 'ctime': '2017-03-09T14:56:53.875721Z', 'commit_time': '2017-03-09T14:56:53.875475Z', }
def test_api_error(logged_in, capsys): nonce = get_random_string() message = f'Oh no! {nonce}' with requests_mock.mock() as m: m.get('https://app.valohai.com/api/foo/', json={'error': message}, status_code=406) with pytest.raises(APIError) as aei: request('get', 'https://app.valohai.com/api/foo/') aei.value.show() out, err = capsys.readouterr() assert message in err
def test_link_projs_create_one_instead(runner, logged_in): name = get_random_string() with get_project_mock(existing_projects=1, create_project_name=name): result = runner.invoke(link, input=f'c\n{name}\n') assert 'Name the new project:' in result.output assert (f'{name} created') in result.output