Skip to content

Commit

Permalink
Deleted dumb fixtures
Browse files Browse the repository at this point in the history
Moved logger to tests/__init__.py
  • Loading branch information
JosePizarro3 committed Mar 5, 2024
1 parent 9027215 commit b410cba
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 91 deletions.
22 changes: 22 additions & 0 deletions tests/__init__.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.
#

import logging


logger = logging.getLogger(__name__)
81 changes: 29 additions & 52 deletions tests/test_atoms_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import pytest
import numpy as np
import logging

from nomad.units import ureg

from . import logger
from nomad_simulations.atoms_state import (
OrbitalsState,
CoreHole,
Expand All @@ -35,8 +35,6 @@ class TestOrbitalsState:
Test the `OrbitalsState` class defined in atoms_state.py.
"""

logger = logging.getLogger(__name__)

@staticmethod
def add_quantum_numbers(orbital_state, quantum_name, quantum_type, value) -> None:
"""Adds quantum numbers to the `OrbitalsState` section."""
Expand All @@ -55,10 +53,6 @@ def add_state(
orbital_state.j_quantum_number = j_number
orbital_state.mj_quantum_number = mj_number

@pytest.fixture(autouse=True)
def orbital_state(self) -> OrbitalsState:
return OrbitalsState(n_quantum_number=2)

@pytest.mark.parametrize(
'number, values, results',
[
Expand All @@ -69,15 +63,16 @@ def orbital_state(self) -> OrbitalsState:
('ms_quantum_number', [0, 10, -0.5, 0.5], [False, False, True, True]),
],
)
def test_check_quantum_numbers(self, orbital_state, number, values, results):
def test_check_quantum_numbers(self, number, values, results):
"""
Test the quantum number check for the `OrbitalsState` section.
"""
orbital_state = OrbitalsState(n_quantum_number=2)
for val, res in zip(values, results):
if number == 'ml_quantum_number':
orbital_state.l_quantum_number = 2
setattr(orbital_state, number, val)
check = orbital_state._check_quantum_numbers(self.logger)
check = orbital_state._check_quantum_numbers(logger)
assert check == res

@pytest.mark.parametrize(
Expand All @@ -100,7 +95,6 @@ def test_check_quantum_numbers(self, orbital_state, number, values, results):
)
def test_number_and_symbol(
self,
orbital_state,
quantum_name,
quantum_type,
value,
Expand All @@ -111,17 +105,18 @@ def test_number_and_symbol(
Test the number and symbol resolution for each of the quantum numbers defined in the parametrization.
"""
# Adding quantum numbers to the `OrbitalsState` section
orbital_state = OrbitalsState(n_quantum_number=2)
self.add_quantum_numbers(orbital_state, quantum_name, quantum_type, value)

# Making sure that the `quantum_type` is assigned
resolved_type = orbital_state.resolve_number_and_symbol(
quantum_name, quantum_type, self.logger
quantum_name, quantum_type, logger
)
assert resolved_type == value

# Resolving if the counter-type is assigned
resolved_countertype = orbital_state.resolve_number_and_symbol(
quantum_name, countertype, self.logger
quantum_name, countertype, logger
)
assert resolved_countertype == expected_result

Expand All @@ -139,7 +134,6 @@ def test_number_and_symbol(
)
def test_degeneracy(
self,
orbital_state,
l_quantum_number,
ml_quantum_number,
j_quantum_number,
Expand All @@ -150,6 +144,7 @@ def test_degeneracy(
"""
Test the degeneracy of each of orbital states defined in the parametrization.
"""
orbital_state = OrbitalsState(n_quantum_number=2)
self.add_state(
orbital_state,
l_quantum_number,
Expand All @@ -161,12 +156,13 @@ def test_degeneracy(
resolved_degeneracy = orbital_state.resolve_degeneracy()
assert resolved_degeneracy == degeneracy

def test_normalize(self, orbital_state):
def test_normalize(self):
"""
Test the normalization of the `OrbitalsState`. Inputs are defined as the quantities of the `OrbitalsState` section.
"""
orbital_state = OrbitalsState(n_quantum_number=2)
self.add_state(orbital_state, 2, -2, None, None, None)
orbital_state.normalize(None, self.logger)
orbital_state.normalize(None, logger)
assert orbital_state.n_quantum_number == 2
assert orbital_state.l_quantum_number == 2
assert orbital_state.l_quantum_symbol == 'd'
Expand All @@ -180,12 +176,6 @@ class TestCoreHole:
Test the `CoreHole` class defined in atoms_state.py.
"""

logger = logging.getLogger(__name__)

@pytest.fixture(autouse=True)
def core_hole(self) -> CoreHole:
return CoreHole()

@pytest.mark.parametrize(
'orbital_ref, degeneracy, n_excited_electrons, occupation',
[
Expand All @@ -194,17 +184,16 @@ def core_hole(self) -> CoreHole:
(None, None, 0.5, None),
],
)
def test_occupation(
self, core_hole, orbital_ref, degeneracy, n_excited_electrons, occupation
):
def test_occupation(self, orbital_ref, degeneracy, n_excited_electrons, occupation):
"""
Test the occupation of a core hole for a given set of orbital reference and degeneracy.
"""
core_hole = CoreHole()
core_hole.orbital_ref = orbital_ref
if orbital_ref is not None:
assert orbital_ref.resolve_degeneracy() == degeneracy
core_hole.n_excited_electrons = n_excited_electrons
resolved_occupation = core_hole.resolve_occupation(self.logger)
resolved_occupation = core_hole.resolve_occupation(logger)
if resolved_occupation is not None:
assert np.isclose(resolved_occupation, occupation)
else:
Expand All @@ -226,17 +215,16 @@ def test_occupation(
(None, 0.5, None, (0.5, None, None)),
],
)
def test_normalize(
self, core_hole, orbital_ref, n_excited_electrons, dscf_state, results
):
def test_normalize(self, orbital_ref, n_excited_electrons, dscf_state, results):
"""
Test the normalization of the `CoreHole`. Inputs are defined as the quantities of the `CoreHole` section.
"""
core_hole = CoreHole()
core_hole.orbital_ref = orbital_ref
core_hole.n_excited_electrons = n_excited_electrons
core_hole.dscf_state = dscf_state

core_hole.normalize(None, self.logger)
core_hole.normalize(None, logger)

assert core_hole.n_excited_electrons == results[0]
if core_hole.orbital_ref:
Expand All @@ -249,8 +237,6 @@ class TestHubbardInteractions:
Test the `HubbardInteractions` class defined in atoms_state.py.
"""

logger = logging.getLogger(__name__)

@staticmethod
def add_slater_interactions(hubbard_interactions, slater_integrals) -> None:
"""Adds `slater_integrals` (in eV) to the `HubbardInteractions` section."""
Expand All @@ -265,10 +251,6 @@ def add_u_j(hubbard_interactions, u, j) -> None:
if j is not None:
hubbard_interactions.j_local_exchange_interaction = j * ureg('eV')

@pytest.fixture(autouse=True)
def hubbard_interactions(self) -> HubbardInteractions:
return HubbardInteractions()

@pytest.mark.parametrize(
'slater_integrals, results',
[
Expand All @@ -277,19 +259,20 @@ def hubbard_interactions(self) -> HubbardInteractions:
([3.0, 2.0, 1.0, 0.5], (None, None, None)),
],
)
def test_u_interactions(self, hubbard_interactions, slater_integrals, results):
def test_u_interactions(self, slater_integrals, results):
"""
Test the Hubbard interactions `U`, `U'`, and `J` for a given set of Slater integrals.
"""
# Adding `slater_integrals` to the `HubbardInteractions` section
hubbard_interactions = HubbardInteractions()
self.add_slater_interactions(hubbard_interactions, slater_integrals)

# Resolving U, U', and J from class method
(
u_interaction,
u_interorbital_interaction,
j_hunds_coupling,
) = hubbard_interactions.resolve_u_interactions(self.logger)
) = hubbard_interactions.resolve_u_interactions(logger)

if None not in (u_interaction, u_interorbital_interaction, j_hunds_coupling):
assert np.isclose(u_interaction.to('eV').magnitude, results[0])
Expand All @@ -313,7 +296,6 @@ def test_u_interactions(self, hubbard_interactions, slater_integrals, results):
)
def test_u_effective(
self,
hubbard_interactions,
u_interaction,
j_local_exchange_interaction,
u_effective,
Expand All @@ -322,25 +304,27 @@ def test_u_effective(
Test the effective Hubbard interaction `Ueff` for a given set of Hubbard interactions `U` and `J`.
"""
# Adding `u_interaction` and `j_local_exchange_interaction` to the `HubbardInteractions` section
hubbard_interactions = HubbardInteractions()
self.add_u_j(hubbard_interactions, u_interaction, j_local_exchange_interaction)

# Resolving Ueff from class method
resolved_u_effective = hubbard_interactions.resolve_u_effective(self.logger)
resolved_u_effective = hubbard_interactions.resolve_u_effective(logger)
if resolved_u_effective is not None:
assert np.isclose(resolved_u_effective.to('eV').magnitude, u_effective)
else:
assert resolved_u_effective == u_effective

def test_normalize(self, hubbard_interactions):
def test_normalize(self):
"""
Test the normalization of the `HubbardInteractions`. Inputs are defined as the quantities of the `HubbardInteractions` section.
"""
# ? Is this enough for testing? Can we do more?
hubbard_interactions = HubbardInteractions()
self.add_u_j(hubbard_interactions, 3.0, 2.0)
hubbard_interactions.u_interorbital_interaction = 1.0 * ureg('eV')
hubbard_interactions.j_hunds_coupling = 2.0 * ureg('eV')

hubbard_interactions.normalize(None, self.logger)
hubbard_interactions.normalize(None, logger)
assert np.isclose(hubbard_interactions.u_effective.to('eV').magnitude, 1.0)
assert np.isclose(hubbard_interactions.u_interaction.to('eV').magnitude, 3.0)
assert hubbard_interactions.slater_integrals is None
Expand All @@ -351,16 +335,10 @@ class TestAtomsState:
Tests the `AtomsState` class defined in atoms_state.py.
"""

logger = logging.getLogger(__name__)

@staticmethod
def add_element_information(atom_state, quantity_name, value) -> None:
setattr(atom_state, quantity_name, value)

@pytest.fixture(autouse=True)
def atom_state(self) -> AtomsState:
return AtomsState()

@pytest.mark.parametrize(
'chemical_symbol, atomic_number',
[
Expand All @@ -370,17 +348,16 @@ def atom_state(self) -> AtomsState:
('O', 8),
],
)
def test_chemical_symbol_and_atomic_number(
self, atom_state, chemical_symbol, atomic_number
):
def test_chemical_symbol_and_atomic_number(self, chemical_symbol, atomic_number):
"""
Test the `chemical_symbol` and `atomic_number` resolution for the `AtomsState` section.
"""
# Testing `chemical_symbol`
atom_state = AtomsState()
self.add_element_information(atom_state, 'chemical_symbol', chemical_symbol)
resolved_atomic_number = atom_state.resolve_atomic_number(self.logger)
resolved_atomic_number = atom_state.resolve_atomic_number(logger)
assert resolved_atomic_number == atomic_number
# Testing `atomic_number`
self.add_element_information(atom_state, 'atomic_number', atomic_number)
resolved_chemical_symbol = atom_state.resolve_chemical_symbol(self.logger)
resolved_chemical_symbol = atom_state.resolve_chemical_symbol(logger)
assert resolved_chemical_symbol == chemical_symbol
1 change: 1 addition & 0 deletions tests/test_model_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import pytest

from . import logger
from nomad_simulations.model_system import AtomicCell


Expand Down
39 changes: 0 additions & 39 deletions tests/test_template.py

This file was deleted.

0 comments on commit b410cba

Please sign in to comment.