Skip to content

Commit

Permalink
Final checks and added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto committed Jun 28, 2024
1 parent d4b6033 commit cef05c4
Show file tree
Hide file tree
Showing 9 changed files with 46,767 additions and 33 deletions.
14 changes: 0 additions & 14 deletions src/pysdmx/io/xml/sdmx21/reader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""SDMX 2.1 reader package."""

from pathlib import Path
from typing import Any, Dict, Optional

import xmltodict
Expand Down Expand Up @@ -128,16 +127,3 @@ def __parse_dataset(message_info: Dict[str, Any], mode: str) -> Dict[str, Any]:
ds = create_dataset(dataset, str_info, mode)
datasets[ds.unique_id] = ds
return datasets


if __name__ == "__main__":
basepath = Path(__file__).parent / "gen_ser.xml"
with open(basepath, "r") as f:
content = f.read()
result = read_xml(content)
dataset = result["BIS:BIS_DER(1.0)"]

print(dataset.unique_id)
print(dataset.structure_type)
print(dataset.attached_attributes)
print(dataset.data)
20 changes: 1 addition & 19 deletions src/pysdmx/util/handlers.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
"""Handlers file provide functions to make the code more readable."""

from typing import Any, Dict
from typing import Any


# import pandas as pd


def first_element_dict(obj_: Dict[int, Any]) -> None:
"""First element dict.
Args:
obj_: An object
Returns:
dict: A dict with the first element.
"""
if len(obj_) != 0:
values_view = obj_.values()
value_iterator = iter(values_view)
first_value = next(value_iterator)
return first_value
else:
return None


def split_unique_id(obj_: str) -> tuple[str, str, str]:
"""Split unique id.
Expand Down
4,031 changes: 4,031 additions & 0 deletions tests/io/xml/sdmx21/reader/samples/dataflow.xml

Large diffs are not rendered by default.

26,026 changes: 26,026 additions & 0 deletions tests/io/xml/sdmx21/reader/samples/gen_all.xml

Large diffs are not rendered by default.

8,950 changes: 8,950 additions & 0 deletions tests/io/xml/sdmx21/reader/samples/gen_ser.xml

Large diffs are not rendered by default.

4,025 changes: 4,025 additions & 0 deletions tests/io/xml/sdmx21/reader/samples/str_all.xml

Large diffs are not rendered by default.

1,193 changes: 1,193 additions & 0 deletions tests/io/xml/sdmx21/reader/samples/str_ser.xml

Large diffs are not rendered by default.

2,489 changes: 2,489 additions & 0 deletions tests/io/xml/sdmx21/reader/samples/str_ser_group.xml

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions tests/io/xml/sdmx21/reader/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def submission_path():
return Path(__file__).parent / "samples" / "submission_append.xml"


@pytest.fixture()
def samples_folder():
return Path(__file__).parent / "samples"


@pytest.fixture()
def error_304_path():
return Path(__file__).parent / "samples" / "error_304.xml"
Expand Down Expand Up @@ -66,3 +71,50 @@ def test_error_message_with_different_mode(error_304_path):
assert filetype == "xml"
with pytest.raises(ValueError, match="Unable to parse sdmx file as"):
read_xml(input_str, validate=True, mode=MessageType.Submission)


@pytest.mark.parametrize(
"filename",
[
"gen_all.xml",
"gen_ser.xml",
"str_all.xml",
"str_ser.xml",
"str_ser_group.xml",
],
)
def test_reading_validation(samples_folder, filename):
data_path = samples_folder / filename
input_str, filetype = process_string_to_read(data_path)
assert filetype == "xml"
result = read_xml(input_str, validate=True)
assert result is not None
data = result["BIS:BIS_DER(1.0)"].data
num_rows = len(data)
num_columns = data.shape[1]
assert num_rows > 0
assert num_columns > 0
expected_num_rows = 1000
expected_num_columns = 20
assert num_rows == expected_num_rows
assert num_columns == expected_num_columns


# Test reading of dataflow SDMX file
def test_dataflow(samples_folder):
data_path = samples_folder / "dataflow.xml"
input_str, filetype = process_string_to_read(data_path)
assert filetype == "xml"
result = read_xml(input_str, validate=True)
data_dataflow = result["BIS:WEBSTATS_DER_DATAFLOW(1.0)"].data
num_rows = len(data_dataflow)
num_columns = data_dataflow.shape[1]
assert num_rows > 0
assert num_columns > 0
expected_num_rows = 1000
expected_num_columns = 20
assert num_rows == expected_num_rows
assert num_columns == expected_num_columns
assert "BIS:WEBSTATS_DER_DATAFLOW(1.0)" in result
assert "AVAILABILITY" in data_dataflow.columns
assert "DER_CURR_LEG1" in data_dataflow.columns

0 comments on commit cef05c4

Please sign in to comment.