Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use log files in testing #375

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/plugin_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
branch: main
tests_to_run: tests/.
- plugin: pynxtools-xps
branch: main
branch: adapt-testing
tests_to_run: tests/.
# - plugin: pynxtools-apm
# branch: main
Expand Down
67 changes: 44 additions & 23 deletions src/pynxtools/testing/nexus_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from glob import glob
from typing import Literal

# from click.testing import CliRunner

try:
from nomad.client import parse

Expand Down Expand Up @@ -39,7 +41,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, ref_log_file=None
) -> None:
"""Initialize the test object.

Parameters
Expand All @@ -51,22 +55,24 @@ def __init__(self, nxdl, reader_name, files_or_dir, tmp_path, caplog) -> None:
files_or_dir : str
List of input files or full path string to the example data directory that contains all the files
required for running the data conversion through the reader.
ref_nexus_file : str
Full path string to the reference NeXus file generated from the same
set of input files.
tmp_path : pathlib.PosixPath
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.
ref_log_file : str
Full path string to the reference log file generated from the same
set of input files in files_or_dir. This can also be parsed automatically if files_or_dir
is the full path string to the example data directory and there is only one reference
log file.
"""

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.ref_log_file = ref_log_file
self.created_nexus = f"{tmp_path}/{os.sep}/output.nxs"

def convert_to_nexus(
Expand All @@ -86,11 +92,17 @@ def convert_to_nexus(
example_files = self.files_or_dir
else:
example_files = sorted(glob(os.path.join(self.files_or_dir, "*")))
self.ref_nexus_file = [file for file in example_files if file.endswith(".nxs")][
0

if not self.ref_log_file:
self.ref_log_file = [
file for file in example_files if file.endswith(".log")
][0]
lukaspie marked this conversation as resolved.
Show resolved Hide resolved
assert self.ref_log_file, "Reference nexus .log file not found"

input_files = [
file for file in example_files if not file.endswith((".nxs", ".log"))
]
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 @@ -117,27 +129,23 @@ def convert_to_nexus(

Writer(read_data, nxdl_file, self.created_nexus).write()

if NOMAD_AVAILABLE:
kwargs = dict(
strict=True,
parser_name=None,
server_context=False,
username=None,
password=None,
)

parse(self.created_nexus, **kwargs)
# This shall be reactivated once verify_nexus works!
# # def test_verify_nexus(
# self,
# caplog_level: Literal["ERROR", "WARNING"] = "ERROR",
# ):
# with self.caplog.at_level(caplog_level):
# _ = CliRunner().invoke(verify, [str(self.created_nexus)])

def check_reproducibility_of_nexus(self):
"""Reproducibility test for the generated nexus file."""
IGNORE_LINES = [
"DEBUG - value: v",
"DEBUG - value: https://github.com/FAIRmat-NFDI/nexus_definitions/blob/",
]
ref_log = get_log_file(self.ref_nexus_file, "ref_nexus.log", self.tmp_path)
gen_log = get_log_file(self.created_nexus, "gen_nexus.log", self.tmp_path)
with open(gen_log, "r", encoding="utf-8") as gen, open(
ref_log, "r", encoding="utf-8"
gen_log_file = get_log_file(self.created_nexus, "gen_nexus.log", self.tmp_path)
with open(gen_log_file, "r", encoding="utf-8") as gen, open(
self.ref_log_file, "r", encoding="utf-8"
) as ref:
gen_lines = gen.readlines()
ref_lines = ref.readlines()
Expand All @@ -154,3 +162,16 @@ def check_reproducibility_of_nexus(self):
f"Log files are different at line {ind}"
f" generated: {gen_l} \n referenced : {ref_l}"
)

def test_parse_nomad(self):
"""Check if the generated nexus file can be parse by NOMAD."""
if NOMAD_AVAILABLE:
kwargs = dict(
strict=True,
parser_name=None,
server_context=False,
username=None,
password=None,
)

parse(self.created_nexus, **kwargs)
Loading