Skip to content

Commit

Permalink
Added testing for utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Mar 12, 2024
1 parent 5c56b90 commit efaaa2a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/nomad_simulations/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
72 changes: 72 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit efaaa2a

Please sign in to comment.