Skip to content

Commit

Permalink
Merge pull request #23 from openscm/hotfix-test-pypi-installation
Browse files Browse the repository at this point in the history
Fix up test PyPI workflow
  • Loading branch information
znicholls authored Jan 8, 2025
2 parents c5b5e1b + 56318ed commit 179408a
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 45 deletions.
22 changes: 19 additions & 3 deletions .github/workflows/install-pypi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,26 @@ jobs:
run: |
which python
python scripts/test-install.py
- name: Install pytest
- name: Install min test dependencies
run: |
pip install pytest
pip install pytest pytest-regressions
- name: Run tests
run: |
# Can't run doctests here because the paths are different
# Can't run doctests here because the paths are different.
# This also only runs with minimum test dependencies.
# So this is really just a smoke test,
# rather than a super thorough integration test.
pytest tests -r a -vv tests
# # This is a sketch of how you would do more thorough tests.
# # I don't like this solution because it is out of line with the lock file.
# # Probably the simplest way to do this is to use uv somehow,
# # install the package from pypi and the test dependencies
# # based on the lock file.
# - name: Install extra test dependencies
# run: |
# # A bit annoying to maintain this by hand, but ok for now.
# pip install ipython matplotlib openscm-units scipy
# - name: Run tests with extra test dependencies installed
# run: |
# # Can't run doctests here because the paths are different.
# pytest tests -r a -vv tests
1 change: 1 addition & 0 deletions changelog/23.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed up test PyPI workflow
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
from unittest.mock import patch

import numpy as np
import openscm_units
import pint.testing
import pytest

from continuous_timeseries import InterpolationOption, Timeseries
from continuous_timeseries import budget_compatible_pathways as ct_bcp
from continuous_timeseries.exceptions import MissingOptionalDependencyError

openscm_units = pytest.importorskip("openscm_units")

UR = openscm_units.unit_registry
Q = UR.Quantity

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
)
from continuous_timeseries.typing import PINT_NUMPY_ARRAY, PINT_SCALAR

pytest.importorskip("scipy.interpolate")

UR = pint.get_application_registry()
Q = UR.Quantity

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_plotting_helpers_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from continuous_timeseries.plotting_helpers import get_plot_vals

pytest.importorskip("matplotlib")

UR = pint.get_application_registry()
Q = UR.Quantity

Expand Down
32 changes: 17 additions & 15 deletions tests/integration/test_time_axis_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

from __future__ import annotations

import textwrap

import numpy as np
import pint
import pint.testing
import pytest
from IPython.lib.pretty import pretty

from continuous_timeseries.time_axis import TimeAxis

Expand Down Expand Up @@ -75,37 +76,38 @@ def test_str(bounds, exp_str):


@pytest.mark.parametrize(
"bounds, exp_pretty",
"bounds ",
(
pytest.param(
Q([1.0, 2.0, 3.0], "yr"),
"TimeAxis(bounds=<Quantity([1. 2. 3.], 'year')>)",
id="basic",
),
pytest.param(
Q(np.linspace(1750, 2000 + 1, 1000), "yr"),
(
"TimeAxis(\n"
f"bounds={pretty(Q(np.linspace(1750, 2000 + 1, 1000), 'yr'))})"
),
marks=pytest.mark.skip(reason="Too hard to predict indenting and slow"),
id="big_array",
),
pytest.param(
Q(np.linspace(1750, 2000 + 1, int(1e5)), "yr"),
(
"TimeAxis(\n"
" bounds=<Quantity([1750. 1750.00251003 1750.00502005 ... 2000.99497995 2000.99748997\n" # noqa: E501
" 2001. ], 'year')>)"
),
id="really_big_array",
),
),
)
def test_pretty(bounds, exp_pretty):
def test_pretty(bounds):
pytest.importorskip("IPython")

from IPython.lib.pretty import pretty

formatted = f"bounds={pretty(bounds)}"

if len(formatted) > 60:
indented = textwrap.indent(formatted, " ")
exp = f"TimeAxis(\n{indented})"
else:
exp = f"TimeAxis({formatted})"

instance = TimeAxis(bounds)

assert pretty(instance) == exp_pretty
assert pretty(instance) == exp


