Skip to content

Commit

Permalink
added pytests for config, api and dependency testing
Browse files Browse the repository at this point in the history
  • Loading branch information
iam-yashpradhan committed Nov 25, 2024
1 parent aa85b45 commit a33cc74
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MinuteMate/back/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# pip install pytest pytest-asyncio pytest-cov

# Running the tests
# pytest --cov=app tests/ -v
25 changes: 25 additions & 0 deletions MinuteMate/back/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from fastapi.testclient import TestClient
from unittest.mock import Mock, patch
from typing import Generator

from app.main import app # Adjust import path as needed

@pytest.fixture
def test_client() -> Generator:
with TestClient(app) as client:
yield client

@pytest.fixture
def mock_weaviate_client():
with patch('weaviate.connect_to_weaviate_cloud') as mock:
mock_client = Mock()
mock.return_value = mock_client
yield mock_client

@pytest.fixture
def mock_openai_client():
with patch('openai.OpenAI') as mock:
mock_client = Mock()
mock.return_value = mock_client
yield mock_client
25 changes: 25 additions & 0 deletions MinuteMate/back/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

Check failure on line 1 in MinuteMate/back/tests/test_api.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

MinuteMate/back/tests/test_api.py:1:8: F401 `pytest` imported but unused

Check failure on line 1 in MinuteMate/back/tests/test_api.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

MinuteMate/back/tests/test_api.py:1:8: F401 `pytest` imported but unused
from fastapi.testclient import TestClient

Check failure on line 2 in MinuteMate/back/tests/test_api.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

MinuteMate/back/tests/test_api.py:2:32: F401 `fastapi.testclient.TestClient` imported but unused

Check failure on line 2 in MinuteMate/back/tests/test_api.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

MinuteMate/back/tests/test_api.py:2:32: F401 `fastapi.testclient.TestClient` imported but unused
from app.main import app # Adjust import path

Check failure on line 3 in MinuteMate/back/tests/test_api.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

MinuteMate/back/tests/test_api.py:3:22: F401 `app.main.app` imported but unused

Check failure on line 3 in MinuteMate/back/tests/test_api.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

MinuteMate/back/tests/test_api.py:3:22: F401 `app.main.app` imported but unused

def test_process_prompt_valid_request(test_client):
response = test_client.post(
"/process-prompt",
json={"user_prompt_text": "Test prompt"}
)
assert response.status_code == 200

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert "generated_response" in response.json()

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

def test_process_prompt_invalid_request(test_client):
response = test_client.post(
"/process-prompt",
json={"user_prompt_text": ""}
)
assert response.status_code == 422

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

def test_process_prompt_long_text(test_client):
response = test_client.post(
"/process-prompt",
json={"user_prompt_text": "a" * 1001}
)
assert response.status_code == 422

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
30 changes: 30 additions & 0 deletions MinuteMate/back/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
import os
from unittest.mock import patch
from app.main import WeaviateConfig, PromptProcessor # Adjust import path

def test_weaviate_config_initialization():
test_url = "https://test-url"
test_api_key = "test-key"

with patch('weaviate.connect_to_weaviate_cloud') as mock_connect:
WeaviateConfig.get_weaviate_client(test_url, test_api_key)
mock_connect.assert_called_once()

def test_prompt_processor_env_vars():
test_env_vars = {
'OPENAI_API_KEY': 'test-openai-key',
'WEAVIATE_URL': 'test-weaviate-url',
'WEAVIATE_API_KEY': 'test-weaviate-key'
}

with patch.dict(os.environ, test_env_vars):
processor = PromptProcessor()
assert processor.OPENAI_API_KEY == 'test-openai-key'

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert processor.WEAVIATE_URL == 'test-weaviate-url'

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert processor.WEAVIATE_API_KEY == 'test-weaviate-key'

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

def test_prompt_processor_missing_env_vars():
with patch.dict(os.environ, {}, clear=True):
with pytest.raises(ValueError):
PromptProcessor()
34 changes: 34 additions & 0 deletions MinuteMate/back/tests/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from app.main import PromptProcessor, PromptRequest # Adjust import path

Check failure on line 2 in MinuteMate/back/tests/test_dependencies.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

MinuteMate/back/tests/test_dependencies.py:2:39: F401 `app.main.PromptRequest` imported but unused

Check failure on line 2 in MinuteMate/back/tests/test_dependencies.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

MinuteMate/back/tests/test_dependencies.py:2:39: F401 `app.main.PromptRequest` imported but unused

def test_extract_keywords(mock_weaviate_client):
processor = PromptProcessor()
text = "This is a test meeting about project planning and team coordination"
keywords = processor.extract_keywords(text)
assert isinstance(keywords, list)

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert len(keywords) <= 3

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

def test_search_weaviate(mock_weaviate_client):
processor = PromptProcessor()

# Mock the collection query response
mock_result = Mock()

Check failure on line 15 in MinuteMate/back/tests/test_dependencies.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

MinuteMate/back/tests/test_dependencies.py:15:19: F821 Undefined name `Mock`

Check failure on line 15 in MinuteMate/back/tests/test_dependencies.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

MinuteMate/back/tests/test_dependencies.py:15:19: F821 Undefined name `Mock`
mock_result.objects = []
mock_weaviate_client.collections.get().query.bm25.return_value = mock_result

context_segments, keywords = processor.search_weaviate("test query")
assert isinstance(context_segments, list)

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert isinstance(keywords, list)

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

@pytest.mark.asyncio
async def test_process_prompt_endpoint(test_client):
test_prompt = "Test prompt"
response = test_client.post(
"/process-prompt",
json={"user_prompt_text": test_prompt}
)

assert response.status_code == 200

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert "generated_response" in response.json()

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert "context_segments" in response.json()

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert "keywords" in response.json()

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

0 comments on commit a33cc74

Please sign in to comment.