From 9f7704e0e8da79b2557e9a4a52a8371dd5520c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pstr=C4=85g?= Date: Thu, 17 Oct 2024 10:40:17 +0200 Subject: [PATCH] add tests for require_dependencies decorator --- .../tests/unit/utils/test_decorators.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 packages/ragbits-core/tests/unit/utils/test_decorators.py diff --git a/packages/ragbits-core/tests/unit/utils/test_decorators.py b/packages/ragbits-core/tests/unit/utils/test_decorators.py new file mode 100644 index 00000000..49752e3d --- /dev/null +++ b/packages/ragbits-core/tests/unit/utils/test_decorators.py @@ -0,0 +1,47 @@ +import pytest + +from ragbits.core.utils.decorators import requires_dependencies + + +def test_single_dependency_installed() -> None: + @requires_dependencies("pytest") + def some_function() -> str: + return "success" + + assert some_function() == "success" + + +def test_single_dependency_missing() -> None: + @requires_dependencies("nonexistent_dependency") + def some_function() -> str: + return "success" + + with pytest.raises(ImportError) as exc: + some_function() + + assert ( + str(exc.value) + == "Following dependencies are missing: nonexistent_dependency. Please install them using `pip install nonexistent_dependency`." + ) + + +def test_multiple_dependencies_installed() -> None: + @requires_dependencies(["pytest", "asyncio"]) + def some_function() -> str: + return "success" + + assert some_function() == "success" + + +def test_multiple_dependencies_some_missing() -> None: + @requires_dependencies(["pytest", "nonexistent_dependency"]) + def some_function() -> str: + return "success" + + with pytest.raises(ImportError) as exc: + some_function() + + assert ( + str(exc.value) + == "Following dependencies are missing: nonexistent_dependency. Please install them using `pip install nonexistent_dependency`." + )