Skip to content

Commit

Permalink
Update tests for #218
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Jan 13, 2025
1 parent d823546 commit 83cda00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions sdmx/tests/format/test_format_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,19 @@ def test_validate_xml_invalid_doc(tmp_path, installed_schemas):

msg_path.write_bytes(sdmx.to_xml(msg))

# Expect validation to fail
assert not sdmx.validate_xml(msg_path, schema_dir=installed_schemas.joinpath("2.1"))
assert sdmx.validate_xml(msg_path, schema_dir=installed_schemas.joinpath("2.1"))


def test_validate_xml_invalid_message_type(installed_schemas):
"""Ensure that an invalid document fails validation."""
# Create a mangled structure message with its outmost tag changed to be invalid
msg = StructureMessage()
invalid_msg = re.sub(b"mes:Structure([ >])", rb"mes:FooBar\1", sdmx.to_xml(msg))
invalid_msg = io.BytesIO(
re.sub(b"mes:Structure([ >])", rb"mes:FooBar\1", sdmx.to_xml(msg))
)

with pytest.raises(NotImplementedError, match="Validate non-SDMX root.*FooBar>"):
sdmx.validate_xml(io.BytesIO(invalid_msg), installed_schemas)
sdmx.validate_xml(invalid_msg, installed_schemas)


@pytest.mark.parametrize("version", ["1", 1, None])
Expand All @@ -157,6 +158,23 @@ def test_validate_xml_invalid_version(version):
sdmx.validate_xml("samples/common/common.xml", version=version)


def test_validate_xml_max_errors(caplog, installed_schemas):
"""Test :py:`validate_xml(..., max_errors=...)`."""
msg = StructureMessage()
invalid_msg = io.BytesIO(
re.sub(b"<(mes:Structures)/>", rb"<\1><Foo/></\1><Bar/>", sdmx.to_xml(msg))
)

# Without max_errors, 2 messages are logged
sdmx.validate_xml(invalid_msg, installed_schemas)
assert 2 == len(caplog.messages)
caplog.clear()

# With the argument, only 1 message is logged
sdmx.validate_xml(invalid_msg, installed_schemas, max_errors=1)
assert 1 == len(caplog.messages)


def test_validate_xml_no_schemas(tmp_path, specimen):
"""Check that supplying an invalid schema path will raise ``ValueError``."""
with specimen("IPI-2010-A21-structure.xml", opened=False) as msg_path:
Expand Down
2 changes: 1 addition & 1 deletion sdmx/tests/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_get_source(caplog):
def test_list_sources():
source_ids = list_sources()
# Correct number of sources, excluding those created for testing
assert 29 == len(set(source_ids) - {"MOCK", "TEST"})
assert 34 == len(set(source_ids) - {"MOCK", "TEST"})

# Listed alphabetically
assert "ABS" == source_ids[0]
Expand Down

0 comments on commit 83cda00

Please sign in to comment.