Skip to content

Commit

Permalink
feat(tests): add integration tests for all api usage scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
raf-nr committed Oct 2, 2024
1 parent e787967 commit 00d24e8
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 3 deletions.
8 changes: 6 additions & 2 deletions internal/rest/http/file/di.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
get_dataset_repo,
)
from internal.uow import UnitOfWork
from internal.usecase.file import SaveFile, SaveDataset, CheckContentType
from internal.usecase.file.retrieve_dataset import RetrieveDataset
from internal.usecase.file import (
SaveFile,
SaveDataset,
CheckContentType,
RetrieveDataset,
)
from internal.usecase.file.save_dataset import DatasetRepo as SaveDatasetRepo
from internal.usecase.file.retrieve_dataset import DatasetRepo as RetrieveDatasetRepo
from internal.usecase.file.save_file import FileRepo, FileMetadataRepo
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def set_task_use_case(unit_of_work, task_repo, dataset_repo, profiling_task_work
def profile_task_use_case(unit_of_work_without_pool, dataset_repo, file_repo):
return ProfileTask(
unit_of_work=unit_of_work_without_pool,
dataset_repo=task_repo,
dataset_repo=dataset_repo,
file_repo=file_repo,
)

Expand Down
9 changes: 9 additions & 0 deletions tests/datasets/university.txt
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
36 changes: 36 additions & 0 deletions tests/integration/common_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from fastapi.testclient import TestClient


def upload_csv_dataset(
client: TestClient,
file_name,
mime_type,
separator,
header,
):
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, config):
response = client.post(
"api/task/set", params={"dataset_id": dataset_id}, json=config.model_dump()
)
return response
8 changes: 8 additions & 0 deletions tests/integration/test_ping.py
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!"
40 changes: 40 additions & 0 deletions tests/integration/test_retrieve_dataset_info.py
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"
58 changes: 58 additions & 0 deletions tests/integration/test_retrieve_task_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import time
from fastapi.testclient import TestClient
from uuid import UUID, uuid4

from internal.domain.task.value_objects import FdTaskConfig, PrimitiveName
from internal.domain.task.value_objects.fd.algo_name import FdAlgoName
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=FdAlgoName.Aid, is_null_equal_null=True)
task_config = FdTaskConfig(primitive_name=PrimitiveName.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"
42 changes: 42 additions & 0 deletions tests/integration/test_set_profiling_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from fastapi.testclient import TestClient
from uuid import UUID, uuid4

from internal.domain.task.value_objects import FdTaskConfig, PrimitiveName
from internal.domain.task.value_objects.fd.algo_name import FdAlgoName
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=FdAlgoName.Aid, is_null_equal_null=True)
task_config = FdTaskConfig(primitive_name=PrimitiveName.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=FdAlgoName.Aid, is_null_equal_null=True)
task_config = FdTaskConfig(primitive_name=PrimitiveName.fd, config=algo_config)

response = set_task(client, dataset_id, task_config)

assert response.status_code == 404
assert response.json()["detail"] == "Dataset not found"
58 changes: 58 additions & 0 deletions tests/integration/test_upload_dataset.py
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"

0 comments on commit 00d24e8

Please sign in to comment.