# Path: tests/commands/execution/utils.py
# def get_execution_data_mock():
#     exec_id = EXECUTION_DATA['id']
#     datum_id = OUTPUT_DATUM_DATA['id']
#     m = requests_mock.mock()
#     project_id = PROJECT_DATA['id']
#     execution_counter = EXECUTION_DATA['counter']
#     m.get(f'{API_PREFIX}projects/{project_id}/', json=PROJECT_DATA)
#     m.get(f'{API_PREFIX}executions/', json={'results': [EXECUTION_DATA]})
#     m.get(f'{API_PREFIX}executions/{exec_id}/', json=EXECUTION_DATA)
#     m.get(f'{API_PREFIX}executions/{exec_id}/events/', json=EVENT_RESPONSE_DATA)
#     m.get(f'{API_PREFIX}data/?output_execution={exec_id}&limit=9000', json=OUTPUT_DATUM_RESPONSE_DATA)
#     m.get(f'{API_PREFIX}data/{datum_id}/download/', json=OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA)
#     execution_by_counter_url = f'{API_PREFIX}executions/{project_id}:{execution_counter}/'
#     m.get(url=get_startswith_re(execution_by_counter_url), json=EXECUTION_DATA)
#     m.delete(execution_by_counter_url, json={'ok': True})
#     m.post(re.compile('^https://app.valohai.com/api/v0/data/(.+?)/purge/$'), json={'ok': True})
#     return m
# 
# Path: tests/fixture_data.py
# EXECUTION_DATA = {
#     'counter': random.randint(1, 100),
#     'ctime': '2017-02-08T11:09:16.120102Z',
#     'id': execution_id,
#     'project': PROJECT_DATA,
#     'commit': {
#         'repository': 666,
#         'identifier': '000',
#         'ref': 'master',
#         'commit_time': '2017-02-15T08:46:58Z',
#         'url': 'https://app.valohai.com/api/v0/commits/7/'
#     },
#     'task': None,
#     'duration': 777,
#     'status': 'complete',
#     'step': 'run training',
#     'url': f'https://app.valohai.com/api/v0/executions/{execution_id}/',
#     'urls': {
#         'copy': 'https://app.valohai.com/api/v0/executions/34/copy/',
#         'display': 'https://app.valohai.com/p/test/mnist/execution/34/',
#         'stop': 'https://app.valohai.com/api/v0/executions/34/stop/',
#     },
#     'parameters': {
#         'dropout': 0.9,
#         'learning_rate': 0.001,
#         'max_steps': 300,
#     },
#     'outputs': [
#         OUTPUT_DATUM_DATA,
#     ],
#     'environment': {
#         'id': '88888888-8888-8888-8888-888888888888',
#         'name': 'local',
#         'owner': None,
#         'unfinished_job_count': 0,
#     },
#     'cumulative_metadata': {
#         'oispa': 'beer',
#     },
# }
# 
# OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA = {
#     'url': f'https://example.com/{str(uuid.uuid4())}',
# }
# 
# Path: valohai_cli/commands/execution/outputs.py
# @click.command()
# @counter_argument
# @click.option('--download', '-d', 'download_directory', type=click.Path(file_okay=False), help=DOWNLOAD_HELP,
#               default=None)
# @click.option('--filter-download', '-f', help=GLOB_HELP, default=None)
# @click.option('--force', is_flag=True, help='Download all files even if they already exist.')
# @click.option('--sync', '-s', is_flag=True, help='Keep watching for new output files to download.')
# def outputs(counter: str, download_directory: Optional[str], filter_download: Optional[str], force: bool,
#             sync: bool) -> None:
#     """
#     List and download execution outputs.
#     """
#     if download_directory:
#         download_directory = download_directory.replace("{counter}", str(counter))
# 
#     if sync:
#         watch(counter, force, filter_download, download_directory)
#         return
# 
#     project = get_project(require=True)
#     assert project
#     execution = project.get_execution_from_counter(
#         counter=counter,
#         params={'exclude': 'outputs'},
#     )
#     outputs = get_execution_outputs(execution)
#     if not outputs:
#         warn('The execution has no outputs.')
#         return
#     for output in outputs:
#         output['datum_url'] = f"datum://{output['id']}"
#     print_table(outputs, ('name', 'datum_url', 'size'))
#     if download_directory:
#         outputs = filter_outputs(outputs, download_directory, filter_download, force)
#         download_outputs(outputs, download_directory, show_success_message=True)


import os
import pytest
from tests.commands.execution.utils import get_execution_data_mock
from tests.fixture_data import EXECUTION_DATA, OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA
from valohai_cli.commands.execution.outputs import outputs




@pytest.mark.parametrize('download', (False, True))
def test_execution_outputs(runner, logged_in_and_linked, tmpdir, download):
    tmpdir = str(tmpdir)

    with get_execution_data_mock() as m:
        m.get(OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA['url'], content=b'0' * 100)
        params = [str(EXECUTION_DATA['counter'])]
        if download:
            params.append(f'--download={tmpdir}')
        output = runner.invoke(outputs, params, catch_exceptions=False).output
        assert all(o['name'] in output for o in EXECUTION_DATA['outputs'])
        if download:
            # See that things were downloaded

