test_project_import_priority.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """A new project gets a priority and status the project editor accepts.
  2. A file without a priority used to get the number 0: refused by PostgreSQL (a
  3. text column) and stored as a priority the editor itself refuses on SQLite.
  4. Creating a project stored any priority, and importing one any status (an
  5. over-long value failed on PostgreSQL, any other one the editor refuses).
  6. """
  7. from __future__ import annotations
  8. import io
  9. import json
  10. import pytest
  11. from httpx import AsyncClient
  12. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  13. async def _import_file(client: AsyncClient, data: dict):
  14. files = {"file": ("project.json", io.BytesIO(json.dumps(data).encode()), "application/json")}
  15. return await client.post("/api/v1/projects/import/file", files=files)
  16. @pytest.mark.parametrize(
  17. ("given", "expected"),
  18. [(None, "normal"), ("high", "high"), ("urgent", "urgent"), (0, "normal"), ("whenever", "normal")],
  19. )
  20. async def test_a_file_import_gets_a_valid_priority(async_client: AsyncClient, given, expected):
  21. data = {"name": "Imported"} if given is None else {"name": "Imported", "priority": given}
  22. response = await _import_file(async_client, data)
  23. assert response.status_code == 200, response.text
  24. assert response.json()["priority"] == expected
  25. @pytest.mark.parametrize(("given", "expected"), [("low", "low"), ("whenever", "normal")])
  26. async def test_a_json_import_gets_a_valid_priority(async_client: AsyncClient, given, expected):
  27. response = await async_client.post("/api/v1/projects/import", json={"name": "Imported", "priority": given})
  28. assert response.status_code == 200, response.text
  29. assert response.json()["priority"] == expected
  30. @pytest.mark.parametrize(
  31. ("given", "expected"),
  32. [(None, "active"), ("completed", "completed"), ("archived", "archived"), ("done", "active"), (0, "active")],
  33. )
  34. async def test_a_file_import_gets_a_valid_status(async_client: AsyncClient, given, expected):
  35. response = await _import_file(async_client, {"name": "Imported", "status": given})
  36. assert response.status_code == 200, response.text
  37. assert response.json()["status"] == expected
  38. async def test_a_file_import_without_a_status_is_active(async_client: AsyncClient):
  39. response = await _import_file(async_client, {"name": "Imported"})
  40. assert response.status_code == 200, response.text
  41. assert response.json()["status"] == "active"
  42. @pytest.mark.parametrize(("given", "expected"), [("archived", "archived"), ("x" * 40, "active")])
  43. async def test_a_json_import_gets_a_valid_status(async_client: AsyncClient, given, expected):
  44. response = await async_client.post("/api/v1/projects/import", json={"name": "Imported", "status": given})
  45. assert response.status_code == 200, response.text
  46. assert response.json()["status"] == expected
  47. @pytest.mark.parametrize("priority", ["whenever", "x" * 40, ""])
  48. async def test_creating_a_project_refuses_an_unknown_priority(async_client: AsyncClient, priority):
  49. response = await async_client.post("/api/v1/projects/", json={"name": "New", "priority": priority})
  50. assert response.status_code == 400, response.text
  51. assert response.json()["detail"] == "Invalid priority"
  52. listed = await async_client.get("/api/v1/projects/")
  53. assert [p["name"] for p in listed.json()] == []
  54. @pytest.mark.parametrize("priority", ["low", "normal", "high", "urgent"])
  55. async def test_creating_a_project_keeps_a_known_priority(async_client: AsyncClient, priority):
  56. response = await async_client.post("/api/v1/projects/", json={"name": "New", "priority": priority})
  57. assert response.status_code == 200, response.text
  58. assert response.json()["priority"] == priority