Skip to content

Commit

Permalink
test(backend): update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
frgfm committed Jan 16, 2025
1 parent 7eca51a commit c9eee03
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 100 deletions.
15 changes: 10 additions & 5 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
"is_wildfire": True,
"bboxes": "[(.1,.1,.7,.8,.9)]",
"created_at": datetime.strptime("2023-11-07T15:08:19.226673", dt_format),
"updated_at": datetime.strptime("2023-11-07T15:08:19.226673", dt_format),
},
{
"id": 2,
Expand All @@ -110,7 +109,6 @@
"is_wildfire": False,
"bboxes": "[(.1,.1,.7,.8,.9)]",
"created_at": datetime.strptime("2023-11-07T15:18:19.226673", dt_format),
"updated_at": datetime.strptime("2023-11-07T15:18:19.226673", dt_format),
},
{
"id": 3,
Expand All @@ -120,17 +118,15 @@
"is_wildfire": True,
"bboxes": "[(.1,.1,.7,.8,.9)]",
"created_at": datetime.strptime("2023-11-07T15:28:19.226673", dt_format),
"updated_at": datetime.strptime("2023-11-07T15:28:19.226673", dt_format),
},
{
"id": 4,
"camera_id": 2,
"azimuth": 43.7,
"azimuth": 74.8,
"bucket_key": "my_file",
"is_wildfire": None,
"bboxes": "[(.1,.1,.7,.8,.9)]",
"created_at": datetime.strptime("2023-11-07T16:08:19.226673", dt_format),
"updated_at": datetime.strptime("2023-11-07T16:08:19.226673", dt_format),
},
]

Expand All @@ -139,9 +135,18 @@
"id": 1,
"camera_id": 1,
"azimuth": 43.7,
"is_wildfire": True,
"started_at": datetime.strptime("2023-11-07T15:08:19.226673", dt_format),
"last_seen_at": datetime.strptime("2023-11-07T15:28:19.226673", dt_format),
},
{
"id": 2,
"camera_id": 2,
"azimuth": 74.8,
"is_wildfire": None,
"started_at": datetime.strptime("2023-11-07T16:08:19.226673", dt_format),
"last_seen_at": datetime.strptime("2023-11-07T16:08:19.226673", dt_format),
},
]

