Skip to content

Commit

Permalink
Improve test, check the uniqueness of ids in test data
Browse files Browse the repository at this point in the history
  • Loading branch information
m-danya committed Jan 30, 2025
1 parent f894136 commit 685fea9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
26 changes: 15 additions & 11 deletions planty/application/tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async def test_get_archived_tasks(
"parent_id, status_code, error_detail",
[
("0d966845-254b-4b5c-b8a7-8d34dcd3d527", 201, None),
("45561eb6-3570-44de-af8a-54212e2981e6", 201, None),
(
"090eda97-dd2d-45bb-baa0-7814313e5a38",
422,
Expand All @@ -132,18 +133,21 @@ async def test_create_section(
ac: AsyncClient,
additional_test_data: dict[str, Any],
) -> None:
section_data = {
"title": str(additional_test_data["sections"][0]["title"]),
"parent_id": parent_id,
}
response = await ac.post("/api/section", json=section_data)
assert response.status_code == status_code
if not response.is_success:
assert response.json()["detail"] == error_detail
return
# Do it twice to check that the `has_subsections` flag is updated correctly
# (there is no other way to check it using endpoints)
for _ in range(2):
section_data = {
"title": str(additional_test_data["sections"][0]["title"]),
"parent_id": parent_id,
}
response = await ac.post("/api/section", json=section_data)
assert response.status_code == status_code
if not response.is_success:
assert response.json()["detail"] == error_detail
return

data = response.json()
assert "id" in data
data = response.json()
assert "id" in data


@pytest.mark.parametrize(
Expand Down
5 changes: 5 additions & 0 deletions planty/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def additional_test_data() -> dict[str, Any]:
def _load_json_with_data(filename: str) -> dict[str, Any]:
with open(Path(__file__).parent / "resources" / filename) as f:
test_data: dict[str, Any] = json.load(f)
ids = set()
for table_key in test_data:
last_idx: int = 0
for row in test_data[table_key]:
Expand All @@ -58,6 +59,10 @@ def _load_json_with_data(filename: str) -> dict[str, Any]:
"(NOTE: this is just a heuristic)"
)
last_idx = idx
if column == "id":
if row[column] in ids:
raise ValueError(f"Duplicate id {row[column]} in {table_key}")
ids.add(row[column])

# TODO: make this dict immutable to prevent accidental modification
return test_data
Expand Down
2 changes: 1 addition & 1 deletion planty/resources/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"index": 2
},
{
"id": "f05b7527-8173-4d14-a1bd-49aaba743247",
"id": "45561eb6-3570-44de-af8a-54212e2981e6",
"title": "🎸 Music",
"user_id": "38df4136-36b2-4171-8459-27f411af8323",
"parent_id": "36ea0a4f-0334-464d-8066-aa359ecfdcba",
Expand Down

0 comments on commit 685fea9

Please sign in to comment.