|
| 1 | +import uuid |
| 2 | +from contextlib import asynccontextmanager |
| 3 | +from datetime import datetime |
| 4 | +from typing import Any |
| 5 | +from unittest.mock import AsyncMock |
| 6 | + |
| 7 | +from aiohttp.typedefs import StrOrURL |
| 8 | +from icij_worker import Task, TaskError, TaskState |
| 9 | +from icij_worker.objects import StacktraceItem |
| 10 | + |
| 11 | +from datashare_python.task_client import DatashareTaskClient |
| 12 | + |
| 13 | + |
| 14 | +async def test_task_client_create_task(monkeypatch): |
| 15 | + # Given |
| 16 | + datashare_url = "http://some-url" |
| 17 | + api_key = "some-api-key" |
| 18 | + task_name = "hello" |
| 19 | + task_id = f"{task_name}-{uuid.uuid4()}" |
| 20 | + args = {"greeted": "world"} |
| 21 | + group = "PYTHON" |
| 22 | + |
| 23 | + @asynccontextmanager |
| 24 | + async def _put_and_assert(_, url: StrOrURL, *, data: Any = None, **kwargs: Any): |
| 25 | + assert url == f"/api/task/{task_id}?group={group}" |
| 26 | + expected_task = { |
| 27 | + "@type": "Task", |
| 28 | + "id": task_id, |
| 29 | + "state": "CREATED", |
| 30 | + "name": "hello", |
| 31 | + "args": {"greeted": "world"}, |
| 32 | + } |
| 33 | + expected_data = expected_task |
| 34 | + assert data is None |
| 35 | + json_data = kwargs.pop("json") |
| 36 | + assert not kwargs |
| 37 | + assert json_data == expected_data |
| 38 | + mocked_res = AsyncMock() |
| 39 | + mocked_res.json.return_value = {"taskId": task_id} |
| 40 | + yield mocked_res |
| 41 | + |
| 42 | + monkeypatch.setattr("icij_worker.utils.http.AiohttpClient._put", _put_and_assert) |
| 43 | + |
| 44 | + task_client = DatashareTaskClient(datashare_url, api_key=api_key) |
| 45 | + async with task_client: |
| 46 | + # When |
| 47 | + t_id = await task_client.create_task(task_name, args, id_=task_id, group=group) |
| 48 | + assert t_id == task_id |
| 49 | + |
| 50 | + |
| 51 | +async def test_task_client_get_task(monkeypatch): |
| 52 | + # Given |
| 53 | + datashare_url = "http://some-url" |
| 54 | + api_key = "some-api-key" |
| 55 | + task_name = "hello" |
| 56 | + task_id = f"{task_name}-{uuid.uuid4()}" |
| 57 | + |
| 58 | + @asynccontextmanager |
| 59 | + async def _get_and_assert( |
| 60 | + _, url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Any |
| 61 | + ): |
| 62 | + assert url == f"/api/task/{task_id}" |
| 63 | + task = { |
| 64 | + "@type": "Task", |
| 65 | + "id": task_id, |
| 66 | + "state": "CREATED", |
| 67 | + "createdAt": datetime.now(), |
| 68 | + "name": "hello", |
| 69 | + "args": {"greeted": "world"}, |
| 70 | + } |
| 71 | + assert allow_redirects |
| 72 | + assert not kwargs |
| 73 | + mocked_res = AsyncMock() |
| 74 | + mocked_res.json.return_value = task |
| 75 | + yield mocked_res |
| 76 | + |
| 77 | + monkeypatch.setattr("icij_worker.utils.http.AiohttpClient._get", _get_and_assert) |
| 78 | + |
| 79 | + task_client = DatashareTaskClient(datashare_url, api_key=api_key) |
| 80 | + async with task_client: |
| 81 | + # When |
| 82 | + task = await task_client.get_task(task_id) |
| 83 | + assert isinstance(task, Task) |
| 84 | + |
| 85 | + |
| 86 | +async def test_task_client_get_task_state(monkeypatch): |
| 87 | + # Given |
| 88 | + datashare_url = "http://some-url" |
| 89 | + api_key = "some-api-key" |
| 90 | + task_name = "hello" |
| 91 | + task_id = f"{task_name}-{uuid.uuid4()}" |
| 92 | + |
| 93 | + @asynccontextmanager |
| 94 | + async def _get_and_assert( |
| 95 | + _, url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Any |
| 96 | + ): |
| 97 | + assert url == f"/api/task/{task_id}" |
| 98 | + task = { |
| 99 | + "@type": "Task", |
| 100 | + "id": task_id, |
| 101 | + "state": "DONE", |
| 102 | + "createdAt": datetime.now(), |
| 103 | + "completedAt": datetime.now(), |
| 104 | + "name": "hello", |
| 105 | + "args": {"greeted": "world"}, |
| 106 | + "result": "hellow world", |
| 107 | + } |
| 108 | + assert allow_redirects |
| 109 | + assert not kwargs |
| 110 | + mocked_res = AsyncMock() |
| 111 | + mocked_res.json.return_value = task |
| 112 | + yield mocked_res |
| 113 | + |
| 114 | + monkeypatch.setattr("icij_worker.utils.http.AiohttpClient._get", _get_and_assert) |
| 115 | + |
| 116 | + task_client = DatashareTaskClient(datashare_url, api_key=api_key) |
| 117 | + async with task_client: |
| 118 | + # When |
| 119 | + res = await task_client.get_task_state(task_id) |
| 120 | + assert res == TaskState.DONE |
| 121 | + |
| 122 | + |
| 123 | +async def test_task_client_get_task_result(monkeypatch): |
| 124 | + # Given |
| 125 | + datashare_url = "http://some-url" |
| 126 | + api_key = "some-api-key" |
| 127 | + task_name = "hello" |
| 128 | + task_id = f"{task_name}-{uuid.uuid4()}" |
| 129 | + |
| 130 | + @asynccontextmanager |
| 131 | + async def _get_and_assert( |
| 132 | + _, url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Any |
| 133 | + ): |
| 134 | + assert url == f"/api/task/{task_id}/results" |
| 135 | + assert allow_redirects |
| 136 | + assert not kwargs |
| 137 | + mocked_res = AsyncMock() |
| 138 | + mocked_res.json.return_value = "hellow world" |
| 139 | + yield mocked_res |
| 140 | + |
| 141 | + monkeypatch.setattr("icij_worker.utils.http.AiohttpClient._get", _get_and_assert) |
| 142 | + |
| 143 | + task_client = DatashareTaskClient(datashare_url, api_key=api_key) |
| 144 | + async with task_client: |
| 145 | + # When |
| 146 | + res = await task_client.get_task_result(task_id) |
| 147 | + assert res == "hellow world" |
| 148 | + |
| 149 | + |
| 150 | +async def test_task_client_get_task_error(monkeypatch): |
| 151 | + # Given |
| 152 | + datashare_url = "http://some-url" |
| 153 | + api_key = "some-api-key" |
| 154 | + task_name = "hello" |
| 155 | + task_id = f"{task_name}-{uuid.uuid4()}" |
| 156 | + |
| 157 | + @asynccontextmanager |
| 158 | + async def _get_and_assert( |
| 159 | + _, url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Any |
| 160 | + ): |
| 161 | + assert url == f"/api/task/{task_id}" |
| 162 | + task = { |
| 163 | + "@type": "Task", |
| 164 | + "id": task_id, |
| 165 | + "state": "ERROR", |
| 166 | + "createdAt": datetime.now(), |
| 167 | + "completedAt": datetime.now(), |
| 168 | + "name": "hello", |
| 169 | + "args": {"greeted": "world"}, |
| 170 | + "error": { |
| 171 | + "@type": "TaskError", |
| 172 | + "name": "SomeError", |
| 173 | + "message": "some error found", |
| 174 | + "cause": "i'm the culprit", |
| 175 | + "stacktrace": [{"lineno": 666, "file": "some_file.py", "name": "err"}], |
| 176 | + }, |
| 177 | + } |
| 178 | + assert allow_redirects |
| 179 | + assert not kwargs |
| 180 | + mocked_res = AsyncMock() |
| 181 | + mocked_res.json.return_value = task |
| 182 | + yield mocked_res |
| 183 | + |
| 184 | + monkeypatch.setattr("icij_worker.utils.http.AiohttpClient._get", _get_and_assert) |
| 185 | + |
| 186 | + task_client = DatashareTaskClient(datashare_url, api_key=api_key) |
| 187 | + async with task_client: |
| 188 | + # When |
| 189 | + error = await task_client.get_task_error(task_id) |
| 190 | + expected_error = TaskError( |
| 191 | + name="SomeError", |
| 192 | + message="some error found", |
| 193 | + cause="i'm the culprit", |
| 194 | + stacktrace=[StacktraceItem(name="err", file="some_file.py", lineno=666)], |
| 195 | + ) |
| 196 | + assert error == expected_error |
0 commit comments