|
| 1 | +"""Failure-path tests for CDA using mocked HTTP responses. |
| 2 | +
|
| 3 | +Cassette replay covers happy paths from real CDA responses. These |
| 4 | +tests cover failure paths cassettes cannot easily reach: server 5xx |
| 5 | +errors. They run on the unit tier because the HTTP behaviour is |
| 6 | +fully controlled by the mock (no network). |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import unittest |
| 12 | +from datetime import datetime, timezone |
| 13 | +from unittest.mock import MagicMock, patch |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +import speasy as spz |
| 18 | +from speasy.data_providers.cda import CdaWebException |
| 19 | + |
| 20 | +pytestmark = pytest.mark.unit |
| 21 | + |
| 22 | + |
| 23 | +def _fake_http_response(status: int, body: bytes = b'{"Message": ["Internal error"]}') -> MagicMock: |
| 24 | + """Build a minimal stand-in for the speasy ``Response`` returned by |
| 25 | + ``speasy.core.http.get`` so the CDA wrapper sees a 5xx with a JSON |
| 26 | + body it can parse.""" |
| 27 | + resp = MagicMock() |
| 28 | + resp.status_code = status |
| 29 | + resp.ok = False |
| 30 | + resp.url = "https://cdaweb.gsfc.nasa.gov/WS/cdasr/1/fake" |
| 31 | + resp.json.return_value = {"Message": ["Internal error"]} |
| 32 | + resp.bytes = body |
| 33 | + resp.text = body.decode() |
| 34 | + return resp |
| 35 | + |
| 36 | + |
| 37 | +class CdaFailureModes(unittest.TestCase): |
| 38 | + |
| 39 | + def test_get_variable_propagates_server_500(self) -> None: |
| 40 | + """A 500 from CDA must surface as ``CdaWebException`` so callers can |
| 41 | + distinguish a server error from a legitimate "no data" response.""" |
| 42 | + with patch( |
| 43 | + "speasy.data_providers.cda.http.get", |
| 44 | + return_value=_fake_http_response(500), |
| 45 | + ): |
| 46 | + with self.assertRaises(CdaWebException): |
| 47 | + spz.cda.get_variable( |
| 48 | + dataset="THA_L2_FGM", |
| 49 | + variable="tha_fgl_gsm", |
| 50 | + start_time=datetime(2014, 6, 1, tzinfo=timezone.utc), |
| 51 | + stop_time=datetime(2014, 6, 1, 0, 5, tzinfo=timezone.utc), |
| 52 | + disable_proxy=True, |
| 53 | + disable_cache=True, |
| 54 | + method="API", |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + unittest.main() |
0 commit comments