From f2e53ae289723e380c81cb5ec758e587813b0ddf Mon Sep 17 00:00:00 2001 From: Ben Chambers <35960+bjchambers@users.noreply.github.com> Date: Fri, 17 Jan 2025 14:24:12 -0800 Subject: [PATCH 1/4] chore: Enable Astra tests for Python 3.12 only --- .github/workflows/main.yml | 18 +++++-- Makefile | 2 +- .../tests/conftest.py | 51 +++++++++++++++++++ .../tests/integration_tests/conftest.py | 51 ------------------- uv.lock | 4 +- 5 files changed, 67 insertions(+), 59 deletions(-) delete mode 100644 packages/langchain-graph-retriever/tests/integration_tests/conftest.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e598c6ad..c7f59b2a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -66,12 +66,20 @@ jobs: with: python-version: ${{ matrix.python-version }} - - name: Run `make test` + - name: Test All Stores (3.12 only) + if: ${{ matrix.python-version == '3.12' }} run: - uv run --package langchain-graph-retriever python -m pytest -vs - ./packages/langchain-graph-retriever/tests/unit_tests/ - ./packages/langchain-graph-retriever/tests/integration_tests/ - --stores=mem_norm --stores=mem --stores=cassandra --stores=chroma --stores=opensearch + cd packages/langchain-graph-retriever && uv run pytest -vs . \ + --stores=all \ + --junitxml=junit/test-results-${{ matrix.python-version }}.xml + env: + ASTRA_DB_APPLICATION_TOKEN: ${{ secrets.ASTRA_DB_APPLICATION_TOKEN }} + ASTRA_DB_API_ENDPOINT: ${{ secrets.ASTRA_DB_API_ENDPOINT }} + + - name: Test In-Memory Stores (non-3.12) + if: ${{ matrix.python-version != '3.12' }} + run: + cd packages/langchain-graph-retriever && uv run pytest -vs . \ --junitxml=junit/test-results-${{ matrix.python-version }}.xml - name: Run `make mypy` diff --git a/Makefile b/Makefile index ba7cfa3f..6fd64028 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,7 @@ unit: .PHONY: test test: sync-langchain-graph-retriever @echo "🚀 Testing code: Running pytest" - @cd packages/langchain-graph-retriever && uv run pytest -vs ./tests/unit_tests ./tests/integration_tests --stores=all + @cd packages/langchain-graph-retriever && uv run pytest -vs . --stores=all .PHONY: mypy mypy: diff --git a/packages/langchain-graph-retriever/tests/conftest.py b/packages/langchain-graph-retriever/tests/conftest.py index e69de29b..d850cf2f 100644 --- a/packages/langchain-graph-retriever/tests/conftest.py +++ b/packages/langchain-graph-retriever/tests/conftest.py @@ -0,0 +1,51 @@ +import asyncio + +import pytest +from pytest import Parser +from tests.integration_tests.stores import ALL_STORES, TESTCONTAINER_STORES + + +@pytest.fixture(scope="session") +def event_loop(): + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + yield loop + loop.close() + + +def pytest_addoption(parser: Parser): + parser.addoption( + "--stores", + action="append", + metavar="STORE", + choices=ALL_STORES + ["all"], + help="run tests for the given store (default: 'mem' and 'mem_norm')", + ) + parser.addoption( + "--testcontainer", + action="append", + metavar="STORE", + choices=TESTCONTAINER_STORES + ["none"], + help="which stores to run testcontainers for (default: 'all')", + ) + + +# TODO: Allow marking tests as only supporting a subset of stores? +# +# def pytest_configure(config): +# # register an additional marker +# config.addinivalue_line( +# "markers", "svc(name): tests that require the named service" +# ) +# +# def pytest_runtest_setup(item): +# """Skip the test unless all of the marked services are present.""" +# +# required_svcs = {mark.args[0] for mark in item.iter_markers(name="svc")} +# provided_svcs = set(item.config.getoption("-S") or []) +# +# missing_svcs = required_svcs - provided_svcs +# if missing_svcs: +# pytest.skip(f"test requires services {missing_svcs!r}") diff --git a/packages/langchain-graph-retriever/tests/integration_tests/conftest.py b/packages/langchain-graph-retriever/tests/integration_tests/conftest.py deleted file mode 100644 index d850cf2f..00000000 --- a/packages/langchain-graph-retriever/tests/integration_tests/conftest.py +++ /dev/null @@ -1,51 +0,0 @@ -import asyncio - -import pytest -from pytest import Parser -from tests.integration_tests.stores import ALL_STORES, TESTCONTAINER_STORES - - -@pytest.fixture(scope="session") -def event_loop(): - try: - loop = asyncio.get_running_loop() - except RuntimeError: - loop = asyncio.new_event_loop() - yield loop - loop.close() - - -def pytest_addoption(parser: Parser): - parser.addoption( - "--stores", - action="append", - metavar="STORE", - choices=ALL_STORES + ["all"], - help="run tests for the given store (default: 'mem' and 'mem_norm')", - ) - parser.addoption( - "--testcontainer", - action="append", - metavar="STORE", - choices=TESTCONTAINER_STORES + ["none"], - help="which stores to run testcontainers for (default: 'all')", - ) - - -# TODO: Allow marking tests as only supporting a subset of stores? -# -# def pytest_configure(config): -# # register an additional marker -# config.addinivalue_line( -# "markers", "svc(name): tests that require the named service" -# ) -# -# def pytest_runtest_setup(item): -# """Skip the test unless all of the marked services are present.""" -# -# required_svcs = {mark.args[0] for mark in item.iter_markers(name="svc")} -# provided_svcs = set(item.config.getoption("-S") or []) -# -# missing_svcs = required_svcs - provided_svcs -# if missing_svcs: -# pytest.skip(f"test requires services {missing_svcs!r}") diff --git a/uv.lock b/uv.lock index 7445b764..11a437e4 100644 --- a/uv.lock +++ b/uv.lock @@ -788,7 +788,7 @@ wheels = [ [[package]] name = "graph-retriever" -version = "0.0.0" +version = "0.1.0" source = { editable = "packages/graph-retriever" } [package.metadata] @@ -1272,7 +1272,7 @@ wheels = [ [[package]] name = "langchain-graph-retriever" -version = "0.0.1" +version = "0.1.0" source = { editable = "packages/langchain-graph-retriever" } dependencies = [ { name = "langchain-core" }, From d6f8b51c03b8dd993f3ada99c68f14189ea0a4b3 Mon Sep 17 00:00:00 2001 From: Ben Chambers <35960+bjchambers@users.noreply.github.com> Date: Fri, 17 Jan 2025 14:27:25 -0800 Subject: [PATCH 2/4] tweak tests --- .github/workflows/main.yml | 6 +++--- packages/langchain-graph-retriever/tests/conftest.py | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c7f59b2a..cbdc1132 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -69,8 +69,8 @@ jobs: - name: Test All Stores (3.12 only) if: ${{ matrix.python-version == '3.12' }} run: - cd packages/langchain-graph-retriever && uv run pytest -vs . \ - --stores=all \ + cd packages/langchain-graph-retriever && uv run pytest -vs . + --stores=all --junitxml=junit/test-results-${{ matrix.python-version }}.xml env: ASTRA_DB_APPLICATION_TOKEN: ${{ secrets.ASTRA_DB_APPLICATION_TOKEN }} @@ -79,7 +79,7 @@ jobs: - name: Test In-Memory Stores (non-3.12) if: ${{ matrix.python-version != '3.12' }} run: - cd packages/langchain-graph-retriever && uv run pytest -vs . \ + cd packages/langchain-graph-retriever && uv run pytest -vs . --junitxml=junit/test-results-${{ matrix.python-version }}.xml - name: Run `make mypy` diff --git a/packages/langchain-graph-retriever/tests/conftest.py b/packages/langchain-graph-retriever/tests/conftest.py index d850cf2f..e52d6de0 100644 --- a/packages/langchain-graph-retriever/tests/conftest.py +++ b/packages/langchain-graph-retriever/tests/conftest.py @@ -2,6 +2,7 @@ import pytest from pytest import Parser + from tests.integration_tests.stores import ALL_STORES, TESTCONTAINER_STORES From 89ece396d61296cfb6190f12fd66ef37a6d21464 Mon Sep 17 00:00:00 2001 From: Ben Chambers <35960+bjchambers@users.noreply.github.com> Date: Fri, 17 Jan 2025 14:33:31 -0800 Subject: [PATCH 3/4] run in a dedicated keyspace --- .github/workflows/main.yml | 18 ++++++++++++++---- .../tests/integration_tests/stores.py | 4 ++++ scripts/drop-astra-keyspace.py | 12 ++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 scripts/drop-astra-keyspace.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cbdc1132..fd626bbc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -67,17 +67,27 @@ jobs: python-version: ${{ matrix.python-version }} - name: Test All Stores (3.12 only) - if: ${{ matrix.python-version == '3.12' }} + if: ${{ success() && matrix.python-version == '3.12' }} run: cd packages/langchain-graph-retriever && uv run pytest -vs . --stores=all --junitxml=junit/test-results-${{ matrix.python-version }}.xml env: - ASTRA_DB_APPLICATION_TOKEN: ${{ secrets.ASTRA_DB_APPLICATION_TOKEN }} - ASTRA_DB_API_ENDPOINT: ${{ secrets.ASTRA_DB_API_ENDPOINT }} + ASTRA_DB_APPLICATION_TOKEN: ${{ secrets.ASTRA_DB_APPLICATION_TOKEN }} + ASTRA_DB_API_ENDPOINT: ${{ secrets.ASTRA_DB_API_ENDPOINT }} + ASTRA_DB_KEYSPACE: ci_${{ github.run_id }}_${{ strategy.job-index }}_${{ github.run_attempt }} + + - name: Drop Astra Keyspace + if: ${{ matrix.python-version == '3.12' }} + run: + uv run scripts/drop-astra-keyspace.py + env: + ASTRA_DB_APPLICATION_TOKEN: ${{ secrets.ASTRA_DB_APPLICATION_TOKEN }} + ASTRA_DB_API_ENDPOINT: ${{ secrets.ASTRA_DB_API_ENDPOINT }} + ASTRA_DB_KEYSPACE: ci_${{ github.run_id }}_${{ strategy.job-index }}_${{ github.run_attempt }} - name: Test In-Memory Stores (non-3.12) - if: ${{ matrix.python-version != '3.12' }} + if: ${{ success() && matrix.python-version != '3.12' }} run: cd packages/langchain-graph-retriever && uv run pytest -vs . --junitxml=junit/test-results-${{ matrix.python-version }}.xml diff --git a/packages/langchain-graph-retriever/tests/integration_tests/stores.py b/packages/langchain-graph-retriever/tests/integration_tests/stores.py index 0ac746ed..e63ed1cf 100644 --- a/packages/langchain-graph-retriever/tests/integration_tests/stores.py +++ b/packages/langchain-graph-retriever/tests/integration_tests/stores.py @@ -198,6 +198,7 @@ def teardown_open_search(store: OpenSearchVectorSearch) -> None: def _astra_store_factory(_request: pytest.FixtureRequest) -> StoreFactory: import os + from astrapy import AstraDBDatabaseAdmin from astrapy.authentication import StaticTokenProvider from dotenv import load_dotenv from langchain_astradb import AstraDBVectorStore @@ -211,6 +212,9 @@ def _astra_store_factory(_request: pytest.FixtureRequest) -> StoreFactory: keyspace = os.environ.get("ASTRA_DB_KEYSPACE", "default_keyspace") api_endpoint = os.environ["ASTRA_DB_API_ENDPOINT"] + admin = AstraDBDatabaseAdmin(api_endpoint=api_endpoint, token=token) + admin.create_keyspace(keyspace) + def create_astra( name: str, docs: list[Document], embedding: Embeddings ) -> AstraDBVectorStore: diff --git a/scripts/drop-astra-keyspace.py b/scripts/drop-astra-keyspace.py new file mode 100644 index 00000000..0fbdc58d --- /dev/null +++ b/scripts/drop-astra-keyspace.py @@ -0,0 +1,12 @@ +import os + +from astrapy import AstraDBDatabaseAdmin +from astrapy.authentication import StaticTokenProvider + +token = StaticTokenProvider(os.environ["ASTRA_DB_APPLICATION_TOKEN"]) +keyspace = os.environ.get("ASTRA_DB_KEYSPACE", "default_keyspace") +api_endpoint = os.environ["ASTRA_DB_API_ENDPOINT"] + +admin = AstraDBDatabaseAdmin(api_endpoint=api_endpoint, token=token) +if keyspace in admin.list_keyspaces(): + admin.drop_keyspace(keyspace) From bfdafc1617e1e5276ded72103dd542041f1da9d5 Mon Sep 17 00:00:00 2001 From: Ben Chambers <35960+bjchambers@users.noreply.github.com> Date: Fri, 17 Jan 2025 14:46:21 -0800 Subject: [PATCH 4/4] fix junit upload --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fd626bbc..f45b92f6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -100,7 +100,7 @@ jobs: uses: actions/upload-artifact@v4 with: name: pytest-results-${{ matrix.python-version }} - path: junit/test-results-${{ matrix.python-version }}.xml + path: packages/*/junit/test-results-${{ matrix.python-version }}.xml # check-docs: # runs-on: ubuntu-latest