Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pytest-dotenv #30

Merged
merged 9 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading