Skip to content

Commit

Permalink
test: Fix core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisjared committed Nov 6, 2024
1 parent fa93f55 commit 80376f0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 25 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Example of an environment file for the CMIP-REF project
# This allows for running the project in a development environment outside of a container

CELERY_BROKER_URL=redis://localhost:6379/1

REF_OUTPUT_ROOT=out
REF_ESGF_ROOT=.esgpull/data
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ jobs:
make fetch-test-data
- name: Run tests
run: |
cp .env.example .env
uv run --package ref-core pytest packages/ref-core -r a -v --doctest-modules --cov=packages/ref-core/src --cov-report=term
uv run --package ref-celery pytest packages/ref-celery -r a -v --doctest-modules --cov=packages/ref-celery/src --cov-report=term
uv run --package ref-metrics-example pytest packages/ref-metrics-example -r a -v --doctest-modules --cov=packages/ref-metrics-example/src --cov-report=term --cov-append
uv run coverage xml
# Run integration tests (without adding to the coverage)
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ ruff-fixes: ## fix the code using ruff
uv run ruff format

.PHONY: test-core
test-core: ## run the tests
test-core: ## run the tests for ref-core
uv run --package ref-core \
pytest packages/ref-core \
-r a -v --doctest-modules --cov=packages/ref-core/src

.PHONY: test-celery
test-celery: ## run the tests for ref-celery
uv run --package ref-celery \
pytest packages/ref-celery \
-r a -v --doctest-modules --cov=packages/ref-celery/src

.PHONY: test-metrics-example
test-metrics-example: ## run the tests
uv run --package ref-metrics-example \
Expand All @@ -56,7 +62,7 @@ test-integration: ## run the integration tests
-r a -v

.PHONY: test
test: test-core test-metrics-example test-integration ## run the tests
test: test-core test-celery test-metrics-example test-integration ## run the tests

# Note on code coverage and testing:
# If you want to debug what is going on with coverage, we have found
Expand Down
17 changes: 17 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,20 @@ def esgf_data_dir() -> Path:
pull = esgpull.Esgpull()

return pull.config.paths.data


@pytest.fixture
def test_dataset(esgf_data_dir) -> Path:
return (
esgf_data_dir
/ "CMIP6"
/ "ScenarioMIP"
/ "CSIRO"
/ "ACCESS-ESM1-5"
/ "ssp126"
/ "r1i1p1f1"
/ "Amon"
/ "tas"
/ "gn"
/ "v20210318"
)
6 changes: 4 additions & 2 deletions packages/ref-core/tests/unit/test_executor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from ref_core.executor import Executor, ExecutorManager, run_metric
from ref_core.executor.local import LocalExecutor
from ref_core.metrics import TriggerInfo


class TestExecutorManager:
Expand Down Expand Up @@ -29,10 +30,11 @@ def test_run_metric(self, configuration, mock_metric):


@pytest.mark.parametrize("executor_name", ["local", None])
def test_run_metric_local(monkeypatch, executor_name, mock_metric, provider, configuration):
def test_run_metric_local(monkeypatch, executor_name, mock_metric, provider, configuration, test_dataset):
if executor_name:
monkeypatch.setenv("REF_EXECUTOR", executor_name)
result = run_metric("mock", provider, configuration=configuration)
trigger = TriggerInfo(dataset=test_dataset)
result = run_metric("mock", provider, configuration=configuration, trigger=trigger)
assert result.successful


Expand Down
6 changes: 6 additions & 0 deletions packages/ref-core/tests/unit/test_providers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from ref_core.metrics import Metric
from ref_core.providers import MetricsProvider

Expand All @@ -18,6 +19,11 @@ def test_provider_register(self, mock_metric):
assert "mock" in provider._metrics
assert isinstance(provider.get("mock"), Metric)

def test_provider_register_invalid(self, mock_metric):
provider = MetricsProvider("provider_name", "v0.23")
with pytest.raises(ValueError):
provider.register("invalid")

def test_provider_fixture(self, provider):
assert provider.name == "mock_provider"
assert provider.version == "v0.1.0"
Expand Down
20 changes: 0 additions & 20 deletions packages/ref-metrics-example/tests/unit/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
from pathlib import Path

import pytest
from ref_core.metrics import Configuration, TriggerInfo
from ref_metrics_example.example import AnnualGlobalMeanTimeseries, calculate_annual_mean_timeseries


@pytest.fixture
def test_dataset(esgf_data_dir) -> Path:
return (
esgf_data_dir
/ "CMIP6"
/ "ScenarioMIP"
/ "CSIRO"
/ "ACCESS-ESM1-5"
/ "ssp126"
/ "r1i1p1f1"
/ "Amon"
/ "tas"
/ "gn"
/ "v20210318"
)


def test_annual_mean(esgf_data_dir, test_dataset):
annual_mean = calculate_annual_mean_timeseries(test_dataset)

Expand Down
3 changes: 2 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ ignore = [
"test*.py" = [
"D", # Documentation not needed in tests
"S101", # S101 Use of `assert` detected
"PLR2004" # Magic value used in comparison
"PLR2004", # Magic value used in comparison
"PLR0913", # Too many arguments in function definition
]
"conftest.py" = [
"D", # Documentation not needed in tests
Expand Down

0 comments on commit 80376f0

Please sign in to comment.