From 55baa0d153764a0b88c4ec3572d6f3065e518d1e Mon Sep 17 00:00:00 2001 From: Davis Chase <130488702+dev2049@users.noreply.github.com> Date: Thu, 18 May 2023 10:22:17 -0700 Subject: [PATCH] Update redis integration tests (#4937) --- .../vectorstores/test_redis.py | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/tests/integration_tests/vectorstores/test_redis.py b/tests/integration_tests/vectorstores/test_redis.py index 785f0ac6..1fe75586 100644 --- a/tests/integration_tests/vectorstores/test_redis.py +++ b/tests/integration_tests/vectorstores/test_redis.py @@ -1,4 +1,6 @@ """Test Redis functionality.""" +from typing import List + import pytest from langchain.docstore.document import Document @@ -8,6 +10,7 @@ from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings TEST_INDEX_NAME = "test" TEST_REDIS_URL = "redis://localhost:6379" TEST_SINGLE_RESULT = [Document(page_content="foo")] +TEST_SINGLE_WITH_METADATA_RESULT = [Document(page_content="foo", metadata={"a": "b"})] TEST_RESULT = [Document(page_content="foo"), Document(page_content="foo")] COSINE_SCORE = pytest.approx(0.05, abs=0.002) IP_SCORE = -8.0 @@ -20,18 +23,21 @@ def drop(index_name: str) -> bool: ) -def test_redis() -> None: +@pytest.fixture +def texts() -> List[str]: + return ["foo", "bar", "baz"] + + +def test_redis(texts: List[str]) -> None: """Test end to end construction and search.""" - texts = ["foo", "bar", "baz"] docsearch = Redis.from_texts(texts, FakeEmbeddings(), redis_url=TEST_REDIS_URL) output = docsearch.similarity_search("foo", k=1) assert output == TEST_SINGLE_RESULT assert drop(docsearch.index_name) -def test_redis_new_vector() -> None: +def test_redis_new_vector(texts: List[str]) -> None: """Test adding a new document""" - texts = ["foo", "bar", "baz"] docsearch = Redis.from_texts(texts, FakeEmbeddings(), redis_url=TEST_REDIS_URL) docsearch.add_texts(["foo"]) output = docsearch.similarity_search("foo", k=2) @@ -39,9 +45,8 @@ def test_redis_new_vector() -> None: assert drop(docsearch.index_name) -def test_redis_from_existing() -> None: +def test_redis_from_existing(texts: List[str]) -> None: """Test adding a new document""" - texts = ["foo", "bar", "baz"] Redis.from_texts( texts, FakeEmbeddings(), index_name=TEST_INDEX_NAME, redis_url=TEST_REDIS_URL ) @@ -53,6 +58,26 @@ def test_redis_from_existing() -> None: assert output == TEST_SINGLE_RESULT +def test_redis_from_texts_return_keys(texts: List[str]) -> None: + """Test from_texts_return_keys constructor.""" + docsearch, keys = Redis.from_texts_return_keys( + texts, FakeEmbeddings(), redis_url=TEST_REDIS_URL + ) + output = docsearch.similarity_search("foo", k=1) + assert output == TEST_SINGLE_RESULT + assert len(keys) == len(texts) + assert drop(docsearch.index_name) + + +def test_redis_from_documents(texts: List[str]) -> None: + """Test from_documents constructor.""" + docs = [Document(page_content=t, metadata={"a": "b"}) for t in texts] + docsearch = Redis.from_documents(docs, FakeEmbeddings(), redis_url=TEST_REDIS_URL) + output = docsearch.similarity_search("foo", k=1) + assert output == TEST_SINGLE_WITH_METADATA_RESULT + assert drop(docsearch.index_name) + + def test_redis_add_texts_to_existing() -> None: """Test adding a new document""" # Test creating from an existing @@ -65,9 +90,8 @@ def test_redis_add_texts_to_existing() -> None: assert drop(TEST_INDEX_NAME) -def test_cosine() -> None: +def test_cosine(texts: List[str]) -> None: """Test cosine distance.""" - texts = ["foo", "bar", "baz"] docsearch = Redis.from_texts( texts, FakeEmbeddings(), @@ -80,9 +104,8 @@ def test_cosine() -> None: assert drop(docsearch.index_name) -def test_l2() -> None: +def test_l2(texts: List[str]) -> None: """Test Flat L2 distance.""" - texts = ["foo", "bar", "baz"] docsearch = Redis.from_texts( texts, FakeEmbeddings(), redis_url=TEST_REDIS_URL, distance_metric="L2" ) @@ -92,9 +115,8 @@ def test_l2() -> None: assert drop(docsearch.index_name) -def test_ip() -> None: +def test_ip(texts: List[str]) -> None: """Test inner product distance.""" - texts = ["foo", "bar", "baz"] docsearch = Redis.from_texts( texts, FakeEmbeddings(), redis_url=TEST_REDIS_URL, distance_metric="IP" )