From 5c8b0215b013472f68c165fdd8e029e873e89484 Mon Sep 17 00:00:00 2001 From: Yong woo Song Date: Thu, 7 Dec 2023 02:58:20 +0900 Subject: [PATCH] feat: add pytest-dotenv (#30) * feat: add pytest-dotenv * chore: update test deps * chore: update pinecone test mock * chore: update test pinecone to mock * chore: delete test_similarity_search for offline * chore: update test for online * feat: add test to FakeRetriever * feat: update test action for env * feat: update test action for env --- .github/workflows/python-test.yml | 6 ++++++ Makefile | 2 +- app/agent/retriever.py | 3 +-- pytest.ini | 2 ++ test-requirements.txt | 3 +++ tests/test_client.py | 25 +++++++++++++------------ 6 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 test-requirements.txt diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 361b791..ecbc4fd 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -26,6 +26,12 @@ jobs: - name: Install dependencies run: | python3 -m pip install --upgrade pip + - name: Make envs for testing + run: | + echo "PINECONE_API_KEY=${{secrets.PINECONE_API_KEY}}" >> .env + echo "PINECONE_ENVIRONMENT_REGION=${{secrets.PINECONE_ENVIRONMENT_REGION}}" >> .env + echo "OPENAI_API_KEY=${{secrets.OPENAI_API_KEY}}" >> .env + - name: Test code (pytest) run: | make test diff --git a/Makefile b/Makefile index 23da14e..a38abd0 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ set-style-dep: pip3 install isort==5.12.0 black==23.3.0 flake8==4.0.1 set-test-dep: - pip3 install pytest==7.0.1 + pip install -r test-requirements.txt set-precommit: pip3 install pre-commit==2.17.0 diff --git a/app/agent/retriever.py b/app/agent/retriever.py index f30fbbd..e317a2e 100644 --- a/app/agent/retriever.py +++ b/app/agent/retriever.py @@ -18,13 +18,12 @@ def similarity_search(self, query: str, top_k: int = 5, **kwargs: Any): class PineconeRetriever(Retriever): - pinecone.init(api_key=settings.PINECONE_API_KEY, environment=settings.PINECONE_ENVIRONMENT_REGION) - def __init__( self, index_name: str, embedding_model: Union[Embeddings, Callable] = OpenAIEmbeddings(openai_api_key=settings.OPENAI_API_KEY), ): + pinecone.init(api_key=settings.PINECONE_API_KEY, environment=settings.PINECONE_ENVIRONMENT_REGION) self._index = self.get_pinecone_index(index_name=index_name) self._embedding_model = embedding_model diff --git a/pytest.ini b/pytest.ini index 01d0ea4..c101539 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,5 @@ [pytest] markers = live: For testing actual API interactions (deselect with '-m "not live"') +env_files = + .env diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..f325503 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,3 @@ +pytest==7.0.1 +pytest-dotenv==0.5.2 +httpx==0.24.1 diff --git a/tests/test_client.py b/tests/test_client.py index d19dc0d..a21cd07 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,3 +1,4 @@ +import pinecone import pytest from fastapi.testclient import TestClient @@ -5,14 +6,25 @@ from app.agent.retriever import PineconeRetriever from app.main import app + +class FakeRetriever(object): + def __init__(self, *args, **kwargs): + pass + + def get_relevant_doc_string(self, *args, **kwargs): + return "Hello" + + client = TestClient(app) def test_completion(monkeypatch): - def mockreturn(a, b): + def mockreturn(*args, **kwargs): return "Hello" monkeypatch.setattr(ExecutorAgent, "run", mockreturn) + monkeypatch.setattr("app.agent.retriever.PineconeRetriever", FakeRetriever) + req_body = {"query": "Hi"} response = client.post("/api/v1/chat/completion", json=req_body) assert response.status_code == 200 @@ -20,17 +32,6 @@ def mockreturn(a, b): assert "Hello" == response.json()["answer"] -def test_similarity_search(monkeypatch): - def mockreturn(a, b): - return "Hello" - - monkeypatch.setattr(PineconeRetriever, "get_relevant_doc_string", mockreturn) - response = client.get("/api/v1/chat/similarity-search?query=Hi") - assert response.status_code == 200 - assert isinstance(response.text, str) - assert '"Hello"' == response.text - - @pytest.mark.live def test_live_completion(): req_body = {"query": "Hi"}