Skip to content

Commit

Permalink
Merge pull request #250 from ImperialCollegeLondon/test-imports
Browse files Browse the repository at this point in the history
Updated module import in pmodel unit tests
  • Loading branch information
surbhigoel77 authored Jun 27, 2024
2 parents 6351c3d + bc38872 commit 12816d2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class MyReferenceStyle(AuthorYearReferenceStyle):
("py:class", "numpy._typing._generic_alias.ScalarType"),
("py:class", "numpy.float32"),
("py:class", "numpy.timedelta64"),
("py:class", "numpy.bool_"),
("py:class", "numpy.ndarray"),
("py:class", "numpy.dtype"),
("py:class", "numpy.dtype[+ScalarType]"),
Expand Down
2 changes: 1 addition & 1 deletion pyrealm/core/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def check_input_shapes(*args: float | int | np.generic | np.ndarray | None) -> t
# # Note that 0-dim ndarrays (which are scalars) pass through
# if val.ndim > 0:
# shapes.add(val.shape)
elif val is None or isinstance(val, float | int | np.generic):
elif val is None or isinstance(val, (float | int | np.generic)):
pass # No need to track scalars and optional values pass None
else:
raise ValueError(f"Unexpected input to check_input_shapes: {type(val)}")
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/pmodel/test_c3c4competition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
import numpy as np
import pytest

from pyrealm.pmodel import (
C3C4Competition,
CalcCarbonIsotopes,
PModel,
PModelEnvironment,
)


@pytest.mark.parametrize(
argnames=["pmodel_c3_args", "pmodel_c4_args", "expected"],
Expand Down Expand Up @@ -74,6 +67,13 @@
)
def test_c3c4competition(pmodel_c3_args, pmodel_c4_args, expected):
"""Test the C3/C4 competition model."""
from pyrealm.pmodel import (
C3C4Competition,
CalcCarbonIsotopes,
PModel,
PModelEnvironment,
)

env = PModelEnvironment(
tc=np.array([20, 35]),
patm=np.array([101325]),
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/pmodel/test_calc_carbon_isotopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import numpy as np
import pytest

from pyrealm.pmodel import CalcCarbonIsotopes, PModel, PModelEnvironment


@pytest.mark.parametrize(
argnames=["pmodelenv_args", "pmodel_args", "expected"],
Expand Down Expand Up @@ -69,6 +67,8 @@
)
def test_CalcCarbonIsotopes(pmodelenv_args, pmodel_args, expected):
"""Tests the CalcCarbonIsotopes class."""
from pyrealm.pmodel import CalcCarbonIsotopes, PModel, PModelEnvironment

env = PModelEnvironment(**pmodelenv_args)
pmodel = PModel(env, **pmodel_args)
cci = CalcCarbonIsotopes(pmodel, d13CO2=np.array([-8.4]), D14CO2=np.array([19.2]))
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/pmodel/test_jmax_limitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import numpy as np
import pytest

# from pyrealm.constants import PModelConst
from pyrealm.pmodel.jmax_limitation import JmaxLimitation
from pyrealm.pmodel.optimal_chi import OptimalChiPrentice14
from pyrealm.pmodel.pmodel_environment import PModelEnvironment


@pytest.fixture
def mock_optimal_chi():
"""Build an OptimalChiABC instance."""
from pyrealm.pmodel.optimal_chi import OptimalChiPrentice14
from pyrealm.pmodel.pmodel_environment import PModelEnvironment

# Create an instance of OptimalChiABC for testing
env = PModelEnvironment(
tc=np.array([20]),
Expand Down Expand Up @@ -40,6 +38,8 @@ def mock_optimal_chi():
)
def test_jmax_limitation(mock_optimal_chi, method, expected_f_j, expected_f_v):
"""Test that JmaxLimitation class works as expected."""
from pyrealm.pmodel.jmax_limitation import JmaxLimitation

# Create an instance of JmaxLimitation with mock OptimalChiABC and test method
jmax_limitation = JmaxLimitation(mock_optimal_chi, method=method)
# Assert that calculated f_j and f_v match expected values
Expand All @@ -49,6 +49,8 @@ def test_jmax_limitation(mock_optimal_chi, method, expected_f_j, expected_f_v):

def test_invalid_method(mock_optimal_chi):
"""Test that JmaxLimitation raises ValueError for invalid method."""
from pyrealm.pmodel.jmax_limitation import JmaxLimitation

# Test invalid method
with pytest.raises(ValueError):
JmaxLimitation(mock_optimal_chi, method="invalid_method")
22 changes: 13 additions & 9 deletions tests/unit/pmodel/test_pmodel_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
import numpy as np
import pytest

from pyrealm.constants import CoreConst, PModelConst
from pyrealm.pmodel.functions import (
calc_co2_to_ca,
calc_gammastar,
calc_kmm,
calc_ns_star,
)
from pyrealm.pmodel.pmodel_environment import PModelEnvironment

""""Test that the pmodel environment calculates attributes match the expected values."""


Expand All @@ -36,6 +27,8 @@ def test_pmodel_environment(
tc, vpd, co2, patm, expected_ca, expected_gammastar, expected_kmm, expected_ns_star
):
"""Test the PModelEnvironment class."""
from pyrealm.pmodel.pmodel_environment import PModelEnvironment

env = PModelEnvironment(tc=tc, vpd=vpd, co2=co2, patm=patm)

assert env.ca == pytest.approx(expected_ca, abs=1e-5)
Expand All @@ -62,6 +55,9 @@ def function_test_data():

def test_out_of_bound_output(function_test_data):
"""Function to calculate kmm."""
from pyrealm.constants import PModelConst
from pyrealm.pmodel.functions import calc_kmm

tc_ar_values, patm_ar_values, co2_ar_values = function_test_data

pmodel_const = PModelConst()
Expand All @@ -84,6 +80,9 @@ def test_out_of_bound_output(function_test_data):

def test_out_of_bound_output_ns_star(function_test_data):
"""Function to calculate ns_star."""
from pyrealm.constants import CoreConst
from pyrealm.pmodel.functions import calc_ns_star

tc_ar_values, patm_ar_values, _ = function_test_data # Ignore the third variable

core_const = CoreConst(k_To=298.15, k_Po=101325)
Expand All @@ -106,6 +105,9 @@ def test_out_of_bound_output_ns_star(function_test_data):

def test_out_of_bound_output_gammastar(function_test_data):
"""Function to calculate calc_gammastar."""
from pyrealm.constants import CoreConst, PModelConst
from pyrealm.pmodel.functions import calc_gammastar

tc_ar_values, patm_ar_values, _ = function_test_data

core_const = CoreConst(k_To=298.15, k_Po=101325)
Expand All @@ -131,6 +133,8 @@ def test_out_of_bound_output_gammastar(function_test_data):

def test_out_of_bound_output_co2_to_ca(function_test_data):
"""Function to calculate co2_to_ca."""
from pyrealm.pmodel.functions import calc_co2_to_ca

_, patm_ar_values, co2_ar_values = function_test_data

co2_to_ca_lower_bound = 0
Expand Down

0 comments on commit 12816d2

Please sign in to comment.