test_zero_ids_on_create.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """An id of 0 on create means "not given", as on the matching edits.
  2. Queue create, batch create and project create tested these ids for
  3. truthiness: 0 skipped the existence check and was then stored as an id (a
  4. server error on PostgreSQL, a link to nothing on SQLite). Batch create did not
  5. check its archive or library file at all.
  6. """
  7. from __future__ import annotations
  8. import pytest
  9. from httpx import AsyncClient
  10. from sqlalchemy import select
  11. from backend.app.models.library import LibraryFile
  12. from backend.app.models.print_batch import PrintBatch
  13. from backend.app.models.print_queue import PrintQueueItem
  14. from backend.app.models.project import Project
  15. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  16. async def _library_file(db_session) -> int:
  17. row = LibraryFile(filename="part.gcode.3mf", file_path="library/part.gcode.3mf", file_size=5, file_type="3mf")
  18. db_session.add(row)
  19. await db_session.commit()
  20. return row.id
  21. async def test_queueing_a_library_file_with_archive_0(async_client: AsyncClient, db_session, printer_factory):
  22. printer_id = (await printer_factory()).id
  23. file_id = await _library_file(db_session)
  24. response = await async_client.post(
  25. "/api/v1/queue/", json={"archive_id": 0, "library_file_id": file_id, "printer_id": printer_id}
  26. )
  27. assert response.status_code == 200, response.text
  28. db_session.expire_all()
  29. stored = (await db_session.execute(select(PrintQueueItem.archive_id, PrintQueueItem.library_file_id))).one()
  30. assert tuple(stored) == (None, file_id)
  31. async def test_queueing_with_both_ids_0_is_refused(async_client: AsyncClient):
  32. response = await async_client.post("/api/v1/queue/", json={"archive_id": 0, "library_file_id": 0})
  33. assert response.status_code == 400, response.text
  34. assert response.json()["detail"] == "Either archive_id or library_file_id must be provided"
  35. async def test_a_batch_with_archive_0_has_no_archive(async_client: AsyncClient, db_session):
  36. file_id = await _library_file(db_session)
  37. response = await async_client.post(
  38. "/api/v1/queue/batches", json={"name": "B", "archive_id": 0, "library_file_id": file_id}
  39. )
  40. assert response.status_code == 200, response.text
  41. assert (response.json()["archive_id"], response.json()["library_file_id"]) == (None, file_id)
  42. @pytest.mark.parametrize(
  43. ("field", "detail"), [("archive_id", "Archive not found"), ("library_file_id", "Library file not found")]
  44. )
  45. async def test_a_batch_for_an_unknown_file_is_refused(async_client: AsyncClient, db_session, field, detail):
  46. response = await async_client.post("/api/v1/queue/batches", json={"name": "B", field: 9999})
  47. assert response.status_code == 404, response.text
  48. assert response.json()["detail"] == detail
  49. assert (await db_session.execute(select(PrintBatch.id))).first() is None
  50. async def test_a_batch_for_a_known_archive_keeps_it(async_client: AsyncClient, printer_factory, archive_factory):
  51. archive = await archive_factory((await printer_factory()).id)
  52. response = await async_client.post("/api/v1/queue/batches", json={"name": "B", "archive_id": archive.id})
  53. assert response.status_code == 200, response.text
  54. assert response.json()["archive_id"] == archive.id
  55. async def test_a_project_with_parent_0_has_no_parent(async_client: AsyncClient, db_session):
  56. response = await async_client.post("/api/v1/projects/", json={"name": "P", "parent_id": 0})
  57. assert response.status_code == 200, response.text
  58. assert response.json()["parent_id"] is None
  59. db_session.expire_all()
  60. assert (await db_session.execute(select(Project.parent_id))).scalar() is None