From 04dc7bfcd0a4bb28c3d3e347fa3179fdbe88c3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Tue, 8 Oct 2024 11:49:55 +0200 Subject: [PATCH] tests: introduce ETagNotInResponseHeaderWarning to reduce warnings --- dclab/http_utils.py | 7 ++++++- tests/test_rtdc_fmt_http_basin.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/dclab/http_utils.py b/dclab/http_utils.py index 229fe74e..89a0178c 100644 --- a/dclab/http_utils.py +++ b/dclab/http_utils.py @@ -27,6 +27,10 @@ ) +class ETagNotInResponseHeaderWarning(UserWarning): + """Used for cases where the requests.Response does not contain an ETag""" + + class HTTPFile(io.IOBase): def __init__(self, url, chunk_size=2**18, keep_chunks=200): """Chunk-cached access to a URL supporting range requests @@ -78,7 +82,8 @@ def _parse_header(self): etag = resp.headers.get("etag", "").strip("'").strip('"') if len(etag) < 5: etag = None - warnings.warn(f"Got empty ETag header for {self.url}") + warnings.warn(f"Got empty ETag header for {self.url}", + ETagNotInResponseHeaderWarning) self._etag = etag @property diff --git a/tests/test_rtdc_fmt_http_basin.py b/tests/test_rtdc_fmt_http_basin.py index 118834b9..be11d05e 100644 --- a/tests/test_rtdc_fmt_http_basin.py +++ b/tests/test_rtdc_fmt_http_basin.py @@ -7,6 +7,7 @@ import pytest import dclab +from dclab.http_utils import ETagNotInResponseHeaderWarning from dclab import new_dataset, RTDCWriter from dclab.rtdc_dataset.fmt_http import HTTPBasin, RTDC_HTTP from dclab.rtdc_dataset.feat_basin import BasinNotAvailableError @@ -271,8 +272,12 @@ def test_trace_availability_invalid(tmp_path): # because `__contains__` returns True for "trace", but the trace data # are nowhere to find. with (pytest.warns(UserWarning, match="but I cannot get its data"), + pytest.warns(ETagNotInResponseHeaderWarning, + match="Got empty ETag header"), pytest.raises(KeyError, match="trace")): _ = ds["trace"] with (pytest.warns(UserWarning, match="but I cannot get its data"), + pytest.warns(ETagNotInResponseHeaderWarning, + match="Got empty ETag header"), pytest.raises(KeyError, match="trace")): dclab.new_dataset(ds)