diff --git a/packages/ref-celery/pyproject.toml b/packages/ref-celery/pyproject.toml index 9a9f550..8bc66d3 100644 --- a/packages/ref-celery/pyproject.toml +++ b/packages/ref-celery/pyproject.toml @@ -34,7 +34,7 @@ ref-celery = "ref_celery.cli:app" [tool.uv] dev-dependencies = [ - + "pytest-mock>=3.14.0", ] [tool.uv.sources] diff --git a/packages/ref-celery/src/ref_celery/cli.py b/packages/ref-celery/src/ref_celery/cli.py index baf9c70..eb3af27 100644 --- a/packages/ref-celery/src/ref_celery/cli.py +++ b/packages/ref-celery/src/ref_celery/cli.py @@ -34,13 +34,15 @@ def start_worker( try: imp = importlib.import_module(package.replace("-", "_")) # type: ignore except ModuleNotFoundError: - raise ValueError(f"Package '{package}' not found") + typer.echo(f"Package '{package}' not found") + raise typer.Abort() # Get the provider from the package try: provider = imp.provider except AttributeError: - raise ValueError("The package must define a 'provider' variable") + typer.echo("The package must define a 'provider' variable") + raise typer.Abort() # Wrap each metrics in the provider with a celery tasks register_celery_tasks(celery_app, provider) @@ -49,5 +51,5 @@ def start_worker( celery_app.worker_main(argv=argv) -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover app() diff --git a/packages/ref-celery/tests/test_cli.py b/packages/ref-celery/tests/test_cli.py new file mode 100644 index 0000000..1570840 --- /dev/null +++ b/packages/ref-celery/tests/test_cli.py @@ -0,0 +1,33 @@ +from ref_celery.cli import app +from typer.testing import CliRunner + +runner = CliRunner() + + +def test_cli_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + + +def test_cli_spawns_worker(mocker): + mock_app = mocker.patch("ref_celery.cli.create_celery_app") + result = runner.invoke(app, ["--package", "ref-metrics-example"]) + assert result.exit_code == 0 + + mock_app().worker_main.assert_called_once() + + +def test_cli_wrong_package(): + result = runner.invoke(app, ["--package", "missing"]) + assert result.exit_code == 1 + + print(result.output) + assert "Package 'missing' not found" in result.output + + +def test_cli_missing_provider(): + result = runner.invoke(app, ["--package", "pandas"]) + assert result.exit_code == 1 + + print(result.output) + assert "The package must define a 'provider' variable" in result.output diff --git a/packages/ref-celery/tests/test_tasks.py b/packages/ref-celery/tests/test_tasks.py new file mode 100644 index 0000000..c535280 --- /dev/null +++ b/packages/ref-celery/tests/test_tasks.py @@ -0,0 +1 @@ +# TODO: add tests for executing tasks diff --git a/uv.lock b/uv.lock index 8c7a2ba..1d09c28 100644 --- a/uv.lock +++ b/uv.lock @@ -2330,6 +2330,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/78/3a/af5b4fa5961d9a1e6237b530eb87dd04aea6eb83da09d2a4073d81b54ccf/pytest_cov-5.0.0-py3-none-any.whl", hash = "sha256:4f0764a1219df53214206bf1feea4633c3b558a2925c8b59f144f682861ce652", size = 21990 }, ] +[[package]] +name = "pytest-mock" +version = "3.14.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pytest" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/c6/90/a955c3ab35ccd41ad4de556596fa86685bf4fc5ffcc62d22d856cfd4e29a/pytest-mock-3.14.0.tar.gz", hash = "sha256:2719255a1efeceadbc056d6bf3df3d1c5015530fb40cf347c0f9afac88410bd0", size = 32814 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f2/3b/b26f90f74e2986a82df6e7ac7e319b8ea7ccece1caec9f8ab6104dc70603/pytest_mock-3.14.0-py3-none-any.whl", hash = "sha256:0b72c38033392a5f4621342fe11e9219ac11ec9d375f8e2a0c164539e0d70f6f", size = 9863 }, +] + [[package]] name = "python-dateutil" version = "2.9.0.post0" @@ -2552,6 +2564,11 @@ dependencies = [ { name = "typer" }, ] +[package.dev-dependencies] +dev = [ + { name = "pytest-mock" }, +] + [package.metadata] requires-dist = [ { name = "celery", extras = ["redis"], specifier = ">=5.4.0" }, @@ -2561,6 +2578,9 @@ requires-dist = [ { name = "typer", specifier = ">=0.12.0" }, ] +[package.metadata.requires-dev] +dev = [{ name = "pytest-mock", specifier = ">=3.14.0" }] + [[package]] name = "ref-core" version = "0.1.0"