From 0dfe63af2a674fb52c757fa0b92a5c4722ae5b43 Mon Sep 17 00:00:00 2001 From: Chester Curme Date: Tue, 17 Sep 2024 19:18:11 -0400 Subject: [PATCH] update --- .github/workflows/_extended_test.yml | 3 +- .github/workflows/_integration_test.yml | 2 +- .../tests/integration_tests/conftest.py | 87 ++++++++++++++++++- .../document_loaders/test_pdf.py | 4 +- 4 files changed, 91 insertions(+), 5 deletions(-) diff --git a/.github/workflows/_extended_test.yml b/.github/workflows/_extended_test.yml index fa010f3d54e..2a002dcfb6c 100644 --- a/.github/workflows/_extended_test.yml +++ b/.github/workflows/_extended_test.yml @@ -45,7 +45,7 @@ jobs: - name: Install extended dependencies shell: bash run: | - poetry install --with test + poetry install --with test,test_integration poetry run pip install uv poetry run uv pip install -r extended_dependencies/${{ inputs.extended-deps-file }} @@ -58,6 +58,7 @@ jobs: shell: bash run: | make test + make integration_tests - name: Ensure the tests did not create any additional files shell: bash diff --git a/.github/workflows/_integration_test.yml b/.github/workflows/_integration_test.yml index 56035db65ee..2a002dcfb6c 100644 --- a/.github/workflows/_integration_test.yml +++ b/.github/workflows/_integration_test.yml @@ -45,7 +45,7 @@ jobs: - name: Install extended dependencies shell: bash run: | - poetry install --with test + poetry install --with test,test_integration poetry run pip install uv poetry run uv pip install -r extended_dependencies/${{ inputs.extended-deps-file }} diff --git a/libs/community/tests/integration_tests/conftest.py b/libs/community/tests/integration_tests/conftest.py index 02b518e8695..477b3c51855 100644 --- a/libs/community/tests/integration_tests/conftest.py +++ b/libs/community/tests/integration_tests/conftest.py @@ -1,6 +1,11 @@ -# Getting the absolute path of the current file's directory +from importlib import util import os +from typing import Dict, Sequence +import pytest +from pytest import Config, Function, Parser + +# Getting the absolute path of the current file's directory ABS_PATH = os.path.dirname(os.path.abspath(__file__)) # Getting the absolute path of the project's root directory @@ -17,3 +22,83 @@ def _load_env() -> None: _load_env() + +def pytest_addoption(parser: Parser) -> None: + """Add custom command line options to pytest.""" + parser.addoption( + "--only-extended", + action="store_true", + help="Only run extended tests. Does not allow skipping any extended tests.", + ) + parser.addoption( + "--only-core", + action="store_true", + help="Only run core tests. Never runs any extended tests.", + ) + + +def pytest_collection_modifyitems(config: Config, items: Sequence[Function]) -> None: + """Add implementations for handling custom markers. + + At the moment, this adds support for a custom `requires` marker. + + The `requires` marker is used to denote tests that require one or more packages + to be installed to run. If the package is not installed, the test is skipped. + + The `requires` marker syntax is: + + .. code-block:: python + + @pytest.mark.requires("package1", "package2") + def test_something(): + ... + """ + # Mapping from the name of a package to whether it is installed or not. + # Used to avoid repeated calls to `util.find_spec` + required_pkgs_info: Dict[str, bool] = {} + + only_extended = config.getoption("--only-extended") or False + only_core = config.getoption("--only-core") or False + + if only_extended and only_core: + raise ValueError("Cannot specify both `--only-extended` and `--only-core`.") + + for item in items: + requires_marker = item.get_closest_marker("requires") + if requires_marker is not None: + if only_core: + item.add_marker(pytest.mark.skip(reason="Skipping not a core test.")) + continue + + # Iterate through the list of required packages + required_pkgs = requires_marker.args + for pkg in required_pkgs: + # If we haven't yet checked whether the pkg is installed + # let's check it and store the result. + if pkg not in required_pkgs_info: + try: + installed = util.find_spec(pkg) is not None + except Exception: + installed = False + required_pkgs_info[pkg] = installed + + if not required_pkgs_info[pkg]: + if only_extended: + pytest.fail( + f"Package `{pkg}` is not installed but is required for " + f"extended tests. Please install the given package and " + f"try again.", + ) + + else: + # If the package is not installed, we immediately break + # and mark the test as skipped. + item.add_marker( + pytest.mark.skip(reason=f"Requires pkg: `{pkg}`") + ) + break + else: + if only_extended: + item.add_marker( + pytest.mark.skip(reason="Skipping not an extended test.") + ) diff --git a/libs/community/tests/integration_tests/document_loaders/test_pdf.py b/libs/community/tests/integration_tests/document_loaders/test_pdf.py index bdde35d2998..e19a084a871 100644 --- a/libs/community/tests/integration_tests/document_loaders/test_pdf.py +++ b/libs/community/tests/integration_tests/document_loaders/test_pdf.py @@ -86,8 +86,8 @@ def test_pdfminer_pdf_as_html_loader() -> None: assert len(docs) == 1 -@pytest.mark.requires("pypdf") @pytest.mark.runs +@pytest.mark.requires("pypdf") def test_pypdf_loader() -> None: """Test PyPDFLoader.""" file_path = Path(__file__).parent.parent / "examples/hello.pdf" @@ -103,8 +103,8 @@ def test_pypdf_loader() -> None: assert len(docs) == 16 -@pytest.mark.requires("pypdf") @pytest.mark.runs +@pytest.mark.requires("pypdf") def test_pypdf_loader_with_layout() -> None: """Test PyPDFLoader with layout mode.""" file_path = Path(__file__).parent.parent / "examples/layout-parser-paper.pdf"