Skip to content

Commit

Permalink
Adjust tests for #212
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Dec 16, 2024
1 parent fa6c302 commit d86a5d7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
3 changes: 2 additions & 1 deletion sdmx/tests/model/test_v21.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from operator import attrgetter
from typing import cast

import pytest

Expand Down Expand Up @@ -589,7 +590,7 @@ class TestMetadataSet:
@pytest.fixture(scope="class")
def msg(self, specimen) -> sdmx.message.MetadataMessage:
with specimen("esms_generic.xml") as f:
return sdmx.read_sdmx(f)
return cast(sdmx.message.MetadataMessage, sdmx.read_sdmx(f))

def test_report_hierarchy(self, msg: sdmx.message.MetadataMessage) -> None:
# Access message → metadata set → report
Expand Down
10 changes: 6 additions & 4 deletions sdmx/tests/reader/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class cls(BaseReader):
MediaType("", "xml", "2.1", Flag.data, full="application/foo"),
]

def read_message(self, source, dsd=None):
def convert(self, source, dsd=None):
pass # pragma: no cover

return cls
Expand All @@ -32,14 +32,16 @@ def test_deprecated_kwarg(self):
),
pytest.raises(ValueError, match="Mismatched structure=FOO, dsd=BAR"),
):
r.read_message(None, structure=dsd0, dsd=dsd1)
r.convert(None, structure=dsd0, dsd=dsd1)

def test_detect(self, MinimalReader):
assert False is MinimalReader.detect(b"foo")
with pytest.warns(DeprecationWarning, match="use Converter.handles"):
assert False is MinimalReader.detect(b"foo")

def test_handles_media_type(self, caplog, MinimalReader):
""":meth:`.handles_media_type` matches even when params differ, but logs."""
assert True is MinimalReader.handles_media_type("application/foo; bar=qux")
with pytest.warns(DeprecationWarning, match="use Converter.handles"):
assert True is MinimalReader.handles_media_type("application/foo; bar=qux")
assert (
"Match application/foo with params {'bar': 'qux'}; "
"expected {'version': '2.1'}" in caplog.messages
Expand Down
12 changes: 7 additions & 5 deletions sdmx/tests/reader/test_reader_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class TestReader:
],
)
def test_handles_media_type(self, mt, expected) -> None:
assert expected is Reader.handles_media_type(mt)
with pytest.warns(DeprecationWarning, match="use Converter.handles"):
assert expected is Reader.handles_media_type(mt)

@pytest.mark.parametrize(
"content, exc_text",
Expand All @@ -77,11 +78,12 @@ def test_handles_media_type(self, mt, expected) -> None:
)
def test_inspect_header0(self, content, exc_text) -> None:
with pytest.raises(ValueError, match=f"Invalid SDMX-CSV 2.0.0: {exc_text}"):
Reader().read_message(BytesIO(content))
Reader().convert(BytesIO(content))

@pytest.mark.parametrize("value, expected", [(".csv", True), (".xlsx", False)])
def test_supports_suffix(self, value, expected) -> None:
assert expected is Reader.supports_suffix(value)
with pytest.warns(DeprecationWarning, match="use Converter.handles"):
assert expected is Reader.supports_suffix(value)


@lru_cache
Expand Down Expand Up @@ -110,7 +112,7 @@ def get_dfd(n_measure: int = 1) -> "v30.Dataflow":


@pytest.mark.parametrize_specimens("path", format="csv")
def test_read_specimen(path):
def test_read_specimen(path) -> None:
"""Test that the samples from the SDMX-CSV spec can be read."""
import sdmx

Expand All @@ -119,7 +121,7 @@ def test_read_specimen(path):
else:
dfd = get_dfd()

kwargs = dict(structure=dfd)
kwargs: dict = dict(structure=dfd)

if path.stem == "example-04":
kwargs.update(delimiter=";")
Expand Down
12 changes: 7 additions & 5 deletions sdmx/tests/reader/test_reader_xml_v21.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from lxml import etree

import sdmx
import sdmx.message
from sdmx import urn
from sdmx.format.xml.v21 import qname
from sdmx.model import common, v21
Expand Down Expand Up @@ -164,11 +165,11 @@ def test_gh_159():
)

# - Create a reader
# - Convert to a file-like object compatible with read_message()
# - Convert to a file-like object compatible with convert()
# - Parse the element
# - Retrieve the resulting object
reader = Reader()
reader.read_message(BytesIO(etree.tostring(elem)))
reader.convert(BytesIO(etree.tostring(elem)))
obj = reader.pop_single(common.Agency)

assert "Foo Agency" == str(obj.name)
Expand Down Expand Up @@ -258,6 +259,7 @@ def test_gh_205(caplog, specimen) -> None:
"""Test of https://github.com/khaeru/sdmx/issues/205."""
with specimen("INSEE/gh-205.xml") as f:
msg = sdmx.read_sdmx(f)
assert isinstance(msg, sdmx.message.StructureMessage)

# Messages were logged
msg_template = "Could not resolve {cls}.concept_identity reference to ConceptScheme=FR1:CONCEPTS_INSEE(1.0) → Concept={id}"
Expand Down Expand Up @@ -339,7 +341,7 @@ def test_parse_elem(elem, expected):
This method allows unit-level testing of specific XML elements appearing in SDMX-ML
messages. Add elements by extending the list passed to the parametrize() decorator.
"""
# Convert to a file-like object compatible with read_message()
# Convert to a file-like object compatible with convert()
tmp = BytesIO(etree.tostring(elem))

# Create a reader
Expand All @@ -348,10 +350,10 @@ def test_parse_elem(elem, expected):
if isinstance(expected, (str, re.Pattern)):
# Parsing the element raises an exception
with pytest.raises(XMLParseError, match=expected):
reader.read_message(tmp)
reader.convert(tmp)
else:
# The element is parsed successfully
result = reader.read_message(tmp)
result = reader.convert(tmp)

if not result:
stack = list(chain(*[s.values() for s in reader.stack.values()]))
Expand Down
6 changes: 1 addition & 5 deletions sdmx/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ def test_read_sdmx(tmp_path, specimen):

# Exception raised when the file contents don't allow to guess the format
bad_file = BytesIO(b"#! neither XML nor JSON")
exc = (
"cannot infer SDMX message format from path None, format={}, or content "
"'#! ne..'"
)
with pytest.raises(RuntimeError, match=exc.format("None")):
with pytest.raises(RuntimeError, match="cannot infer SDMX message format from "):
sdmx.read_sdmx(bad_file)

# Using the format= argument forces a certain reader to be used
Expand Down
12 changes: 8 additions & 4 deletions sdmx/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
],
)
def test_get_reader_for_media_type0(value):
with pytest.raises(
ValueError, match=re.escape(f"Media type {value!r} not supported by any of")
with (
pytest.raises(
ValueError, match=re.escape(f"Media type {value!r} not supported by any of")
),
pytest.warns(DeprecationWarning, match=r"use get_reader\(requests.Response"),
):
get_reader_for_media_type(value)

Expand All @@ -27,5 +30,6 @@ def test_get_reader_for_media_type0(value):
],
)
def test_get_reader_for_media_type1(value):
# Does not raise
get_reader_for_media_type(value)
with pytest.warns(DeprecationWarning, match=r"use get_reader\(requests.Response"):
# Does not raise
get_reader_for_media_type(value)

0 comments on commit d86a5d7

Please sign in to comment.