import uuid import pytest def _auth_headers(tokens: dict) -> dict: return {"Authorization": f"Bearer {tokens['access_token']}"} def test_projects_list_smoke(client, admin_tokens): response = client.get("/api/v1/projects", headers=_auth_headers(admin_tokens)) assert response.status_code == 200 payload = response.json() assert "result" in payload assert "count" in payload assert isinstance(payload["result"], list) def _create_project(client, admin_tokens) -> tuple[int, str]: name = f"PT_{uuid.uuid4().hex[:10]}" response = client.post( "/api/v1/projects", json={"name": name, "year": 2026, "branch_id": 1}, headers=_auth_headers(admin_tokens), ) assert response.status_code == 200 payload = response.json() return payload["project_id"], name def _add_line(client, admin_tokens, project_id: int) -> int: for expense_item_id in range(1, 80): response = client.post( f"/api/v1/projects/{project_id}/report/2026/LIMIT/line", json={"expense_item_id": expense_item_id}, headers=_auth_headers(admin_tokens), ) if response.status_code != 200: continue rows = response.json() input_row = next( ( row for row in rows if row.get("row_type") == "INPUT" and row.get("data", {}).get("line_id") ), None, ) if input_row: return int(input_row["data"]["line_id"]) pytest.skip("Не удалось подобрать expense_item_id для add_form3_line в текущей БД") def test_projects_write_smoke(client, admin_tokens): project_id, name = _create_project(client, admin_tokens) upd_project_response = client.patch( f"/api/v1/project/{project_id}", json={"column": "name", "value": f"{name}_U"}, headers=_auth_headers(admin_tokens), ) assert upd_project_response.status_code == 200 line_id = _add_line(client, admin_tokens, project_id) upd_cell_response = client.patch( f"/api/v1/projects/{project_id}/report/2026/LIMIT/cell", json={"line_id": line_id, "column": "q1.m1", "value": 100}, headers=_auth_headers(admin_tokens), ) assert upd_cell_response.status_code == 200 assert isinstance(upd_cell_response.json(), list) upd_cells_response = client.patch( f"/api/v1/projects/{project_id}/report/2026/LIMIT/cells", json={ "changes": [ {"line_id": line_id, "column": "q1.m2", "value": 200}, {"line_id": line_id, "column": "q1.m3", "value": 300}, ] }, headers=_auth_headers(admin_tokens), ) assert upd_cells_response.status_code == 200 assert isinstance(upd_cells_response.json(), list) del_line_response = client.delete( f"/api/v1/projects/{project_id}/report/2026/LIMIT/line/{line_id}", headers=_auth_headers(admin_tokens), ) assert del_line_response.status_code == 200 def test_projects_write_invalid_report_type_error_shape(client, admin_tokens): project_id, _ = _create_project(client, admin_tokens) line_id = _add_line(client, admin_tokens, project_id) response = client.patch( f"/api/v1/projects/{project_id}/report/2026/BAD_TYPE/cell", json={"line_id": line_id, "column": "q1.m1", "value": 100}, headers=_auth_headers(admin_tokens), ) assert response.status_code == 400 payload = response.json() assert isinstance(payload, dict) assert "message" in payload assert isinstance(payload["message"], str) def test_projects_write_report_not_found_error_shape(client, admin_tokens): project_id, _ = _create_project(client, admin_tokens) line_id = _add_line(client, admin_tokens, project_id) response = client.patch( f"/api/v1/projects/{project_id}/report/2099/LIMIT/cell", json={"line_id": line_id, "column": "q1.m1", "value": 100}, headers=_auth_headers(admin_tokens), ) assert response.status_code == 400 payload = response.json() assert isinstance(payload, dict) assert payload.get("message") == "Отчёт не найден"