From cf7799022160d84237f6ca6fe56c410456eb3a10 Mon Sep 17 00:00:00 2001 From: Chuck Daniels Date: Tue, 5 Nov 2024 11:36:05 -0500 Subject: [PATCH] Change pqdm download tests to integration tests --- .github/workflows/test.yml | 2 +- scripts/integration-test.sh | 2 +- tests/integration/test_api.py | 46 ++++++++++++++++++++++++++++++++ tests/unit/test_api.py | 50 ----------------------------------- 4 files changed, 48 insertions(+), 52 deletions(-) delete mode 100644 tests/unit/test_api.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1d6250d4..cd7b6cd3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,7 +27,7 @@ jobs: run: mypy - name: Test - run: pytest tests/unit --cov=earthaccess --cov=tests --cov-report=term-missing --capture=no --tb=native --log-cli-level=INFO + run: pytest tests/unit --verbose --cov=earthaccess --cov-report=term-missing --capture=no --tb=native --log-cli-level=INFO - name: Upload coverage # Don't upload coverage when using the `act` tool to run the workflow locally diff --git a/scripts/integration-test.sh b/scripts/integration-test.sh index 506976ad..1a8ad4f6 100755 --- a/scripts/integration-test.sh +++ b/scripts/integration-test.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -x -pytest tests/integration --cov=earthaccess --cov=tests/integration --cov-report=term-missing "${@}" --capture=no --tb=native --log-cli-level=INFO +pytest tests/integration --cov=earthaccess --cov-report=term-missing "${@}" --capture=no --tb=native --log-cli-level=INFO RET=$? set +x diff --git a/tests/integration/test_api.py b/tests/integration/test_api.py index f0fdd219..f6ec1fcd 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -1,6 +1,7 @@ import logging import os from pathlib import Path +from unittest.mock import patch import earthaccess import pytest @@ -77,6 +78,51 @@ def test_download(tmp_path, selection, use_url): assert all(Path(f).exists() for f in files) +def fail_to_download_file(*args, **kwargs): + raise IOError("Download failed") + + +def test_download_immediate_failure(tmp_path: Path): + results = earthaccess.search_data( + short_name="ATL06", + bounding_box=(-10, 20, 10, 50), + temporal=("1999-02", "2019-03"), + count=3, + ) + + with patch.object(earthaccess.__store__, "_download_file", fail_to_download_file): + with pytest.raises(IOError, match="Download failed"): + # By default, we set pqdm exception_behavior to "immediate" so that + # it simply propagates the first download error it encounters, halting + # any further downloads. + earthaccess.download(results, tmp_path, pqdm_kwargs=dict(disable=True)) + + +def test_download_deferred_failure(tmp_path: Path): + count = 3 + results = earthaccess.search_data( + short_name="ATL06", + bounding_box=(-10, 20, 10, 50), + temporal=("1999-02", "2019-03"), + count=count, + ) + + with patch.object(earthaccess.__store__, "_download_file", fail_to_download_file): + # With "deferred" exceptions, pqdm catches all exceptions, then at the end + # raises a single generic Exception, passing the sequence of caught exceptions + # as arguments to the Exception constructor. + with pytest.raises(Exception) as exc_info: + earthaccess.download( + results, + tmp_path, + pqdm_kwargs=dict(exception_behaviour="deferred", disable=True), + ) + + errors = exc_info.value.args + assert len(errors) == count + assert all(isinstance(e, IOError) and str(e) == "Download failed" for e in errors) + + def test_auth_environ(): earthaccess.login(strategy="environment") environ = earthaccess.auth_environ() diff --git a/tests/unit/test_api.py b/tests/unit/test_api.py deleted file mode 100644 index 0f50fe93..00000000 --- a/tests/unit/test_api.py +++ /dev/null @@ -1,50 +0,0 @@ -from pathlib import Path -from unittest.mock import patch - -import earthaccess -import pytest - - -def fail_to_download_file(*args, **kwargs): - raise IOError("Download failed") - - -def test_download_immediate_failure(tmp_path: Path): - earthaccess.login() - - results = earthaccess.search_data( - short_name="ATL06", - bounding_box=(-10, 20, 10, 50), - temporal=("1999-02", "2019-03"), - count=10, - ) - - with patch.object(earthaccess.__store__, "_download_file", fail_to_download_file): - with pytest.raises(IOError, match="Download failed"): - earthaccess.download(results, str(tmp_path)) - - -def test_download_deferred_failure(tmp_path: Path): - earthaccess.login() - - count = 3 - results = earthaccess.search_data( - short_name="ATL06", - bounding_box=(-10, 20, 10, 50), - temporal=("1999-02", "2019-03"), - count=count, - ) - - with patch.object(earthaccess.__store__, "_download_file", fail_to_download_file): - with pytest.raises(Exception) as exc_info: - earthaccess.download( - results, - tmp_path, - None, - 8, - pqdm_kwargs={"exception_behaviour": "deferred"}, - ) - - errors = exc_info.value.args - assert len(errors) == count - assert all(isinstance(e, IOError) and str(e) == "Download failed" for e in errors)