From efaaa2a6d8bd4f05f0ce5f9ae12094358d6f7dc9 Mon Sep 17 00:00:00 2001 From: JosePizarro3 Date: Tue, 12 Mar 2024 23:45:55 +0100 Subject: [PATCH] Added testing for utils.py --- src/nomad_simulations/utils/utils.py | 9 ++-- tests/test_utils.py | 72 ++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 tests/test_utils.py diff --git a/src/nomad_simulations/utils/utils.py b/src/nomad_simulations/utils/utils.py index 8ff907f9..f05fbb17 100644 --- a/src/nomad_simulations/utils/utils.py +++ b/src/nomad_simulations/utils/utils.py @@ -28,8 +28,8 @@ def get_sibling_section( section: ArchiveSection, sibling_section_name: str, + logger: BoundLogger, index_sibling: int = 0, - logger: BoundLogger = None, ) -> Optional[ArchiveSection]: """ Gets the sibling section of a section by performing a seesaw move by going to the parent @@ -112,7 +112,7 @@ def degeneracy(self): ) -def is_not_representative(model_system, logger: BoundLogger = None): +def is_not_representative(model_system, logger: BoundLogger): """ Checks if the given `ModelSystem` is not representative and logs a warning. @@ -123,13 +123,16 @@ def is_not_representative(model_system, logger: BoundLogger = None): Returns: (bool): True if the `ModelSystem` is not representative, False otherwise. """ + if model_system is None: + logger.warning('The `ModelSystem` is empty.') + return None if not model_system.is_representative: logger.warning('The `ModelSystem` was not found to be representative.') return True return False -def check_archive(archive: EntryArchive, logger: BoundLogger = None): +def check_archive(archive: EntryArchive, logger: BoundLogger): """ Checks if the given `EntryArchive` is empty and logs a warning. diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..ed0e3461 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,72 @@ +# +# 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 +import numpy as np + +from nomad.datamodel import EntryArchive + +from . import logger +from nomad_simulations.utils import ( + get_sibling_section, + is_not_representative, + check_archive, +) +from nomad_simulations.model_system import ModelSystem, AtomicCell, Symmetry + + +def test_get_sibling_section(): + """ + Test the `get_sibling_section` utility function. + """ + parent_section = ModelSystem() + section = AtomicCell(type='original') + parent_section.atomic_cell.append(section) + sibling_section = Symmetry() + parent_section.symmetry.append(sibling_section) + assert get_sibling_section(section, '', logger) is None + assert get_sibling_section(section, 'symmetry', logger) == sibling_section + assert get_sibling_section(sibling_section, 'atomic_cell', logger) == section + assert get_sibling_section(section, 'symmetry', logger, index_sibling=2) is None + section2 = AtomicCell(type='primitive') + parent_section.atomic_cell.append(section2) + assert ( + get_sibling_section(sibling_section, 'atomic_cell', logger, index_sibling=0) + == section + ) + assert ( + get_sibling_section(sibling_section, 'atomic_cell', logger, index_sibling=1) + == section2 + ) + + +def test_is_not_representative(): + """ + Test the `is_not_representative` utility function. + """ + assert is_not_representative(None, logger) is None + assert is_not_representative(ModelSystem(), logger) + assert not is_not_representative(ModelSystem(is_representative=True), logger) + + +def test_check_archive(): + """ + Test the `check_archive` utility function. + """ + assert not check_archive(None, logger) + assert check_archive(EntryArchive(), logger)