From c0eca091a2ae3055e5b910aa19834ec17b78b8af Mon Sep 17 00:00:00 2001 From: malmans2 Date: Fri, 18 Oct 2024 12:55:50 +0200 Subject: [PATCH 1/8] add results test --- tests/test_20_results.py | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/test_20_results.py diff --git a/tests/test_20_results.py b/tests/test_20_results.py new file mode 100644 index 0000000..deed3b2 --- /dev/null +++ b/tests/test_20_results.py @@ -0,0 +1,78 @@ +import os +import pathlib + +import pytest +import responses + +from cads_api_client import Results + +RESULTS_URL = "http://localhost:8080/api/retrieve/v1/jobs/bcfc677f-2a4e-4e83-91da-7d1c0340f407/results" +RESULTS_JSON = { + "asset": { + "value": { + "type": "application/x-grib", + "href": "http://httpbin.org/bytes/1", + "file:size": 1, + } + } +} + + +@pytest.fixture +def results() -> Results: + with responses.RequestsMock() as rsps: + rsps.add( + responses.GET, + RESULTS_URL, + json=RESULTS_JSON, + status=200, + content_type="application/json", + ) + results = Results.from_request( + "get", + RESULTS_URL, + headers={}, + session=None, + retry_options={"maximum_tries": 1}, + request_options={}, + download_options={}, + sleep_max=120, + cleanup=False, + log_callback=None, + ) + return results + + +def test_results_download(results: Results, tmp_path: pathlib.Path) -> None: + expected = str(tmp_path / "test.grib") + actual = results.download(target=expected) + assert actual == expected + assert os.path.getsize(actual) == 1 + + +def test_results_assert(results: Results) -> None: + assert results.asset == { + "file:size": 1, + "href": "http://httpbin.org/bytes/1", + "type": "application/x-grib", + } + + +def test_results_content_length(results: Results) -> None: + assert results.content_length == 1 + + +def test_results_content_type(results: Results) -> None: + assert results.content_type == "application/x-grib" + + +def test_results_json(results: Results) -> None: + assert results.json == RESULTS_JSON + + +def test_results_location(results: Results) -> None: + assert results.location == "http://httpbin.org/bytes/1" + + +def test_results_url(results: Results) -> None: + assert results.url == RESULTS_URL From 5b797657ee1c65725ee63278a968c0fe157bd5a2 Mon Sep 17 00:00:00 2001 From: malmans2 Date: Fri, 18 Oct 2024 13:26:37 +0200 Subject: [PATCH 2/8] cleanup --- tests/integration_test_40_results.py | 19 ------- ...{test_20_results.py => test_40_results.py} | 56 ++++++++++++------- 2 files changed, 37 insertions(+), 38 deletions(-) rename tests/{test_20_results.py => test_40_results.py} (58%) diff --git a/tests/integration_test_40_results.py b/tests/integration_test_40_results.py index 13f5968..e5764ba 100644 --- a/tests/integration_test_40_results.py +++ b/tests/integration_test_40_results.py @@ -1,5 +1,4 @@ import contextlib -import os import pathlib import random from typing import Any @@ -17,24 +16,6 @@ def results(api_anon_client: ApiClient) -> Results: return api_anon_client.submit_and_wait_on_results("test-adaptor-dummy", size=1) -def test_results_asset(results: Results) -> None: - assert results.asset["type"] == "application/x-grib" - assert results.asset["file:size"] == 1 - - -@pytest.mark.parametrize("target", ("dummy.grib", None)) -def test_results_download( - monkeypatch: pytest.MonkeyPatch, - results: Results, - tmp_path: pathlib.Path, - target: str | None, -) -> None: - monkeypatch.chdir(tmp_path) - actual_target = results.download(target=target) - assert (actual_target != target) if target is None else (actual_target == target) - assert os.path.getsize(actual_target) == 1 - - @pytest.mark.parametrize("progress", [True, False]) def test_results_progress( api_root_url: str, diff --git a/tests/test_20_results.py b/tests/test_40_results.py similarity index 58% rename from tests/test_20_results.py rename to tests/test_40_results.py index deed3b2..6c985fc 100644 --- a/tests/test_20_results.py +++ b/tests/test_40_results.py @@ -7,24 +7,23 @@ from cads_api_client import Results RESULTS_URL = "http://localhost:8080/api/retrieve/v1/jobs/bcfc677f-2a4e-4e83-91da-7d1c0340f407/results" -RESULTS_JSON = { - "asset": { - "value": { - "type": "application/x-grib", - "href": "http://httpbin.org/bytes/1", - "file:size": 1, - } - } -} -@pytest.fixture -def results() -> Results: +def make_results(size: int = 1) -> Results: + results_json = { + "asset": { + "value": { + "type": "application/x-grib", + "href": f"http://httpbin.org/bytes/{size}", + "file:size": size, + } + } + } with responses.RequestsMock() as rsps: rsps.add( responses.GET, RESULTS_URL, - json=RESULTS_JSON, + json=results_json, status=200, content_type="application/json", ) @@ -43,14 +42,25 @@ def results() -> Results: return results -def test_results_download(results: Results, tmp_path: pathlib.Path) -> None: - expected = str(tmp_path / "test.grib") - actual = results.download(target=expected) - assert actual == expected - assert os.path.getsize(actual) == 1 +@pytest.fixture +def results() -> Results: + return make_results() + +@pytest.mark.parametrize("target", ("dummy.grib", None)) +def test_results_download( + monkeypatch: pytest.MonkeyPatch, + results: Results, + tmp_path: pathlib.Path, + target: str | None, +) -> None: + monkeypatch.chdir(tmp_path) + actual_target = results.download(target=target) + assert (actual_target != target) if target is None else (actual_target == target) + assert os.path.getsize(actual_target) == 1 -def test_results_assert(results: Results) -> None: + +def test_results_asset(results: Results) -> None: assert results.asset == { "file:size": 1, "href": "http://httpbin.org/bytes/1", @@ -67,7 +77,15 @@ def test_results_content_type(results: Results) -> None: def test_results_json(results: Results) -> None: - assert results.json == RESULTS_JSON + assert results.json == { + "asset": { + "value": { + "type": "application/x-grib", + "href": "http://httpbin.org/bytes/1", + "file:size": 1, + } + } + } def test_results_location(results: Results) -> None: From feaac5bb9b43c1ba8db6d254f051dd030fcd7240 Mon Sep 17 00:00:00 2001 From: malmans2 Date: Fri, 18 Oct 2024 13:30:03 +0200 Subject: [PATCH 3/8] restore integrations --- tests/integration_test_40_results.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/integration_test_40_results.py b/tests/integration_test_40_results.py index e5764ba..13f5968 100644 --- a/tests/integration_test_40_results.py +++ b/tests/integration_test_40_results.py @@ -1,4 +1,5 @@ import contextlib +import os import pathlib import random from typing import Any @@ -16,6 +17,24 @@ def results(api_anon_client: ApiClient) -> Results: return api_anon_client.submit_and_wait_on_results("test-adaptor-dummy", size=1) +def test_results_asset(results: Results) -> None: + assert results.asset["type"] == "application/x-grib" + assert results.asset["file:size"] == 1 + + +@pytest.mark.parametrize("target", ("dummy.grib", None)) +def test_results_download( + monkeypatch: pytest.MonkeyPatch, + results: Results, + tmp_path: pathlib.Path, + target: str | None, +) -> None: + monkeypatch.chdir(tmp_path) + actual_target = results.download(target=target) + assert (actual_target != target) if target is None else (actual_target == target) + assert os.path.getsize(actual_target) == 1 + + @pytest.mark.parametrize("progress", [True, False]) def test_results_progress( api_root_url: str, From 7a854f3f564caa28a72f6d14cc9225a6f1619818 Mon Sep 17 00:00:00 2001 From: malmans2 Date: Fri, 18 Oct 2024 13:33:16 +0200 Subject: [PATCH 4/8] improve tests --- tests/integration_test_40_results.py | 14 -------------- tests/test_40_results.py | 2 +- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/tests/integration_test_40_results.py b/tests/integration_test_40_results.py index 13f5968..49ba920 100644 --- a/tests/integration_test_40_results.py +++ b/tests/integration_test_40_results.py @@ -1,5 +1,4 @@ import contextlib -import os import pathlib import random from typing import Any @@ -22,19 +21,6 @@ def test_results_asset(results: Results) -> None: assert results.asset["file:size"] == 1 -@pytest.mark.parametrize("target", ("dummy.grib", None)) -def test_results_download( - monkeypatch: pytest.MonkeyPatch, - results: Results, - tmp_path: pathlib.Path, - target: str | None, -) -> None: - monkeypatch.chdir(tmp_path) - actual_target = results.download(target=target) - assert (actual_target != target) if target is None else (actual_target == target) - assert os.path.getsize(actual_target) == 1 - - @pytest.mark.parametrize("progress", [True, False]) def test_results_progress( api_root_url: str, diff --git a/tests/test_40_results.py b/tests/test_40_results.py index 6c985fc..18f885e 100644 --- a/tests/test_40_results.py +++ b/tests/test_40_results.py @@ -56,7 +56,7 @@ def test_results_download( ) -> None: monkeypatch.chdir(tmp_path) actual_target = results.download(target=target) - assert (actual_target != target) if target is None else (actual_target == target) + assert actual_target == target or "1" assert os.path.getsize(actual_target) == 1 From 7c6c80fb789d5a23fde01ff18c0db4055c1fd523 Mon Sep 17 00:00:00 2001 From: malmans2 Date: Fri, 18 Oct 2024 13:36:49 +0200 Subject: [PATCH 5/8] cleanup --- tests/test_40_results.py | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/tests/test_40_results.py b/tests/test_40_results.py index 18f885e..f41690f 100644 --- a/tests/test_40_results.py +++ b/tests/test_40_results.py @@ -6,24 +6,25 @@ from cads_api_client import Results -RESULTS_URL = "http://localhost:8080/api/retrieve/v1/jobs/bcfc677f-2a4e-4e83-91da-7d1c0340f407/results" - - -def make_results(size: int = 1) -> Results: - results_json = { - "asset": { - "value": { - "type": "application/x-grib", - "href": f"http://httpbin.org/bytes/{size}", - "file:size": size, - } +RESULTS_URL = "http://localhost:8080/api/retrieve/v1/jobs/9bfc1362-2832-48e1-a235-359267420bb2/results" +RESULTS_JSON = { + "asset": { + "value": { + "type": "application/x-grib", + "href": "http://httpbin.org/bytes/1", + "file:size": 1, } } +} + + +@pytest.fixture +def results() -> Results: with responses.RequestsMock() as rsps: rsps.add( responses.GET, RESULTS_URL, - json=results_json, + json=RESULTS_JSON, status=200, content_type="application/json", ) @@ -42,11 +43,6 @@ def make_results(size: int = 1) -> Results: return results -@pytest.fixture -def results() -> Results: - return make_results() - - @pytest.mark.parametrize("target", ("dummy.grib", None)) def test_results_download( monkeypatch: pytest.MonkeyPatch, @@ -77,15 +73,7 @@ def test_results_content_type(results: Results) -> None: def test_results_json(results: Results) -> None: - assert results.json == { - "asset": { - "value": { - "type": "application/x-grib", - "href": "http://httpbin.org/bytes/1", - "file:size": 1, - } - } - } + assert results.json == RESULTS_JSON def test_results_location(results: Results) -> None: From 3b9c54b237ac9769ad5166f89d17107c9fbdf6c3 Mon Sep 17 00:00:00 2001 From: malmans2 Date: Fri, 18 Oct 2024 13:38:59 +0200 Subject: [PATCH 6/8] fix typing --- tests/test_40_results.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_40_results.py b/tests/test_40_results.py index f41690f..365034a 100644 --- a/tests/test_40_results.py +++ b/tests/test_40_results.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import os import pathlib From 55cb9c51b02177658cbd447d7ed0428a860c95ab Mon Sep 17 00:00:00 2001 From: malmans2 Date: Fri, 18 Oct 2024 13:44:36 +0200 Subject: [PATCH 7/8] make sure future annotations are used --- pyproject.toml | 5 ++++- tests/integration_test_50_profile.py | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ad4c8b7..58d1886 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,7 @@ module = ["cdsapi.*", "multiurl.*", "py.*"] # Same as Black. indent-width = 4 line-length = 88 +target-version = "py38" [tool.ruff.lint] ignore = [ @@ -53,7 +54,9 @@ select = [ # isort "I", # pydocstyle - "D" + "D", + # flake8-future-annotations + "FA" ] [tool.ruff.lint.pycodestyle] diff --git a/tests/integration_test_50_profile.py b/tests/integration_test_50_profile.py index 264bd6b..5ebafa9 100644 --- a/tests/integration_test_50_profile.py +++ b/tests/integration_test_50_profile.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import Literal import pytest From b0c524be73bc95b70b811c3dc9b8a12988302abd Mon Sep 17 00:00:00 2001 From: malmans2 Date: Fri, 18 Oct 2024 13:48:04 +0200 Subject: [PATCH 8/8] cleanup --- tests/test_40_results.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/test_40_results.py b/tests/test_40_results.py index 365034a..a04ca20 100644 --- a/tests/test_40_results.py +++ b/tests/test_40_results.py @@ -45,17 +45,24 @@ def results() -> Results: return results -@pytest.mark.parametrize("target", ("dummy.grib", None)) +@pytest.mark.parametrize( + "target,expected", + [ + ("dummy.grib", "dummy.grib"), + (None, "1"), + ], +) def test_results_download( monkeypatch: pytest.MonkeyPatch, results: Results, tmp_path: pathlib.Path, target: str | None, + expected: str, ) -> None: monkeypatch.chdir(tmp_path) - actual_target = results.download(target=target) - assert actual_target == target or "1" - assert os.path.getsize(actual_target) == 1 + actual = results.download(target=target) + assert actual == expected + assert os.path.getsize(actual) == 1 def test_results_asset(results: Results) -> None: