From 7efcc54acb5e82c68587fd4293e3d842fe917624 Mon Sep 17 00:00:00 2001 From: Lukas Pielsticker <50139597+lukaspie@users.noreply.github.com> Date: Mon, 8 Jul 2024 13:59:12 +0200 Subject: [PATCH 1/5] use log files in testing --- src/pynxtools/testing/nexus_conversion.py | 25 ++++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/pynxtools/testing/nexus_conversion.py b/src/pynxtools/testing/nexus_conversion.py index ca732c413..05220effa 100644 --- a/src/pynxtools/testing/nexus_conversion.py +++ b/src/pynxtools/testing/nexus_conversion.py @@ -39,7 +39,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, ref_log_file, tmp_path, caplog + ) -> None: """Initialize the test object. Parameters @@ -51,8 +53,8 @@ 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 + ref_log_file : str + Full path string to the reference log 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. @@ -64,7 +66,7 @@ def __init__(self, nxdl, reader_name, files_or_dir, tmp_path, caplog) -> None: self.reader_name = reader_name self.reader = get_reader(self.reader_name) self.files_or_dir = files_or_dir - self.ref_nexus_file = "" + self.ref_log_file = "" self.tmp_path = tmp_path self.caplog = caplog self.created_nexus = f"{tmp_path}/{os.sep}/output.nxs" @@ -86,11 +88,15 @@ 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] + assert self.ref_log_file, "Reference nexus .log file not found" + 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." @@ -134,10 +140,9 @@ def check_reproducibility_of_nexus(self): "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" + self.ref_log, "r", encoding="utf-8" ) as ref: gen_lines = gen.readlines() ref_lines = ref.readlines() From 0c04027ba78be510c994dffac921d3c3afac646e Mon Sep 17 00:00:00 2001 From: Lukas Pielsticker <50139597+lukaspie@users.noreply.github.com> Date: Mon, 8 Jul 2024 14:36:56 +0200 Subject: [PATCH 2/5] small fix for test framework --- src/pynxtools/testing/nexus_conversion.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pynxtools/testing/nexus_conversion.py b/src/pynxtools/testing/nexus_conversion.py index 05220effa..9dc460ba0 100644 --- a/src/pynxtools/testing/nexus_conversion.py +++ b/src/pynxtools/testing/nexus_conversion.py @@ -66,7 +66,7 @@ def __init__( self.reader_name = reader_name self.reader = get_reader(self.reader_name) self.files_or_dir = files_or_dir - self.ref_log_file = "" + self.ref_log_file = ref_log_file self.tmp_path = tmp_path self.caplog = caplog self.created_nexus = f"{tmp_path}/{os.sep}/output.nxs" @@ -142,7 +142,7 @@ def check_reproducibility_of_nexus(self): ] 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( - self.ref_log, "r", encoding="utf-8" + self.ref_log_file, "r", encoding="utf-8" ) as ref: gen_lines = gen.readlines() ref_lines = ref.readlines() From 933d9f74059b6fd3b3e9fea7d84b7e9ffcd3ffc1 Mon Sep 17 00:00:00 2001 From: Lukas Pielsticker <50139597+lukaspie@users.noreply.github.com> Date: Wed, 10 Jul 2024 14:01:24 +0200 Subject: [PATCH 3/5] move order of args in testing --- src/pynxtools/testing/nexus_conversion.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/pynxtools/testing/nexus_conversion.py b/src/pynxtools/testing/nexus_conversion.py index 9dc460ba0..ea85746af 100644 --- a/src/pynxtools/testing/nexus_conversion.py +++ b/src/pynxtools/testing/nexus_conversion.py @@ -40,7 +40,7 @@ class ReaderTest: """Generic test for reader plugins.""" def __init__( - self, nxdl, reader_name, files_or_dir, ref_log_file, tmp_path, caplog + self, nxdl, reader_name, files_or_dir, tmp_path, caplog, ref_log_file=None ) -> None: """Initialize the test object. @@ -53,22 +53,24 @@ def __init__( 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_log_file : str - Full path string to the reference log 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_log_file = ref_log_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( @@ -95,7 +97,9 @@ def convert_to_nexus( ][0] assert self.ref_log_file, "Reference nexus .log file not found" - input_files = [file for file in example_files if not file.endswith(".nxs")] + input_files = [ + file for file in example_files if not file.endswith((".nxs", ".log")) + ] assert ( self.nxdl in self.reader.supported_nxdls @@ -140,8 +144,8 @@ def check_reproducibility_of_nexus(self): "DEBUG - value: v", "DEBUG - value: https://github.com/FAIRmat-NFDI/nexus_definitions/blob/", ] - 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( + 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() From 9c7fb873414bfc989dc17d4bbe519077850a6cb8 Mon Sep 17 00:00:00 2001 From: Lukas Pielsticker <50139597+lukaspie@users.noreply.github.com> Date: Fri, 12 Jul 2024 15:42:33 +0200 Subject: [PATCH 4/5] use xps feature branch in plugin tests --- .github/workflows/plugin_test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/plugin_test.yaml b/.github/workflows/plugin_test.yaml index a30967303..143d9cd26 100644 --- a/.github/workflows/plugin_test.yaml +++ b/.github/workflows/plugin_test.yaml @@ -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 From 0f7ccd1723238af0251cf65d12fe72739ab4520b Mon Sep 17 00:00:00 2001 From: Lukas Pielsticker <50139597+lukaspie@users.noreply.github.com> Date: Thu, 12 Sep 2024 11:31:42 +0200 Subject: [PATCH 5/5] barebone for verify test, extract nomad parse test --- src/pynxtools/testing/nexus_conversion.py | 32 ++++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/pynxtools/testing/nexus_conversion.py b/src/pynxtools/testing/nexus_conversion.py index ea85746af..d0230b0ec 100644 --- a/src/pynxtools/testing/nexus_conversion.py +++ b/src/pynxtools/testing/nexus_conversion.py @@ -5,6 +5,8 @@ from glob import glob from typing import Literal +# from click.testing import CliRunner + try: from nomad.client import parse @@ -127,16 +129,13 @@ 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.""" @@ -163,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)