# 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',
#     },
# }
# 
# Path: valohai_cli/commands/execution/stop.py
# @click.argument(
#     'counters',
#     help='Range of execution counters, or "latest"',
#     required=False,
#     nargs=-1,
#     cls=HelpfulArgument,
# )
# @click.option('--all', default=None, is_flag=True, help='Stop all in-progress executions.')
# @click.command()
# def stop(
#     counters: Optional[Union[List[str], Tuple[str]]] = None,
#     all: bool = False,
# ) -> None:
#     """
#     Stop one or more in-progress executions.
#     """
#     project = get_project(require=True)
#     assert project
# 
#     if counters and len(counters) == 1 and counters[0] == 'all':  # pragma: no cover
#         # Makes sense to support this spelling too.
#         counters = None
#         all = True
# 
#     if counters and all:
#         # If we spell out latest and ranges in the error message, it becomes kinda
#         # unwieldy, so let's just do this.
#         raise click.UsageError('Pass execution counter(s), or `--all`, not both.')
# 
#     counters = list(counters or [])
#     executions = get_executions_for_stop(
#         project,
#         counters=counters,
#         all=all,
#     )
# 
#     for execution in executions:
#         progress(f"Stopping #{execution['counter']}... ")
#         resp = request('post', execution['urls']['stop'])
#         info(resp.text)
#     success('Done.')


import pytest
import requests_mock
from tests.fixture_data import EXECUTION_DATA
from valohai_cli.commands.execution.stop import stop



def test_stop_requires_arg(runner, logged_in_and_linked):
    output = runner.invoke(stop, catch_exceptions=False).output
    assert 'Nothing to stop' in output


@pytest.mark.parametrize('latest', (False, True))
def test_stop(runner, logged_in_and_linked, latest):
    counter = EXECUTION_DATA['counter']

    with requests_mock.mock() as m:
        m.get('https://app.valohai.com/api/v0/executions/', json={
            'results': [EXECUTION_DATA],
        })
        m.get(
            f'https://app.valohai.com/api/v0/executions/{EXECUTION_DATA["project"]["id"]}:latest/',
            json=EXECUTION_DATA,
        )
        m.post(EXECUTION_DATA['urls']['stop'], json={'message': 'OK'})

        args = (['latest'] if latest else [str(counter)])

# 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',
#     },
# }
# 
# Path: valohai_cli/commands/execution/stop.py
# @click.argument(
#     'counters',
#     help='Range of execution counters, or "latest"',
#     required=False,
#     nargs=-1,
#     cls=HelpfulArgument,
# )
# @click.option('--all', default=None, is_flag=True, help='Stop all in-progress executions.')
# @click.command()
# def stop(
#     counters: Optional[Union[List[str], Tuple[str]]] = None,
#     all: bool = False,
# ) -> None:
#     """
#     Stop one or more in-progress executions.
#     """
#     project = get_project(require=True)
#     assert project
# 
#     if counters and len(counters) == 1 and counters[0] == 'all':  # pragma: no cover
#         # Makes sense to support this spelling too.
#         counters = None
#         all = True
# 
#     if counters and all:
#         # If we spell out latest and ranges in the error message, it becomes kinda
#         # unwieldy, so let's just do this.
#         raise click.UsageError('Pass execution counter(s), or `--all`, not both.')
# 
#     counters = list(counters or [])
#     executions = get_executions_for_stop(
#         project,
#         counters=counters,
#         all=all,
#     )
# 
#     for execution in executions:
#         progress(f"Stopping #{execution['counter']}... ")
#         resp = request('post', execution['urls']['stop'])
#         info(resp.text)
#     success('Done.')


# Path: tests/commands/execution/test_stop.py
import pytest
import requests_mock
from tests.fixture_data import EXECUTION_DATA
from valohai_cli.commands.execution.stop import stop




def test_stop_requires_arg(runner, logged_in_and_linked):
    output = runner.invoke(stop, catch_exceptions=False).output
    assert 'Nothing to stop' in output


@pytest.mark.parametrize('latest', (False, True))
def test_stop(runner, logged_in_and_linked, latest):
    counter = EXECUTION_DATA['counter']

    with requests_mock.mock() as m:
        m.get('https://app.valohai.com/api/v0/executions/', json={
            'results': [EXECUTION_DATA],
        })
        m.get(
            f'https://app.valohai.com/api/v0/executions/{EXECUTION_DATA["project"]["id"]}:latest/',
            json=EXECUTION_DATA,
        )
        m.post(EXECUTION_DATA['urls']['stop'], json={'message': 'OK'})

        args = (['latest'] if latest else [str(counter)])

        output = runner.invoke(stop, args, catch_exceptions=False).output