Skip to content

Commit

Permalink
add test for log in writing
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspie committed Apr 3, 2024
1 parent e50ae35 commit e710518
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
Binary file added tests/data/xps.small_test.nxs
Binary file not shown.
72 changes: 71 additions & 1 deletion tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
Basic example based test for the XPS reader
"""
import os
import xml.etree.ElementTree as ET
from glob import glob
import xml.etree.ElementTree as ET
from pathlib import Path
import pytest
import logging

import pynxtools.dataconverter.convert as dataconverter
from pynxtools.dataconverter.convert import get_reader

from pynxtools.dataconverter.helpers import (
generate_template_from_nxdl,
validate_data_dict,
)
from pynxtools.dataconverter.template import Template
from pynxtools.nexus import nexus # noqa: E402 # noqa: E402
from pynxtools.nexus.nxdl_utils import get_nexus_definitions_path
from pynxtools_xps.reader import XPSReader

Expand Down Expand Up @@ -111,3 +117,67 @@ def test_example_data(sub_reader_data_dir):
# if "REG" in v:
# print(k)
# =============================================================================


def test_xps_writing(tmp_path):
"""Check if xps example can be reproduced"""
data_dir = os.path.join(Path(__file__).parent, "data")
input_files = (
os.path.join(data_dir, "xml", "In-situ_PBTTT_XPS_SPECS.xml"),
os.path.join(data_dir, "xml", "eln_data_xml.yaml"),
)
dataconverter.convert(
input_files,
"xps",
"NXmpes",
os.path.join(tmp_path, "xps.small_test.nxs"),
False,
False,
)
# check generated nexus file
test_data = os.path.join(tmp_path, "xps.small_test.nxs")
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(os.path.join(tmp_path, "xps_test.log"), "w")
formatter = logging.Formatter("%(levelname)s - %(message)s")
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
nexus_helper = nexus.HandleNexus(logger, test_data, None, None)
nexus_helper.process_nexus_master_file(None)
with open(os.path.join(tmp_path, "xps_test.log"), "r", encoding="utf-8") as logfile:
log = logfile.readlines()
with open(
os.path.join(data_dir, "Ref_nexus_xps.log"), "r", encoding="utf-8"
) as logfile:
ref_log = logfile.readlines()
assert log == ref_log


pwd = os.path.join(Path(__file__).parent, "data")
test_xps_writing(pwd)


def test_shows_correct_warnings():
"""
Checks whether the read function generates the correct warnings.
"""
def_dir = get_nexus_definitions_path()

data_dir = os.path.join(Path(__file__).parent, "data")
input_files = (
os.path.join(data_dir, "xml", "In-situ_PBTTT_XPS_SPECS.xml"),
os.path.join(data_dir, "xml", "eln_data_xml.yaml"),
)
nxdl_file = os.path.join(def_dir, "contributed_definitions", "NXmpes.nxdl.xml")

root = ET.parse(nxdl_file).getroot()
template = Template()
generate_template_from_nxdl(root, template)

read_data = get_reader("xps")().read(
template=Template(template), file_paths=tuple(input_files)
)

assert validate_data_dict(template, read_data, root)
assert not list(read_data.undocumented.keys())
Binary file added tests/xps.small_test.nxs
Binary file not shown.

0 comments on commit e710518

Please sign in to comment.