# Path: tests/commands/execution/utils.py
# def get_execution_data_mock():
#     exec_id = EXECUTION_DATA['id']
#     datum_id = OUTPUT_DATUM_DATA['id']
#     m = requests_mock.mock()
#     project_id = PROJECT_DATA['id']
#     execution_counter = EXECUTION_DATA['counter']
#     m.get(f'{API_PREFIX}projects/{project_id}/', json=PROJECT_DATA)
#     m.get(f'{API_PREFIX}executions/', json={'results': [EXECUTION_DATA]})
#     m.get(f'{API_PREFIX}executions/{exec_id}/', json=EXECUTION_DATA)
#     m.get(f'{API_PREFIX}executions/{exec_id}/events/', json=EVENT_RESPONSE_DATA)
#     m.get(f'{API_PREFIX}data/?output_execution={exec_id}&limit=9000', json=OUTPUT_DATUM_RESPONSE_DATA)
#     m.get(f'{API_PREFIX}data/{datum_id}/download/', json=OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA)
#     execution_by_counter_url = f'{API_PREFIX}executions/{project_id}:{execution_counter}/'
#     m.get(url=get_startswith_re(execution_by_counter_url), json=EXECUTION_DATA)
#     m.delete(execution_by_counter_url, json={'ok': True})
#     m.post(re.compile('^https://app.valohai.com/api/v0/data/(.+?)/purge/$'), json={'ok': True})
#     return m
# 
# Path: tests/fixture_data.py
# EXECUTION_DATA = {
#     'counter': random.randint(1, 100),
#     'ctime': '2017-02-08T11:09:16.120102Z',
#     'id': execution_id,
#     'project': PROJECT_DATA,
#     'commit': {
#         'repository': 666,
#         'identifier': '000',
#         'ref': 'master',
#         'commit_time': '2017-02-15T08:46:58Z',
#         'url': 'https://app.valohai.com/api/v0/commits/7/'
#     },
#     'task': None,
#     'duration': 777,
#     'status': 'complete',
#     'step': 'run training',
#     'url': f'https://app.valohai.com/api/v0/executions/{execution_id}/',
#     'urls': {
#         'copy': 'https://app.valohai.com/api/v0/executions/34/copy/',
#         'display': 'https://app.valohai.com/p/test/mnist/execution/34/',
#         'stop': 'https://app.valohai.com/api/v0/executions/34/stop/',
#     },
#     'parameters': {
#         'dropout': 0.9,
#         'learning_rate': 0.001,
#         'max_steps': 300,
#     },
#     'outputs': [
#         OUTPUT_DATUM_DATA,
#     ],
#     'environment': {
#         'id': '88888888-8888-8888-8888-888888888888',
#         'name': 'local',
#         'owner': None,
#         'unfinished_job_count': 0,
#     },
#     'cumulative_metadata': {
#         'oispa': 'beer',
#     },
# }
# 
# OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA = {
#     'url': f'https://example.com/{str(uuid.uuid4())}',
# }
# 
# Path: valohai_cli/commands/execution/outputs.py
# @click.command()
# @counter_argument
# @click.option('--download', '-d', 'download_directory', type=click.Path(file_okay=False), help=DOWNLOAD_HELP,
#               default=None)
# @click.option('--filter-download', '-f', help=GLOB_HELP, default=None)
# @click.option('--force', is_flag=True, help='Download all files even if they already exist.')
# @click.option('--sync', '-s', is_flag=True, help='Keep watching for new output files to download.')
# def outputs(counter: str, download_directory: Optional[str], filter_download: Optional[str], force: bool,
#             sync: bool) -> None:
#     """
#     List and download execution outputs.
#     """
#     if download_directory:
#         download_directory = download_directory.replace("{counter}", str(counter))
# 
#     if sync:
#         watch(counter, force, filter_download, download_directory)
#         return
# 
#     project = get_project(require=True)
#     assert project
#     execution = project.get_execution_from_counter(
#         counter=counter,
#         params={'exclude': 'outputs'},
#     )
#     outputs = get_execution_outputs(execution)
#     if not outputs:
#         warn('The execution has no outputs.')
#         return
#     for output in outputs:
#         output['datum_url'] = f"datum://{output['id']}"
#     print_table(outputs, ('name', 'datum_url', 'size'))
#     if download_directory:
#         outputs = filter_outputs(outputs, download_directory, filter_download, force)
#         download_outputs(outputs, download_directory, show_success_message=True)


# Path: tests/commands/execution/test_outputs.py
import os
import pytest
from tests.commands.execution.utils import get_execution_data_mock
from tests.fixture_data import EXECUTION_DATA, OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA
from valohai_cli.commands.execution.outputs import outputs





@pytest.mark.parametrize('download', (False, True))
def test_execution_outputs(runner, logged_in_and_linked, tmpdir, download):
    tmpdir = str(tmpdir)

    with get_execution_data_mock() as m:
        m.get(OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA['url'], content=b'0' * 100)
        params = [str(EXECUTION_DATA['counter'])]
        if download:
            params.append(f'--download={tmpdir}')
        output = runner.invoke(outputs, params, catch_exceptions=False).output
        assert all(o['name'] in output for o in EXECUTION_DATA['outputs'])
        if download:
            # See that things were downloaded

            for output in EXECUTION_DATA['outputs']: