-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): add integration tests for all api usage scenarios
- Loading branch information
Showing
8 changed files
with
259 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,9 @@ | ||
Course,Classroom,Professor,Semester | ||
Math,512,Dr. Smith,Fall | ||
Physics,406,Dr. Green,Fall | ||
English,208,Prof. Turner,Fall | ||
History,209,Prof. Davis,Fall | ||
Math,512,Dr. Smith,Spring | ||
Physics,503,Dr. Gray,Spring | ||
English,116,Prof. Turner,Spring | ||
Biology,209,Prof. Light,Spring |
This file contains 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,40 @@ | ||
from uuid import UUID | ||
|
||
from fastapi.testclient import TestClient | ||
|
||
from internal.domain.task.value_objects import OneOfTaskConfig | ||
|
||
|
||
def upload_csv_dataset( | ||
client: TestClient, | ||
file_name: str, | ||
mime_type: str, | ||
separator: str, | ||
header: list[int], | ||
) -> dict: | ||
file_path = f"tests/datasets/{file_name}" | ||
|
||
with open(file_path, "rb") as file: | ||
form_data = { | ||
"separator": separator, | ||
"header": header, | ||
"file": (file_name, file, mime_type), | ||
} | ||
|
||
response = client.post( | ||
"api/file/csv", | ||
files={"file": form_data["file"]}, | ||
data={ | ||
"separator": form_data["separator"], | ||
"header": form_data["header"], | ||
}, | ||
) | ||
|
||
return response | ||
|
||
|
||
def set_task(client: TestClient, dataset_id: UUID, config: OneOfTaskConfig): | ||
response = client.post( | ||
"api/task/set", params={"dataset_id": dataset_id}, json=config.model_dump() | ||
) | ||
return response |
This file contains 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,8 @@ | ||
from fastapi.testclient import TestClient | ||
|
||
|
||
def test_retrieve_task(client: TestClient): | ||
response = client.get("api/common/ping") | ||
|
||
assert response.status_code == 200 | ||
assert response.json() == "Pong!" |
This file contains 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,40 @@ | ||
from fastapi.testclient import TestClient | ||
from uuid import UUID, uuid4 | ||
|
||
from internal.infrastructure.data_storage.relational.model.file import DatasetORM | ||
from tests.integration.common_requests import upload_csv_dataset | ||
|
||
|
||
def test_retrieve_dataset(client: TestClient, context): | ||
file_name = "university_fd.csv" | ||
file_path = f"tests/datasets/{file_name}" | ||
mime_type = "text/csv" | ||
separator = "," | ||
header = [0] | ||
|
||
response = upload_csv_dataset(client, file_name, mime_type, separator, header) | ||
assert response.status_code == 200 | ||
|
||
dataset_id = UUID(response.json()) | ||
|
||
response = client.post(f"api/file/dataset/{dataset_id}") | ||
|
||
assert response.status_code == 200 | ||
|
||
response_data = response.json() | ||
assert response_data["id"] == str(dataset_id) | ||
|
||
dataset = context.postgres_context.get(DatasetORM, dataset_id) | ||
assert dataset is not None | ||
assert response_data["file_id"] == str(dataset.file_id) | ||
assert response_data["separator"] == dataset.separator | ||
assert response_data["header"] == dataset.header | ||
|
||
|
||
def test_retrieve_non_existent_dataset(client: TestClient): | ||
dataset_id = uuid4() # non existen dataset | ||
|
||
response = client.post(f"api/file/dataset/{dataset_id}") | ||
|
||
assert response.status_code == 404 | ||
assert response.json()["detail"] == "Dataset not found" |
This file contains 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,57 @@ | ||
import time | ||
from fastapi.testclient import TestClient | ||
from uuid import UUID, uuid4 | ||
|
||
from internal.domain.task.value_objects import FdTaskConfig | ||
from internal.domain.task.value_objects.fd.algo_config import AidConfig | ||
from internal.infrastructure.data_storage.relational.model.task import TaskORM | ||
from tests.integration.common_requests import upload_csv_dataset, set_task | ||
|
||
|
||
def test_retrieve_task(client: TestClient, context): | ||
dataset_response = upload_csv_dataset( | ||
client, "university_fd.csv", "text/csv", ".", [0] | ||
) | ||
assert dataset_response.status_code == 200 | ||
dataset_id = UUID(dataset_response.json()) | ||
|
||
algo_config = AidConfig(algo_name="aid", is_null_equal_null=True) | ||
task_config = FdTaskConfig(primitive_name="fd", config=algo_config) | ||
|
||
task_response = set_task(client, dataset_id, task_config) | ||
assert task_response.status_code == 200 | ||
task_id = UUID(task_response.json()) | ||
|
||
time.sleep(1) | ||
|
||
response = client.get(f"api/task/{task_id}") | ||
assert response.status_code == 200 | ||
|
||
response_data = response.json() | ||
assert response_data["dataset_id"] == str(dataset_id) | ||
|
||
task = context.postgres_context.get(TaskORM, task_id) | ||
assert task is not None | ||
assert response_data["dataset_id"] == str(task.dataset_id) | ||
assert response_data["status"] == task.status | ||
assert response_data["config"] == task.config | ||
assert response_data["result"] == task.result | ||
assert response_data["raised_exception_name"] == task.raised_exception_name | ||
assert response_data["failure_reason"] == task.failure_reason | ||
assert response_data["traceback"] == task.traceback | ||
|
||
if response_data["status"] == "completed": | ||
assert response_data["result"] is not None | ||
if response_data["status"] == "failure": | ||
assert response_data["raised_exception_name"] is not None | ||
assert response_data["failure_reason"] is not None | ||
assert response_data["traceback"] is not None | ||
|
||
|
||
def test_retrieve_non_existent_task(client: TestClient): | ||
task_id = uuid4() # non existen task | ||
|
||
response = client.get(f"api/task/{task_id}") | ||
|
||
assert response.status_code == 404 | ||
assert response.json()["detail"] == "Task not found" |
This file contains 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,41 @@ | ||
from fastapi.testclient import TestClient | ||
from uuid import UUID, uuid4 | ||
|
||
from internal.domain.task.value_objects import FdTaskConfig | ||
from internal.domain.task.value_objects.fd.algo_config import AidConfig | ||
from internal.infrastructure.data_storage.relational.model.task import TaskORM | ||
from tests.integration.common_requests import upload_csv_dataset, set_task | ||
|
||
|
||
def test_set_profiling_task(client: TestClient, context): | ||
dataset_response = upload_csv_dataset( | ||
client, "university_fd.csv", "text/csv", ".", [0] | ||
) | ||
assert dataset_response.status_code == 200 | ||
dataset_id = UUID(dataset_response.json()) | ||
|
||
algo_config = AidConfig(algo_name="aid", is_null_equal_null=True) | ||
task_config = FdTaskConfig(primitive_name="fd", config=algo_config) | ||
|
||
response = set_task(client, dataset_id, task_config) | ||
|
||
assert response.status_code == 200 | ||
|
||
task_id = UUID(response.json()) | ||
|
||
task_data = context.postgres_context.get(TaskORM, task_id) | ||
|
||
assert task_data is not None | ||
assert task_data.config == task_config.model_dump() | ||
assert task_data.dataset_id == dataset_id | ||
|
||
|
||
def test_set_profiling_task_with_non_existent_dataset(client: TestClient): | ||
dataset_id = uuid4() | ||
algo_config = AidConfig(algo_name="aid", is_null_equal_null=True) | ||
task_config = FdTaskConfig(primitive_name="fd", config=algo_config) | ||
|
||
response = set_task(client, dataset_id, task_config) | ||
|
||
assert response.status_code == 404 | ||
assert response.json()["detail"] == "Dataset not found" |
This file contains 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 @@ | ||
import os | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
|
||
from uuid import UUID | ||
|
||
from internal.infrastructure.data_storage.relational.model.file import DatasetORM | ||
from tests.integration.common_requests import upload_csv_dataset | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_upload_csv_dataset(client: TestClient, context, tmp_upload_dir): | ||
file_name = "university_fd.csv" | ||
file_path = f"tests/datasets/{file_name}" | ||
mime_type = "text/csv" | ||
separator = "," | ||
header = [0] | ||
|
||
response = upload_csv_dataset(client, file_name, mime_type, separator, header) | ||
|
||
assert response.status_code == 200 | ||
|
||
dataset_id = UUID(response.json()) | ||
|
||
data = context.postgres_context.get(DatasetORM, dataset_id) | ||
assert data is not None | ||
assert data.id == dataset_id | ||
assert data.separator == separator | ||
assert data.header == header | ||
assert data.related_tasks == [] | ||
|
||
file = data.file_metadata | ||
assert file.original_file_name == "university_fd.csv" | ||
assert file.mime_type == mime_type | ||
|
||
saved_file_path = os.path.join(tmp_upload_dir, str(file.file_name)) | ||
assert os.path.exists(saved_file_path) | ||
|
||
with open(saved_file_path, "rb") as saved_file, open( | ||
file_path, "rb" | ||
) as original_file: | ||
saved_file_content = saved_file.read() | ||
original_file_content = original_file.read() | ||
assert saved_file_content == original_file_content | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_upload_csv_dataset_with_incorrect_mime_type(client: TestClient): | ||
file_name = "university.txt" | ||
file_path = f"tests/datasets/{file_name}" | ||
mime_type = "text/plain" | ||
separator = "," | ||
header = [0] | ||
|
||
response = upload_csv_dataset(client, file_name, mime_type, separator, header) | ||
|
||
assert response.status_code == 400 | ||
assert response.json()["detail"] == "File is not CSV" |