Skip to content

Commit

Permalink
Merge pull request #468 from FAIRmat-NFDI/467-allow-kwargs-in-readertest
Browse files Browse the repository at this point in the history
Allow kwargs in ReaderTest
  • Loading branch information
lukaspie authored Nov 22, 2024
2 parents 7af2e03 + b17f00b commit 8d9931c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
14 changes: 11 additions & 3 deletions src/pynxtools/testing/nexus_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
from glob import glob
from typing import Literal, List, Tuple
from typing import List, Literal, Tuple

try:
from nomad.client import parse
Expand All @@ -15,8 +15,8 @@

from pynxtools.dataconverter.convert import get_reader, transfer_data_into_template
from pynxtools.dataconverter.helpers import (
get_nxdl_root_and_path,
add_default_root_attributes,
get_nxdl_root_and_path,
)
from pynxtools.dataconverter.validation import validate_dict_against
from pynxtools.dataconverter.writer import Writer
Expand All @@ -42,7 +42,9 @@ def get_log_file(nxs_file, log_file, tmp_path):
class ReaderTest:
"""Generic test for reader plugins."""

def __init__(self, nxdl, reader_name, files_or_dir, tmp_path, caplog) -> None:
def __init__(
self, nxdl, reader_name, files_or_dir, tmp_path, caplog, **kwargs
) -> None:
"""Initialize the test object.
Parameters
Expand All @@ -61,16 +63,20 @@ def __init__(self, nxdl, reader_name, files_or_dir, tmp_path, caplog) -> None:
Pytest fixture variable, used to clean up the files generated during the test.
caplog : _pytest.logging.LogCaptureFixture
Pytest fixture variable, used to capture the log messages during the test.
kwargs : Dict[str, Any]
Any additional keyword arguments to be passed to the readers' read function.
"""

self.nxdl = nxdl
self.reader_name = reader_name
self.reader = get_reader(self.reader_name)

self.files_or_dir = files_or_dir
self.ref_nexus_file = ""
self.tmp_path = tmp_path
self.caplog = caplog
self.created_nexus = f"{tmp_path}/{os.sep}/output.nxs"
self.kwargs = kwargs

def convert_to_nexus(
self,
Expand All @@ -94,6 +100,7 @@ def convert_to_nexus(
]
input_files = [file for file in example_files if not file.endswith(".nxs")]
assert self.ref_nexus_file, "Reference nexus (.nxs) file not found"

assert (
self.nxdl in self.reader.supported_nxdls
), f"Reader does not support {self.nxdl} NXDL."
Expand All @@ -107,6 +114,7 @@ def convert_to_nexus(
nxdl_name=self.nxdl,
nxdl_root=nxdl_root,
skip_verify=True,
**self.kwargs,
)

# Clear the log of `transfer_data_into_template`
Expand Down
22 changes: 6 additions & 16 deletions tests/dataconverter/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from pynxtools.dataconverter.convert import get_names_of_all_readers, get_reader
from pynxtools.dataconverter.helpers import generate_template_from_nxdl
from pynxtools.dataconverter.readers.base.reader import BaseReader
from pynxtools.dataconverter.readers.multi.reader import MultiFormatReader
from pynxtools.dataconverter.template import Template
from pynxtools.dataconverter.validation import validate_dict_against

Expand All @@ -51,30 +52,19 @@ def get_all_readers() -> List[ParameterSet]:
"""Scans through the reader list and returns them for pytest parametrization"""
readers = []

# Explicitly removing ApmReader and EmNionReader because we need to add test data
for reader in [get_reader(x) for x in get_names_of_all_readers()]:
if reader.__name__ in (
"ApmReader",
"EmOmReader",
"EmSpctrscpyReader",
"EmNionReader",
):
readers.append(
pytest.param(
reader, marks=pytest.mark.skip(reason="Missing test data.")
)
)
else:
readers.append(pytest.param(reader))
readers.append(pytest.param(reader))

return readers


@pytest.mark.parametrize("reader", get_all_readers())
def test_if_readers_are_children_of_base_reader(reader):
"""Test to verify that all readers are children of BaseReader"""
"""Test to verify that all readers are children of BaseReader or MultiFormatReader"""
if reader.__name__ != "BaseReader":
assert isinstance(reader(), BaseReader)
assert isinstance(reader(), BaseReader) or isinstance(
reader(), MultiFormatReader
)


@pytest.mark.parametrize("reader", get_all_readers())
Expand Down

0 comments on commit 8d9931c

Please sign in to comment.