Skip to content

Commit

Permalink
Fixes #121. Raises exception when wrong dict found. (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
sherjeelshabih committed May 31, 2023
1 parent f8dcf39 commit 43c9edf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
22 changes: 22 additions & 0 deletions pynxtools/dataconverter/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Exceptions defined for use with the Dataconverter"""


class InvalidDictProvided(Exception):
"""An exception for a situation when an unexpected Python dict is provided"""
8 changes: 8 additions & 0 deletions pynxtools/dataconverter/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from pynxtools.dataconverter import helpers
from pynxtools.nexus import nexus
from pynxtools.dataconverter.exceptions import InvalidDictProvided

logger = logging.getLogger(__name__) # pylint: disable=C0103
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -158,6 +159,11 @@ def handle_dicts_entries(data, grp, entry_name, output_path, path):
compression_opts=strength)
else:
grp.create_dataset(entry_name, data=data["compress"])
else:
print("comes here!!")
raise InvalidDictProvided("A dictionary was provided to the template but it didn't"
" fall into any of the know cases of handling"
" dictionaries. This occured for: " + entry_name)
return grp[entry_name]


Expand Down Expand Up @@ -260,6 +266,8 @@ def add_units_key(dataset, path):
self.output_path, path])
else:
dataset = grp.create_dataset(entry_name, data=data)
except InvalidDictProvided as exc:
print(str(exc))
except Exception as exc:
raise IOError(f"Unknown error occured writing the path: {path} "
f"with the following message: {str(exc)}") from exc
Expand Down
10 changes: 5 additions & 5 deletions tests/dataconverter/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"""Test cases for the convert script used to access the DataConverter."""

import os
from distutils import file_util
import logging
from setuptools import distutils
from click.testing import CliRunner
import pytest
import h5py
Expand All @@ -32,17 +32,17 @@ def move_xarray_file_to_tmp(tmp_path):
"""Moves the xarray file, which is used to test linking into the tmp_path directory."""
test_file_path = os.path.join(os.path.dirname(__file__),
"../data/dataconverter/readers/mpes")
file_util.copy_file(os.path.join(test_file_path, "xarray_saved_small_calibration.h5"),
os.path.join(tmp_path, "xarray_saved_small_calibration.h5"))
distutils.file_util.copy_file(os.path.join(test_file_path, "xarray_saved_small_calibration.h5"),
os.path.join(tmp_path, "xarray_saved_small_calibration.h5"))


def restore_xarray_file_from_tmp(tmp_path):
"""Restores the xarray file from the tmp_path directory."""
test_file_path = os.path.join(os.path.dirname(__file__),
"../data/dataconverter/readers/mpes")
os.remove(os.path.join(test_file_path, "xarray_saved_small_calibration.h5"))
file_util.move_file(os.path.join(tmp_path, "xarray_saved_small_calibration.h5"),
os.path.join(test_file_path, "xarray_saved_small_calibration.h5"))
distutils.file_util.move_file(os.path.join(tmp_path, "xarray_saved_small_calibration.h5"),
os.path.join(test_file_path, "xarray_saved_small_calibration.h5"))


@pytest.mark.parametrize("cli_inputs", [
Expand Down
14 changes: 7 additions & 7 deletions tests/dataconverter/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import xml.etree.ElementTree as ET
import os
from distutils import file_util
from setuptools import distutils
import pytest
import numpy as np

Expand Down Expand Up @@ -93,12 +93,12 @@ def fixture_filled_test_data(template, tmp_path):
# Copy original measurement file to tmp dir,
# because h5py.ExternalLink is modifying it while
# linking the nxs file.
file_util.copy_file(f"{os.path.dirname(__file__)}"
f"/../"
f"data/dataconverter/"
f"readers/mpes/"
f"xarray_saved_small_cali"
"bration.h5", tmp_path)
distutils.file_util.copy_file(f"{os.path.dirname(__file__)}"
f"/../"
f"data/dataconverter/"
f"readers/mpes/"
f"xarray_saved_small_cali"
"bration.h5", tmp_path)

template.clear()
template["optional"]["/ENTRY[my_entry]/NXODD_name/float_value"] = 2.0
Expand Down
24 changes: 21 additions & 3 deletions tests/dataconverter/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

import pytest
import h5py
from pynxtools.dataconverter.exceptions import InvalidDictProvided


from pynxtools.dataconverter.writer import Writer
from .test_helpers import fixture_filled_test_data, fixture_template # pylint: disable=unused-import
from .test_helpers import fixture_filled_test_data, fixture_template, alter_dict # pylint: disable=unused-import


@pytest.mark.usefixtures("filled_test_data")
Expand Down Expand Up @@ -59,7 +61,23 @@ def test_write_link(writer):
Checks whether entries given above get written out when a dictionary containing a link is
given in the template dictionary."""
writer.write()
print(writer)
print(type(writer))
test_nxs = h5py.File(writer.output_path, "r")
assert isinstance(test_nxs["/my_entry/links/ext_link"], h5py.Dataset)


@pytest.mark.usefixtures("filled_test_data")
def test_wrong_dict_provided_in_template(filled_test_data, tmp_path):
"""Tests if the writer correctly fails when a wrong dictionary is provided"""
writer = Writer(
alter_dict(filled_test_data,
"/ENTRY[my_entry]/links/ext_link",
{"not a link or anything": 2.0}),
os.path.join("tests", "data", "dataconverter", "NXtest.nxdl.xml"),
os.path.join(tmp_path, "test.nxs")
)
with pytest.raises(InvalidDictProvided) as execinfo:
writer.write()
assert str(execinfo.value) == ("pynxtools.dataconverter.exceptions.InvalidDictProvided: "
"A dictionary was provided to the template but it didn't "
"fall into any of the know cases of handling dictionaries"
". This occured for: ext_link")

0 comments on commit 43c9edf

Please sign in to comment.