Skip to content

Commit

Permalink
Restructure tests (#152)
Browse files Browse the repository at this point in the history
* Move parsing into conftest

* Parametrize two fixtures

* Make subdirectory for every measurement method

* Renaming test files

* Minor
  • Loading branch information
ka-sarthak authored Jan 7, 2025
1 parent 1cbc436 commit 617fb72
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 80 deletions.
47 changes: 47 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
#
# 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.
#
import logging
import os

import pytest
import structlog
from nomad.client import parse
from nomad.utils import structlogging
from structlog.testing import LogCapture

Expand Down Expand Up @@ -34,3 +53,31 @@ def fixture_caplog(request):
processors.clear()
processors.extend(old_processors)
structlog.configure(processors=processors)


@pytest.fixture(
name='parsed_measurement_archive',
scope='function',
)
def fixture_parsed_measurement_archive(request):
"""
Sets up data for testing and cleans up after the test. The data file is parsed,
returning an `EntryArchive` object. It contains a reference to the `.archive.json`
file created by plugin parsers for the measurement data. Parsing this
`.archive.json` file returns the `EntryArchive` object for the measurement data,
which is finally yeilded to the test function.
"""
rel_file_path = request.param
file_archive = parse(rel_file_path)[0]

rel_measurement_archive_path = os.path.join(
rel_file_path.rsplit('.', 1)[0] + '.archive.json'
)
assert file_archive.data.measurement.m_proxy_value == os.path.abspath(
rel_measurement_archive_path
)

yield parse(rel_measurement_archive_path)[0]

if os.path.exists(rel_measurement_archive_path):
os.remove(rel_measurement_archive_path)
80 changes: 0 additions & 80 deletions tests/test_parser.py

This file was deleted.

File renamed without changes.
62 changes: 62 additions & 0 deletions tests/test_xrd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#
# 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.
#
import pytest
from nomad.client import normalize_all

test_files = [
'tests/data/xrd/XRD-918-16_10.xrdml',
'tests/data/xrd/m54313_om2th_10.xrdml',
'tests/data/xrd/m82762_rc1mm_1_16dg_src_slit_phi-101_3dg_-420_mesh_long.xrdml',
'tests/data/xrd/23-012-AG_2thomegascan_long.brml',
'tests/data/xrd/EJZ060_13_004_RSM.brml',
'tests/data/xrd/Omega-2Theta_scan_high_temperature.rasx',
'tests/data/xrd/RSM_111_sdd=350.rasx',
'tests/data/xrd/TwoTheta_scan_powder.rasx',
]
log_levels = ['error', 'critical']


@pytest.mark.parametrize(
'parsed_measurement_archive, caplog',
[(file, log_level) for file in test_files for log_level in log_levels],
indirect=True,
)
def test_normalize_all(parsed_measurement_archive, caplog):
"""
Tests the normalization of the parsed archive.
Args:
parsed_archive (pytest.fixture): Fixture to handle the parsing of archive.
caplog (pytest.fixture): Fixture to capture errors from the logger.
"""
normalize_all(parsed_measurement_archive)

assert (
parsed_measurement_archive.data.xrd_settings.source.xray_tube_material == 'Cu'
)
assert parsed_measurement_archive.data.results[
0
].source_peak_wavelength.magnitude == pytest.approx(1.540598, 1e-2)
if len(parsed_measurement_archive.data.results[0].intensity.shape) == 1:
assert (
parsed_measurement_archive.results.properties.structural.diffraction_pattern[
0
].incident_beam_wavelength.magnitude
* 1e10
== pytest.approx(1.540598, 1e-2)
)

0 comments on commit 617fb72

Please sign in to comment.