2024-01-12 05:41:24 +00:00
|
|
|
"""Implement integration tests for AstraDB storage."""
|
infra: update mypy 1.10, ruff 0.5 (#23721)
```python
"""python scripts/update_mypy_ruff.py"""
import glob
import tomllib
from pathlib import Path
import toml
import subprocess
import re
ROOT_DIR = Path(__file__).parents[1]
def main():
for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True):
print(path)
with open(path, "rb") as f:
pyproject = tomllib.load(f)
try:
pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = (
"^1.10"
)
pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = (
"^0.5"
)
except KeyError:
continue
with open(path, "w") as f:
toml.dump(pyproject, f)
cwd = "/".join(path.split("/")[:-1])
completed = subprocess.run(
"poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color",
cwd=cwd,
shell=True,
capture_output=True,
text=True,
)
logs = completed.stdout.split("\n")
to_ignore = {}
for l in logs:
if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l):
path, line_no, error_type = re.match(
"^(.*)\:(\d+)\: error:.*\[(.*)\]", l
).groups()
if (path, line_no) in to_ignore:
to_ignore[(path, line_no)].append(error_type)
else:
to_ignore[(path, line_no)] = [error_type]
print(len(to_ignore))
for (error_path, line_no), error_types in to_ignore.items():
all_errors = ", ".join(error_types)
full_path = f"{cwd}/{error_path}"
try:
with open(full_path, "r") as f:
file_lines = f.readlines()
except FileNotFoundError:
continue
file_lines[int(line_no) - 1] = (
file_lines[int(line_no) - 1][:-1] + f" # type: ignore[{all_errors}]\n"
)
with open(full_path, "w") as f:
f.write("".join(file_lines))
subprocess.run(
"poetry run ruff format .; poetry run ruff --select I --fix .",
cwd=cwd,
shell=True,
capture_output=True,
text=True,
)
if __name__ == "__main__":
main()
```
2024-07-03 17:33:27 +00:00
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-12 05:41:24 +00:00
|
|
|
import os
|
2024-02-19 18:11:49 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2024-01-12 05:41:24 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from langchain_community.storage.astradb import AstraDBByteStore, AstraDBStore
|
2024-02-19 18:11:49 +00:00
|
|
|
from langchain_community.utilities.astradb import SetupMode
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from astrapy.db import AstraDB, AsyncAstraDB
|
2024-01-12 05:41:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _has_env_vars() -> bool:
|
|
|
|
return all(
|
|
|
|
[
|
|
|
|
"ASTRA_DB_APPLICATION_TOKEN" in os.environ,
|
|
|
|
"ASTRA_DB_API_ENDPOINT" in os.environ,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-02-19 18:11:49 +00:00
|
|
|
def astra_db() -> AstraDB:
|
2024-01-12 05:41:24 +00:00
|
|
|
from astrapy.db import AstraDB
|
|
|
|
|
|
|
|
return AstraDB(
|
|
|
|
token=os.environ["ASTRA_DB_APPLICATION_TOKEN"],
|
|
|
|
api_endpoint=os.environ["ASTRA_DB_API_ENDPOINT"],
|
|
|
|
namespace=os.environ.get("ASTRA_DB_KEYSPACE"),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def async_astra_db() -> AsyncAstraDB:
|
|
|
|
from astrapy.db import AsyncAstraDB
|
|
|
|
|
|
|
|
return AsyncAstraDB(
|
|
|
|
token=os.environ["ASTRA_DB_APPLICATION_TOKEN"],
|
|
|
|
api_endpoint=os.environ["ASTRA_DB_API_ENDPOINT"],
|
|
|
|
namespace=os.environ.get("ASTRA_DB_KEYSPACE"),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def init_store(astra_db: AstraDB, collection_name: str) -> AstraDBStore:
|
2024-01-12 05:41:24 +00:00
|
|
|
store = AstraDBStore(collection_name=collection_name, astra_db_client=astra_db)
|
|
|
|
store.mset([("key1", [0.1, 0.2]), ("key2", "value2")])
|
|
|
|
return store
|
|
|
|
|
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
def init_bytestore(astra_db: AstraDB, collection_name: str) -> AstraDBByteStore:
|
2024-01-12 05:41:24 +00:00
|
|
|
store = AstraDBByteStore(collection_name=collection_name, astra_db_client=astra_db)
|
|
|
|
store.mset([("key1", b"value1"), ("key2", b"value2")])
|
|
|
|
return store
|
|
|
|
|
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
async def init_async_store(
|
|
|
|
async_astra_db: AsyncAstraDB, collection_name: str
|
|
|
|
) -> AstraDBStore:
|
|
|
|
store = AstraDBStore(
|
|
|
|
collection_name=collection_name,
|
|
|
|
async_astra_db_client=async_astra_db,
|
|
|
|
setup_mode=SetupMode.ASYNC,
|
|
|
|
)
|
|
|
|
await store.amset([("key1", [0.1, 0.2]), ("key2", "value2")])
|
|
|
|
return store
|
|
|
|
|
|
|
|
|
2024-01-12 05:41:24 +00:00
|
|
|
@pytest.mark.requires("astrapy")
|
|
|
|
@pytest.mark.skipif(not _has_env_vars(), reason="Missing Astra DB env. vars")
|
|
|
|
class TestAstraDBStore:
|
2024-02-19 18:11:49 +00:00
|
|
|
def test_mget(self, astra_db: AstraDB) -> None:
|
2024-01-12 05:41:24 +00:00
|
|
|
"""Test AstraDBStore mget method."""
|
|
|
|
collection_name = "lc_test_store_mget"
|
|
|
|
try:
|
|
|
|
store = init_store(astra_db, collection_name)
|
|
|
|
assert store.mget(["key1", "key2"]) == [[0.1, 0.2], "value2"]
|
|
|
|
finally:
|
|
|
|
astra_db.delete_collection(collection_name)
|
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
async def test_amget(self, async_astra_db: AsyncAstraDB) -> None:
|
|
|
|
"""Test AstraDBStore amget method."""
|
|
|
|
collection_name = "lc_test_store_mget"
|
|
|
|
try:
|
|
|
|
store = await init_async_store(async_astra_db, collection_name)
|
|
|
|
assert await store.amget(["key1", "key2"]) == [[0.1, 0.2], "value2"]
|
|
|
|
finally:
|
|
|
|
await async_astra_db.delete_collection(collection_name)
|
|
|
|
|
|
|
|
def test_mset(self, astra_db: AstraDB) -> None:
|
2024-01-12 05:41:24 +00:00
|
|
|
"""Test that multiple keys can be set with AstraDBStore."""
|
|
|
|
collection_name = "lc_test_store_mset"
|
|
|
|
try:
|
|
|
|
store = init_store(astra_db, collection_name)
|
|
|
|
result = store.collection.find_one({"_id": "key1"})
|
|
|
|
assert result["data"]["document"]["value"] == [0.1, 0.2]
|
|
|
|
result = store.collection.find_one({"_id": "key2"})
|
|
|
|
assert result["data"]["document"]["value"] == "value2"
|
|
|
|
finally:
|
|
|
|
astra_db.delete_collection(collection_name)
|
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
async def test_amset(self, async_astra_db: AsyncAstraDB) -> None:
|
|
|
|
"""Test that multiple keys can be set with AstraDBStore."""
|
|
|
|
collection_name = "lc_test_store_mset"
|
|
|
|
try:
|
|
|
|
store = await init_async_store(async_astra_db, collection_name)
|
|
|
|
result = await store.async_collection.find_one({"_id": "key1"})
|
|
|
|
assert result["data"]["document"]["value"] == [0.1, 0.2]
|
|
|
|
result = await store.async_collection.find_one({"_id": "key2"})
|
|
|
|
assert result["data"]["document"]["value"] == "value2"
|
|
|
|
finally:
|
|
|
|
await async_astra_db.delete_collection(collection_name)
|
|
|
|
|
|
|
|
def test_mdelete(self, astra_db: AstraDB) -> None:
|
2024-01-12 05:41:24 +00:00
|
|
|
"""Test that deletion works as expected."""
|
|
|
|
collection_name = "lc_test_store_mdelete"
|
|
|
|
try:
|
|
|
|
store = init_store(astra_db, collection_name)
|
|
|
|
store.mdelete(["key1", "key2"])
|
|
|
|
result = store.mget(["key1", "key2"])
|
|
|
|
assert result == [None, None]
|
|
|
|
finally:
|
|
|
|
astra_db.delete_collection(collection_name)
|
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
async def test_amdelete(self, async_astra_db: AsyncAstraDB) -> None:
|
|
|
|
"""Test that deletion works as expected."""
|
|
|
|
collection_name = "lc_test_store_mdelete"
|
|
|
|
try:
|
|
|
|
store = await init_async_store(async_astra_db, collection_name)
|
|
|
|
await store.amdelete(["key1", "key2"])
|
|
|
|
result = await store.amget(["key1", "key2"])
|
|
|
|
assert result == [None, None]
|
|
|
|
finally:
|
|
|
|
await async_astra_db.delete_collection(collection_name)
|
|
|
|
|
|
|
|
def test_yield_keys(self, astra_db: AstraDB) -> None:
|
2024-01-12 05:41:24 +00:00
|
|
|
collection_name = "lc_test_store_yield_keys"
|
|
|
|
try:
|
|
|
|
store = init_store(astra_db, collection_name)
|
|
|
|
assert set(store.yield_keys()) == {"key1", "key2"}
|
|
|
|
assert set(store.yield_keys(prefix="key")) == {"key1", "key2"}
|
|
|
|
assert set(store.yield_keys(prefix="lang")) == set()
|
|
|
|
finally:
|
|
|
|
astra_db.delete_collection(collection_name)
|
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
async def test_ayield_keys(self, async_astra_db: AsyncAstraDB) -> None:
|
|
|
|
collection_name = "lc_test_store_yield_keys"
|
|
|
|
try:
|
|
|
|
store = await init_async_store(async_astra_db, collection_name)
|
|
|
|
assert {key async for key in store.ayield_keys()} == {"key1", "key2"}
|
|
|
|
assert {key async for key in store.ayield_keys(prefix="key")} == {
|
|
|
|
"key1",
|
|
|
|
"key2",
|
|
|
|
}
|
|
|
|
assert {key async for key in store.ayield_keys(prefix="lang")} == set()
|
|
|
|
finally:
|
|
|
|
await async_astra_db.delete_collection(collection_name)
|
|
|
|
|
|
|
|
def test_bytestore_mget(self, astra_db: AstraDB) -> None:
|
2024-01-12 05:41:24 +00:00
|
|
|
"""Test AstraDBByteStore mget method."""
|
|
|
|
collection_name = "lc_test_bytestore_mget"
|
|
|
|
try:
|
|
|
|
store = init_bytestore(astra_db, collection_name)
|
|
|
|
assert store.mget(["key1", "key2"]) == [b"value1", b"value2"]
|
|
|
|
finally:
|
|
|
|
astra_db.delete_collection(collection_name)
|
|
|
|
|
2024-02-19 18:11:49 +00:00
|
|
|
def test_bytestore_mset(self, astra_db: AstraDB) -> None:
|
2024-01-12 05:41:24 +00:00
|
|
|
"""Test that multiple keys can be set with AstraDBByteStore."""
|
|
|
|
collection_name = "lc_test_bytestore_mset"
|
|
|
|
try:
|
|
|
|
store = init_bytestore(astra_db, collection_name)
|
|
|
|
result = store.collection.find_one({"_id": "key1"})
|
|
|
|
assert result["data"]["document"]["value"] == "dmFsdWUx"
|
|
|
|
result = store.collection.find_one({"_id": "key2"})
|
|
|
|
assert result["data"]["document"]["value"] == "dmFsdWUy"
|
|
|
|
finally:
|
|
|
|
astra_db.delete_collection(collection_name)
|