diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..5870cdc --- /dev/null +++ b/.env.example @@ -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 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 067426f..6cb4de4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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) diff --git a/Makefile b/Makefile index b3e9a05..47e1bb5 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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 diff --git a/conftest.py b/conftest.py index cc30b67..9377045 100644 --- a/conftest.py +++ b/conftest.py @@ -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" + ) diff --git a/packages/ref-core/tests/unit/test_executor.py b/packages/ref-core/tests/unit/test_executor.py index feec036..f9c60bf 100644 --- a/packages/ref-core/tests/unit/test_executor.py +++ b/packages/ref-core/tests/unit/test_executor.py @@ -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: @@ -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 diff --git a/packages/ref-core/tests/unit/test_providers.py b/packages/ref-core/tests/unit/test_providers.py index a9007cf..0931632 100644 --- a/packages/ref-core/tests/unit/test_providers.py +++ b/packages/ref-core/tests/unit/test_providers.py @@ -1,3 +1,4 @@ +import pytest from ref_core.metrics import Metric from ref_core.providers import MetricsProvider @@ -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" diff --git a/packages/ref-metrics-example/tests/unit/test_metrics.py b/packages/ref-metrics-example/tests/unit/test_metrics.py index ec6c963..9bb529b 100644 --- a/packages/ref-metrics-example/tests/unit/test_metrics.py +++ b/packages/ref-metrics-example/tests/unit/test_metrics.py @@ -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) diff --git a/ruff.toml b/ruff.toml index 3351c69..7978890 100644 --- a/ruff.toml +++ b/ruff.toml @@ -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