WEBHOOK_TABLE = [
Expand Down
86 changes: 0 additions & 86 deletions src/tests/endpoints/test_detections.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,92 +135,6 @@ async def test_fetch_detections(
assert response.json() == expected_result


@pytest.mark.parametrize(
("user_idx", "from_date", "status_code", "status_detail", "expected_result"),
[
(None, "2018-06-06T00:00:00", 401, "Not authenticated", None),
(0, "", 422, None, None),
(0, "old-date", 422, None, None),
(0, "2018-19-20T00:00:00", 422, None, None), # impossible date
(0, "2018-06-06", 422, None, None), # datetime != date
(0, "2018-06-06T00:00:00", 200, None, [pytest.detection_table[3]]),
(1, "2018-06-06T00:00:00", 200, None, []),
(2, "2018-06-06T00:00:00", 200, None, [pytest.detection_table[3]]),
],
)
@pytest.mark.asyncio
async def test_fetch_unlabeled_detections(
async_client: AsyncClient,
detection_session: AsyncSession,
user_idx: Union[int, None],
from_date: str,
status_code: int,
status_detail: Union[str, None],
expected_result: Union[List[Dict[str, Any]], None],
):
auth = None
if isinstance(user_idx, int):
auth = pytest.get_token(
pytest.user_table[user_idx]["id"],
pytest.user_table[user_idx]["role"].split(),
pytest.user_table[user_idx]["organization_id"],
)

response = await async_client.get(f"/detections/unlabeled/fromdate?from_date={from_date}", headers=auth)

assert response.status_code == status_code, print(response.__dict__)
if isinstance(status_detail, str):
assert response.json()["detail"] == status_detail
if response.status_code // 100 == 2:
assert [{k: v for k, v in det.items() if k != "url"} for det in response.json()] == expected_result
assert all(det["url"].startswith("http://") for det in response.json())


@pytest.mark.parametrize(
("user_idx", "detection_id", "payload", "status_code", "status_detail", "expected_idx"),
[
(None, 1, {"is_wildfire": True}, 401, "Not authenticated", None),
(0, 0, {"is_wildfire": True}, 422, None, None),
(0, 1, {"label": True}, 422, None, None),
(0, 1, {"is_wildfire": "hello"}, 422, None, None),
# (0, 1, {"is_wildfire": "True"}, 422, None, None), # odd, this works
(0, 1, {"is_wildfire": True}, 200, None, 0),
(0, 2, {"is_wildfire": True}, 200, None, 1),
(1, 1, {"is_wildfire": True}, 200, None, 0),
(1, 2, {"is_wildfire": True}, 200, None, 1),
(2, 1, {"is_wildfire": True}, 403, None, 0),
],
)
@pytest.mark.asyncio
async def test_label_detection(
async_client: AsyncClient,
detection_session: AsyncSession,
user_idx: Union[int, None],
detection_id: int,
payload: Dict[str, Any],
status_code: int,
status_detail: Union[str, None],
expected_idx: Union[int, None],
):
auth = None
if isinstance(user_idx, int):
auth = pytest.get_token(
pytest.user_table[user_idx]["id"],
pytest.user_table[user_idx]["role"].split(),
pytest.user_table[user_idx]["organization_id"],
)

response = await async_client.patch(f"/detections/{detection_id}/label", json=payload, headers=auth)
assert response.status_code == status_code, print(response.__dict__)
if isinstance(status_detail, str):
assert response.json()["detail"] == status_detail
if response.status_code // 100 == 2:
assert response.json() == {
**{k: v for k, v in pytest.detection_table[expected_idx].items() if k != "is_wildfire"},
**payload,
}


@pytest.mark.parametrize(
("user_idx", "detection_id", "status_code", "status_detail"),
[
Expand Down
149 changes: 140 additions & 9 deletions src/tests/endpoints/test_sequences.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
from typing import Union
from typing import Any, Dict, List, Union

import pytest
from httpx import AsyncClient
from sqlmodel.ext.asyncio.session import AsyncSession


@pytest.mark.parametrize(
("user_idx", "status_code", "status_detail"),
("user_idx", "sequence_id", "status_code", "status_detail", "expected_result"),
[
(None, 401, "Not authenticated"),
(0, 200, None),
(1, 403, "Incompatible token scope."),
(None, 1, 401, "Not authenticated", None),
(0, 1, 200, None, pytest.detection_table[:3]),
(0, 2, 404, "Table Sequence has no corresponding entry.", None),
(1, 1, 200, None, pytest.detection_table[:3]),
(1, 2, 403, "Incompatible token scope.", None),
(2, 1, 403, "Incompatible token scope.", None),
(2, 2, 200, None, pytest.detection_table[3:4]),
],
)
@pytest.mark.asyncio
async def test_fetch_sequences(
async def test_fetch_sequence_detections(
async_client: AsyncClient,
sequence_session: AsyncSession,
user_idx: Union[int, None],
sequence_id: int,
status_code: int,
status_detail: Union[str, None],
expected_result: Union[List[Dict[str, Any]], None],
):
auth = None
if isinstance(user_idx, int):
Expand All @@ -29,12 +35,12 @@ async def test_fetch_sequences(
pytest.user_table[user_idx]["organization_id"],
)

response = await async_client.get("/sequences/", headers=auth)
response = await async_client.get(f"/sequences/{sequence_id}/detections", headers=auth)
assert response.status_code == status_code, print(response.__dict__)
if isinstance(status_detail, str):
assert response.json()["detail"] == status_detail
if response.status_code // 100 == 2:
assert response.json() == pytest.sequence_table
if response.status_code // 100 == 2 and expected_result is not None:
assert {k: v for k, v in response.json().items() if k != "url"} == expected_result


@pytest.mark.parametrize(
Expand Down Expand Up @@ -69,3 +75,128 @@ async def test_delete_sequence(
assert response.json()["detail"] == status_detail
if response.status_code // 100 == 2:
assert response.json() is None


@pytest.mark.parametrize(
("user_idx", "sequence_id", "payload", "status_code", "status_detail", "expected_idx"),
[
(None, 1, {"is_wildfire": True}, 401, "Not authenticated", None),
(0, 0, {"is_wildfire": True}, 422, None, None),
(0, 99, {"is_wildfire": True}, 404, None, None),
(0, 1, {"label": True}, 422, None, None),
(0, 1, {"is_wildfire": "hello"}, 422, None, None),
# (0, 1, {"is_wildfire": "True"}, 422, None, None), # odd, this works
(0, 1, {"is_wildfire": True}, 200, None, 0),
(0, 2, {"is_wildfire": True}, 200, None, 1),
(1, 1, {"is_wildfire": True}, 200, None, 0),
(1, 2, {"is_wildfire": True}, 403, None, None),
(2, 1, {"is_wildfire": True}, 403, None, None),
(2, 2, {"is_wildfire": True}, 200, None, 1),
],
)
@pytest.mark.asyncio
async def test_label_sequence(
async_client: AsyncClient,
sequence_session: AsyncSession,
user_idx: Union[int, None],
sequence_id: int,
payload: Dict[str, Any],
status_code: int,
status_detail: Union[str, None],
expected_idx: Union[int, None],
):
auth = None
if isinstance(user_idx, int):
auth = pytest.get_token(
pytest.user_table[user_idx]["id"],
pytest.user_table[user_idx]["role"].split(),
pytest.user_table[user_idx]["organization_id"],
)

response = await async_client.patch(f"/sequences/{sequence_id}/label", json=payload, headers=auth)
assert response.status_code == status_code, print(response.__dict__)
if isinstance(status_detail, str):
assert response.json()["detail"] == status_detail
if response.status_code // 100 == 2:
assert response.json() == {
**{k: v for k, v in pytest.detection_table[expected_idx].items() if k != "is_wildfire"},
**payload,
}


@pytest.mark.parametrize(
("user_idx", "from_date", "status_code", "status_detail", "expected_result"),
[
(None, "2018-06-06", 401, "Not authenticated", None),
(0, "", 422, None, None),
(0, "old-date", 422, None, None),
(0, "2018-19-20", 422, None, None), # impossible date
(0, "2018-06-06T00:00:00", 422, None, None), # datetime != date
(0, "2018-06-06", 200, None, []),
(0, "2023-11-07", 200, None, pytest.sequence_table[:1]),
(1, "2023-11-07", 200, None, pytest.sequence_table[:1]),
(2, "2023-11-07", 200, None, pytest.sequence_table[1:2]),
],
)
@pytest.mark.asyncio
async def test_fetch_sequences_from_date(
async_client: AsyncClient,
sequence_session: AsyncSession,
user_idx: Union[int, None],
from_date: str,
status_code: int,
status_detail: Union[str, None],
expected_result: Union[List[Dict[str, Any]], None],
):
auth = None
if isinstance(user_idx, int):
auth = pytest.get_token(
pytest.user_table[user_idx]["id"],
pytest.user_table[user_idx]["role"].split(),
pytest.user_table[user_idx]["organization_id"],
)

response = await async_client.get(f"/sequences/all/fromdate?from_date={from_date}", headers=auth)

assert response.status_code == status_code, print(response.__dict__)
if isinstance(status_detail, str):
assert response.json()["detail"] == status_detail
if response.status_code // 100 == 2:
assert [{k: v for k, v in det.items() if k != "url"} for det in response.json()] == expected_result
assert all(det["url"].startswith("http://") for det in response.json())


@pytest.mark.parametrize(
("user_idx", "status_code", "status_detail", "expected_result"),
[
(None, 401, "Not authenticated", None),
(0, 200, None, []),
(1, 200, None, []),
(2, 200, None, []),
],
)
@pytest.mark.asyncio
async def test_latest_sequences(
async_client: AsyncClient,
sequence_session: AsyncSession,
user_idx: Union[int, None],
status_code: int,
status_detail: Union[str, None],
expected_result: Union[List[Dict[str, Any]], None],
):
auth = None
if isinstance(user_idx, int):
auth = pytest.get_token(
pytest.user_table[user_idx]["id"],
pytest.user_table[user_idx]["role"].split(),
pytest.user_table[user_idx]["organization_id"],
)

response = await async_client.get("/sequences/unlabeled/latest", headers=auth)

assert response.status_code == status_code, print(response.__dict__)
if isinstance(status_detail, str):
assert response.json()["detail"] == status_detail
if response.status_code // 100 == 2:
assert [{k: v for k, v in det.items() if k != "url"} for det in response.json()] == expected_result
assert all(det["url"].startswith("http://") for det in response.json())

0 comments on commit c9eee03

Please sign in to comment.