@pytest.mark.parametrize(
Expand Down
12 changes: 10 additions & 2 deletions tests/integration/test_timeseries_continous_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import pint
import pint.testing
import pytest
import scipy.interpolate
from attrs import define
from IPython.lib.pretty import pretty

from continuous_timeseries.exceptions import (
ExtrapolationNotAllowedError,
Expand All @@ -27,6 +25,8 @@
TimeseriesContinuous,
)

scipy = pytest.importorskip("scipy")

UR = pint.get_application_registry()
Q = UR.Quantity

Expand Down Expand Up @@ -145,6 +145,10 @@ def test_str_continuous_function_scipy_ppoly(continuous_function_scipy_ppoly, ex
),
)
def test_pretty_continuous_function_scipy_ppoly(continuous_function_scipy_ppoly, exp):
pytest.importorskip("IPython")

from IPython.lib.pretty import pretty

pretty_value = pretty(continuous_function_scipy_ppoly)

assert pretty_value == exp
Expand Down Expand Up @@ -335,6 +339,10 @@ def test_str(ts, file_regression):
)
@formatting_check_cases
def test_pretty(ts, file_regression):
pytest.importorskip("IPython")

from IPython.lib.pretty import pretty

file_regression.check(
f"{pretty(ts)}\n",
extension=".txt",
Expand Down
17 changes: 13 additions & 4 deletions tests/integration/test_timeseries_discrete_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import pint
import pint.testing
import pytest
from IPython.lib.pretty import pretty

from continuous_timeseries.discrete_to_continuous import InterpolationOption
from continuous_timeseries.exceptions import MissingOptionalDependencyError
Expand Down Expand Up @@ -94,6 +93,10 @@ def test_str(ts, file_regression):

@formatting_check_cases
def test_pretty(ts, file_regression):
pytest.importorskip("IPython")

from IPython.lib.pretty import pretty

file_regression.check(
f"{pretty(ts)}\n",
extension=".txt",
Expand Down Expand Up @@ -148,7 +151,7 @@ def test_to_continuous_timeseries_warning_suppression():
def test_plot( # noqa: PLR0913
x_units, y_units, plot_kwargs, legend, image_regression, tmp_path
):
import matplotlib
matplotlib = pytest.importorskip("matplotlib")

# ensure matplotlib does not use a GUI backend (such as Tk)
matplotlib.use("Agg")
Expand Down Expand Up @@ -246,7 +249,7 @@ def test_plot( # noqa: PLR0913
def test_plot_matplotlib_units_not_registered(
plot_kwargs, expectation, image_regression, tmp_path
):
import matplotlib
matplotlib = pytest.importorskip("matplotlib")

# ensure matplotlib does not use a GUI backend (such as Tk)
matplotlib.use("Agg")
Expand Down Expand Up @@ -297,7 +300,11 @@ def test_plot_matplotlib_units_not_registered(
@pytest.mark.parametrize(
"sys_modules_patch, expectation",
(
pytest.param({}, does_not_raise(), id="matplotlib_available"),
pytest.param(
{},
does_not_raise(),
id="matplotlib_available",
),
pytest.param(
{"matplotlib": None},
pytest.raises(
Expand All @@ -309,6 +316,8 @@ def test_plot_matplotlib_units_not_registered(
),
)
def test_plot_ax_creation(sys_modules_patch, expectation):
(pytest.importorskip("matplotlib"),)

ts = TimeseriesDiscrete(
name="basic",
time_axis=TimeAxis(Q([1.0, 2.0, 3.0], "yr")),
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/test_timeseries_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import pint.testing
import pytest
from attrs import define, field, validators
from IPython.lib.pretty import pretty

from continuous_timeseries.discrete_to_continuous import InterpolationOption
from continuous_timeseries.exceptions import (
Expand All @@ -35,6 +34,9 @@
InterpolationUpdateChangedValuesAtBoundsWarning,
)

# Tests don't make sense without scipy
pytest.importorskip("scipy")

UR = pint.get_application_registry()
Q = UR.Quantity

Expand Down Expand Up @@ -116,6 +118,10 @@ def test_str(ts, file_regression):
)
@formatting_check_cases
def test_pretty(ts, file_regression):
pytest.importorskip("IPython")

from IPython.lib.pretty import pretty

file_regression.check(
f"{pretty(ts)}\n",
extension=".txt",
Expand Down
32 changes: 17 additions & 15 deletions tests/integration/test_values_at_bounds_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

from __future__ import annotations

import textwrap

import numpy as np
import pint
import pytest
from IPython.lib.pretty import pretty

from continuous_timeseries.values_at_bounds import ValuesAtBounds

Expand Down Expand Up @@ -74,37 +75,38 @@ def test_str(values, exp_str):


@pytest.mark.parametrize(
"values, exp_pretty",
"values",
(
pytest.param(
Q([1.0, 2.0, 3.0], "kg"),
"ValuesAtBounds(values=<Quantity([1. 2. 3.], 'kilogram')>)",
id="basic",
),
pytest.param(
Q(np.linspace(1750, 2000 + 1, 1000), "yr"),
(
"ValuesAtBounds(\n"
f"values={pretty(Q(np.linspace(1750, 2000 + 1, 1000), 'yr'))})"
),
marks=pytest.mark.skip(reason="Too hard to predict indenting and slow"),
id="big_array",
),
pytest.param(
Q(np.linspace(1750, 2000 + 1, int(1e5)), "yr"),
(
"ValuesAtBounds(\n"
" values=<Quantity([1750. 1750.00251003 1750.00502005 ... 2000.99497995 2000.99748997\n" # noqa: E501
" 2001. ], 'year')>)"
),
id="really_big_array",
),
),
)
def test_pretty(values, exp_pretty):
def test_pretty(values):
pytest.importorskip("IPython")

from IPython.lib.pretty import pretty

formatted = f"values={pretty(values)}"

if len(formatted) > 60:
indented = textwrap.indent(formatted, " ")
exp = f"ValuesAtBounds(\n{indented})"
else:
exp = f"ValuesAtBounds({formatted})"

instance = ValuesAtBounds(values)

assert pretty(instance) == exp_pretty
assert pretty(instance) == exp


@pytest.mark.parametrize(
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/test_discrete_to_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def test_implicit_extrapolation_raises(piecewise_constant_class, x, times, expec
def test_differentiate_scipy_availability(
piecewise_constant_class, sys_modules_patch, expectation
):
pytest.importorskip("scipy")
with patch.dict(sys.modules, sys_modules_patch):
with expectation:
piecewise_constant_class(np.arange(10), np.arange(10)).differentiate()
Expand All @@ -155,7 +156,11 @@ def test_differentiate_scipy_availability(
@pytest.mark.parametrize(
"sys_modules_patch, expectation",
(
pytest.param({}, does_not_raise(), id="scipy_available"),
pytest.param(
{},
does_not_raise(),
id="scipy_available",
),
pytest.param(
{"scipy": None},
pytest.raises(
Expand All @@ -170,6 +175,7 @@ def test_differentiate_scipy_availability(
def test_integrate_scipy_availability(
piecewise_constant_class, sys_modules_patch, expectation
):
pytest.importorskip("scipy")
with patch.dict(sys.modules, sys_modules_patch):
with expectation:
piecewise_constant_class(np.arange(10), np.arange(10)).integrate(1.0, 5.0)
5 changes: 3 additions & 2 deletions tests/unit/test_timeseries_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import pint
import pint.testing
import pytest
import scipy.interpolate

from continuous_timeseries.exceptions import (
MissingOptionalDependencyError,
Expand Down Expand Up @@ -81,8 +80,10 @@ def test_validation_time_axis_values_same_shape(domain, expectation):
),
)
def test_integrate_no_scipy(sys_modules_patch, expectation):
scipy_interpolate = pytest.importorskip("scipy.interpolate")

continuous_function_scipy_ppoly = ContinuousFunctionScipyPPoly(
scipy.interpolate.PPoly(x=[1, 10, 20], c=[[10, 12]])
scipy_interpolate.PPoly(x=[1, 10, 20], c=[[10, 12]])
)
with patch.dict(sys.modules, sys_modules_patch):
with expectation:
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 179408a

Please sign in to comment.