-
Notifications
You must be signed in to change notification settings - Fork 0
testing: add unit testing #11
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d6afe5e
add testing to splitter endpoints
laurasgkadri98 e77ae92
add unit testing to ci_cd
laurasgkadri98 987380b
fix pre-commit errors
laurasgkadri98 46713dd
add requirement installation to tests
laurasgkadri98 534f266
fix pre-commit errors
laurasgkadri98 a24d520
add pytest ini_options to toml
laurasgkadri98 a697da4
add setup.py
laurasgkadri98 3522527
fix pre-commit errors
laurasgkadri98 0e81bf8
remove duplcated cov options
laurasgkadri98 60c6b89
add pytest-cov to requirements
laurasgkadri98 b1d68e2
update ci_cd
laurasgkadri98 6bd6d8f
Merge branch 'main' into test/add-unit-testing
laurasgkadri98 a9b600c
add httpx as requirement
laurasgkadri98 b994423
address comments
laurasgkadri98 1d4cb2a
Update .github/workflows/ci_cd.yml
laurasgkadri98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
fastapi==0.112.0 | ||
httpx==0.27.0 | ||
langchain==0.2.12 | ||
pydantic==2.8.2 | ||
pymupdf==1.24.9 | ||
pytest==8.3.2 | ||
pytest-cov==5.0.0 | ||
python_pptx==1.0.1 | ||
laurasgkadri98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyYAML==6.0.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from setuptools import find_packages, setup | ||
|
||
setup( | ||
name="ansys-allie-flowkit-python", | ||
version="0.1.0", | ||
packages=find_packages(include=["app", "docker", "configs"]), | ||
) | ||
RobPasMue marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Tests module.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
# Mock API key for testing | ||
MOCK_API_KEY = "test_api_key" | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mock_api_key(): | ||
"""Mock the API key for testing.""" | ||
with patch("app.config.CONFIG.flowkit_python_api_key", MOCK_API_KEY): | ||
yield |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import base64 | ||
|
||
from app.app import app | ||
from app.endpoints.splitter import validate_request | ||
from app.models.splitter import SplitterRequest | ||
from fastapi import HTTPException | ||
from fastapi.testclient import TestClient | ||
import pytest | ||
|
||
from tests.conftest import MOCK_API_KEY | ||
|
||
# Create a test client | ||
client = TestClient(app) | ||
|
||
|
||
def encode_file_to_base64(file_path): | ||
"""Encode a file to base64 string.""" | ||
with open(file_path, "rb") as file: | ||
return base64.b64encode(file.read()).decode("utf-8") | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_split_ppt(): | ||
"""Test splitting text in a PowerPoint document into chunks.""" | ||
ppt_content_base64 = encode_file_to_base64("./tests/test_files/test_presentation.pptx") | ||
request_payload = { | ||
"document_content": ppt_content_base64, | ||
"chunk_size": 100, | ||
"chunk_overlap": 10, | ||
} | ||
response = client.post("/splitter/ppt", json=request_payload, headers={"api-key": MOCK_API_KEY}) | ||
if response.status_code != 200: | ||
print(f"Response status code: {response.status_code}") | ||
print(f"Response content: {response.json()}") | ||
assert response.status_code == 200 | ||
assert "chunks" in response.json() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_split_py(): | ||
"""Test splitting Python code into chunks.""" | ||
python_code = """ | ||
def hello_world(): | ||
print("Hello, world!") | ||
""" | ||
python_code_base64 = base64.b64encode(python_code.encode()).decode("utf-8") | ||
request_payload = {"document_content": python_code_base64, "chunk_size": 50, "chunk_overlap": 5} | ||
response = client.post("/splitter/py", json=request_payload, headers={"api-key": MOCK_API_KEY}) | ||
assert response.status_code == 200 | ||
assert "chunks" in response.json() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_split_pdf(): | ||
"""Test splitting text in a PDF document into chunks.""" | ||
pdf_content_base64 = encode_file_to_base64("./tests/test_files/test_document.pdf") | ||
request_payload = { | ||
"document_content": pdf_content_base64, | ||
"chunk_size": 200, | ||
"chunk_overlap": 20, | ||
} | ||
response = client.post("/splitter/pdf", json=request_payload, headers={"api-key": MOCK_API_KEY}) | ||
assert response.status_code == 200 | ||
assert "chunks" in response.json() | ||
|
||
|
||
# Define test cases for validate_request() | ||
validate_request_test_cases = [ | ||
# Test case 1: valid request | ||
( | ||
SplitterRequest( | ||
document_content="dGVzdA==", chunk_size=100, chunk_overlap=10 # base64 for "test" | ||
), | ||
MOCK_API_KEY, | ||
None, | ||
), | ||
# Test case: invalid API key | ||
( | ||
SplitterRequest(document_content="dGVzdA==", chunk_size=100, chunk_overlap=10), | ||
"invalid_api_key", | ||
HTTPException(status_code=401, detail="Invalid API key"), | ||
), | ||
# Test case 2: missing document content | ||
( | ||
SplitterRequest(document_content="", chunk_size=100, chunk_overlap=10), | ||
MOCK_API_KEY, | ||
HTTPException(status_code=400, detail="No document content provided"), | ||
), | ||
# Test case 4: invalid chunk size | ||
( | ||
SplitterRequest(document_content="dGVzdA==", chunk_size=0, chunk_overlap=10), | ||
MOCK_API_KEY, | ||
HTTPException(status_code=400, detail="No chunk size provided"), | ||
), | ||
# Test case 5: invalid chunk overlap | ||
( | ||
SplitterRequest(document_content="dGVzdA==", chunk_size=100, chunk_overlap=-1), | ||
MOCK_API_KEY, | ||
HTTPException(status_code=400, detail="Chunk overlap must be greater than or equal to 0"), | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("api_request, api_key, expected_exception", validate_request_test_cases) | ||
def test_validate_request(api_request, api_key, expected_exception): | ||
"""Test the validate_request function with various scenarios.""" | ||
if expected_exception: | ||
with pytest.raises(HTTPException) as exc_info: | ||
validate_request(api_request, api_key) | ||
assert exc_info.value.status_code == expected_exception.status_code | ||
assert exc_info.value.detail == expected_exception.detail | ||
else: | ||
try: | ||
validate_request(api_request, api_key) | ||
except HTTPException: | ||
pytest.fail("validate_request() raised HTTPException unexpectedly!") |
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from app.app import app | ||
from fastapi.testclient import TestClient | ||
import pytest | ||
|
||
# Initialize the test client | ||
client = TestClient(app) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_list_functions(): | ||
"""Test listing available functions.""" | ||
# Test splitter results | ||
response = client.get("/", headers={"api-key": "test_api_key"}) | ||
assert response.status_code == 200 | ||
response_data = response.json() | ||
|
||
expected_response_start = [ | ||
{ | ||
"name": "split_ppt", | ||
"path": "/splitter/ppt", | ||
"inputs": [ | ||
{"name": "document_content", "type": "string(binary)"}, | ||
{"name": "chunk_size", "type": "integer"}, | ||
{"name": "chunk_overlap", "type": "integer"}, | ||
], | ||
"outputs": [{"name": "chunks", "type": "array<string>"}], | ||
"definitions": {}, | ||
}, | ||
{ | ||
"name": "split_py", | ||
"path": "/splitter/py", | ||
"inputs": [ | ||
{"name": "document_content", "type": "string(binary)"}, | ||
{"name": "chunk_size", "type": "integer"}, | ||
{"name": "chunk_overlap", "type": "integer"}, | ||
], | ||
"outputs": [{"name": "chunks", "type": "array<string>"}], | ||
"definitions": {}, | ||
}, | ||
{ | ||
"name": "split_pdf", | ||
"path": "/splitter/pdf", | ||
"inputs": [ | ||
{"name": "document_content", "type": "string(binary)"}, | ||
{"name": "chunk_size", "type": "integer"}, | ||
{"name": "chunk_overlap", "type": "integer"}, | ||
], | ||
"outputs": [{"name": "chunks", "type": "array<string>"}], | ||
"definitions": {}, | ||
}, | ||
] | ||
|
||
assert response_data[:3] == expected_response_start | ||
|
||
# Test invalid API key | ||
response = client.get("/", headers={"api-key": "invalid_api_key"}) | ||
assert response.status_code == 401 | ||
assert response.json() == {"detail": "Invalid API key"} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.