Skip to content

Commit

Permalink
feat: add pytest-dotenv (#30)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FacerAin authored Dec 6, 2023
1 parent 44e6fe0 commit 5c8b021
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions app/agent/retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[pytest]
markers =
live: For testing actual API interactions (deselect with '-m "not live"')
env_files =
.env
3 changes: 3 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest==7.0.1
pytest-dotenv==0.5.2
httpx==0.24.1
25 changes: 13 additions & 12 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import pinecone
import pytest
from fastapi.testclient import TestClient

from app.agent import ExecutorAgent
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
assert "answer" in response.json()
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"}
Expand Down

0 comments on commit 5c8b021

Please sign in to comment.