From b410cba9f3a3a03a44b06e72755419e471dfb518 Mon Sep 17 00:00:00 2001 From: JosePizarro3 Date: Tue, 5 Mar 2024 14:31:52 +0100 Subject: [PATCH] Deleted dumb fixtures Moved logger to tests/__init__.py --- tests/__init__.py | 22 +++++++++++ tests/test_atoms_state.py | 81 ++++++++++++++------------------------ tests/test_model_system.py | 1 + tests/test_template.py | 39 ------------------ 4 files changed, 52 insertions(+), 91 deletions(-) delete mode 100644 tests/test_template.py diff --git a/tests/__init__.py b/tests/__init__.py index e69de29b..b14591ac 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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__) diff --git a/tests/test_atoms_state.py b/tests/test_atoms_state.py index 27d80903..c5a9f1bc 100644 --- a/tests/test_atoms_state.py +++ b/tests/test_atoms_state.py @@ -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, @@ -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.""" @@ -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', [ @@ -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( @@ -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, @@ -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 @@ -139,7 +134,6 @@ def test_number_and_symbol( ) def test_degeneracy( self, - orbital_state, l_quantum_number, ml_quantum_number, j_quantum_number, @@ -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, @@ -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' @@ -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', [ @@ -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: @@ -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: @@ -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.""" @@ -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', [ @@ -277,11 +259,12 @@ 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 @@ -289,7 +272,7 @@ def test_u_interactions(self, hubbard_interactions, slater_integrals, results): 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]) @@ -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, @@ -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 @@ -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', [ @@ -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 diff --git a/tests/test_model_system.py b/tests/test_model_system.py index 20299a19..14ed993f 100644 --- a/tests/test_model_system.py +++ b/tests/test_model_system.py @@ -18,6 +18,7 @@ import pytest +from . import logger from nomad_simulations.model_system import AtomicCell diff --git a/tests/test_template.py b/tests/test_template.py deleted file mode 100644 index 4d56c854..00000000 --- a/tests/test_template.py +++ /dev/null @@ -1,39 +0,0 @@ -# -# 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.utils import get_logger -from nomad.datamodel import EntryArchive, EntryMetadata - - -def approx(value, abs=0, rel=1e-6): - return pytest.approx(value, abs=abs, rel=rel) - - -LOGGER = get_logger(__name__) - - -def run_parsing(parser_class, filepath): - archive = EntryArchive(metadata=EntryMetadata()) - parser_class().parse(filepath, archive, LOGGER) - return archive - - -def test_dummy(): - assert True