test_print_queue_api.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. """Integration tests for Print Queue API endpoints."""
  2. import pytest
  3. from httpx import AsyncClient
  4. class TestPrintQueueAPI:
  5. """Integration tests for /api/v1/queue endpoints."""
  6. @pytest.fixture
  7. async def printer_factory(self, db_session):
  8. """Factory to create test printers."""
  9. _counter = [0]
  10. async def _create_printer(**kwargs):
  11. from backend.app.models.printer import Printer
  12. _counter[0] += 1
  13. counter = _counter[0]
  14. defaults = {
  15. "name": f"Test Printer {counter}",
  16. "ip_address": f"192.168.1.{100 + counter}",
  17. "serial_number": f"TESTSERIAL{counter:04d}",
  18. "access_code": "12345678",
  19. "model": "X1C",
  20. }
  21. defaults.update(kwargs)
  22. printer = Printer(**defaults)
  23. db_session.add(printer)
  24. await db_session.commit()
  25. await db_session.refresh(printer)
  26. return printer
  27. return _create_printer
  28. @pytest.fixture
  29. async def archive_factory(self, db_session):
  30. """Factory to create test archives."""
  31. _counter = [0]
  32. async def _create_archive(**kwargs):
  33. from backend.app.models.archive import PrintArchive
  34. _counter[0] += 1
  35. counter = _counter[0]
  36. defaults = {
  37. "filename": f"test_print_{counter}.3mf",
  38. "print_name": f"Test Print {counter}",
  39. "file_path": f"/tmp/test_print_{counter}.3mf",
  40. "file_size": 1024,
  41. "content_hash": f"testhash{counter:08d}",
  42. "status": "completed",
  43. }
  44. defaults.update(kwargs)
  45. archive = PrintArchive(**defaults)
  46. db_session.add(archive)
  47. await db_session.commit()
  48. await db_session.refresh(archive)
  49. return archive
  50. return _create_archive
  51. @pytest.fixture
  52. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  53. """Factory to create test queue items."""
  54. _counter = [0]
  55. async def _create_queue_item(**kwargs):
  56. from backend.app.models.print_queue import PrintQueueItem
  57. _counter[0] += 1
  58. counter = _counter[0]
  59. # Create printer and archive if not provided
  60. if "printer_id" not in kwargs:
  61. printer = await printer_factory()
  62. kwargs["printer_id"] = printer.id
  63. if "archive_id" not in kwargs:
  64. archive = await archive_factory()
  65. kwargs["archive_id"] = archive.id
  66. defaults = {
  67. "status": "pending",
  68. "position": counter,
  69. }
  70. defaults.update(kwargs)
  71. item = PrintQueueItem(**defaults)
  72. db_session.add(item)
  73. await db_session.commit()
  74. await db_session.refresh(item)
  75. return item
  76. return _create_queue_item
  77. @pytest.mark.asyncio
  78. @pytest.mark.integration
  79. async def test_list_queue_empty(self, async_client: AsyncClient):
  80. """Verify empty list when no queue items exist."""
  81. response = await async_client.get("/api/v1/queue/")
  82. assert response.status_code == 200
  83. assert isinstance(response.json(), list)
  84. @pytest.mark.asyncio
  85. @pytest.mark.integration
  86. async def test_add_to_queue(self, async_client: AsyncClient, printer_factory, archive_factory, db_session):
  87. """Verify item can be added to queue."""
  88. printer = await printer_factory()
  89. archive = await archive_factory()
  90. data = {
  91. "printer_id": printer.id,
  92. "archive_id": archive.id,
  93. }
  94. response = await async_client.post("/api/v1/queue/", json=data)
  95. assert response.status_code == 200
  96. result = response.json()
  97. assert result["printer_id"] == printer.id
  98. assert result["archive_id"] == archive.id
  99. assert result["status"] == "pending"
  100. assert result["manual_start"] is False
  101. @pytest.mark.asyncio
  102. @pytest.mark.integration
  103. async def test_add_to_queue_with_manual_start(
  104. self, async_client: AsyncClient, printer_factory, archive_factory, db_session
  105. ):
  106. """Verify item can be added to queue with manual_start=True."""
  107. printer = await printer_factory()
  108. archive = await archive_factory()
  109. data = {
  110. "printer_id": printer.id,
  111. "archive_id": archive.id,
  112. "manual_start": True,
  113. }
  114. response = await async_client.post("/api/v1/queue/", json=data)
  115. assert response.status_code == 200
  116. result = response.json()
  117. assert result["printer_id"] == printer.id
  118. assert result["archive_id"] == archive.id
  119. assert result["status"] == "pending"
  120. assert result["manual_start"] is True
  121. @pytest.mark.asyncio
  122. @pytest.mark.integration
  123. async def test_get_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  124. """Verify single queue item can be retrieved."""
  125. item = await queue_item_factory()
  126. response = await async_client.get(f"/api/v1/queue/{item.id}")
  127. assert response.status_code == 200
  128. assert response.json()["id"] == item.id
  129. @pytest.mark.asyncio
  130. @pytest.mark.integration
  131. async def test_get_queue_item_not_found(self, async_client: AsyncClient):
  132. """Verify 404 for non-existent queue item."""
  133. response = await async_client.get("/api/v1/queue/9999")
  134. assert response.status_code == 404
  135. @pytest.mark.asyncio
  136. @pytest.mark.integration
  137. async def test_update_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  138. """Verify queue item can be updated."""
  139. item = await queue_item_factory()
  140. response = await async_client.patch(f"/api/v1/queue/{item.id}", json={"auto_off_after": True})
  141. assert response.status_code == 200
  142. result = response.json()
  143. assert result["auto_off_after"] is True
  144. @pytest.mark.asyncio
  145. @pytest.mark.integration
  146. async def test_update_queue_item_manual_start(self, async_client: AsyncClient, queue_item_factory, db_session):
  147. """Verify queue item manual_start can be updated."""
  148. item = await queue_item_factory(manual_start=False)
  149. response = await async_client.patch(f"/api/v1/queue/{item.id}", json={"manual_start": True})
  150. assert response.status_code == 200
  151. result = response.json()
  152. assert result["manual_start"] is True
  153. @pytest.mark.asyncio
  154. @pytest.mark.integration
  155. async def test_delete_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  156. """Verify queue item can be deleted."""
  157. item = await queue_item_factory()
  158. response = await async_client.delete(f"/api/v1/queue/{item.id}")
  159. assert response.status_code == 200
  160. assert response.json()["message"] == "Queue item deleted"
  161. @pytest.mark.asyncio
  162. @pytest.mark.integration
  163. async def test_delete_queue_item_not_found(self, async_client: AsyncClient):
  164. """Verify 404 for deleting non-existent queue item."""
  165. response = await async_client.delete("/api/v1/queue/9999")
  166. assert response.status_code == 404
  167. class TestQueueStartEndpoint:
  168. """Tests for the /queue/{item_id}/start endpoint."""
  169. @pytest.fixture
  170. async def printer_factory(self, db_session):
  171. """Factory to create test printers."""
  172. _counter = [0]
  173. async def _create_printer(**kwargs):
  174. from backend.app.models.printer import Printer
  175. _counter[0] += 1
  176. counter = _counter[0]
  177. defaults = {
  178. "name": f"Test Printer {counter}",
  179. "ip_address": f"192.168.1.{100 + counter}",
  180. "serial_number": f"TESTSERIAL{counter:04d}",
  181. "access_code": "12345678",
  182. "model": "X1C",
  183. }
  184. defaults.update(kwargs)
  185. printer = Printer(**defaults)
  186. db_session.add(printer)
  187. await db_session.commit()
  188. await db_session.refresh(printer)
  189. return printer
  190. return _create_printer
  191. @pytest.fixture
  192. async def archive_factory(self, db_session):
  193. """Factory to create test archives."""
  194. _counter = [0]
  195. async def _create_archive(**kwargs):
  196. from backend.app.models.archive import PrintArchive
  197. _counter[0] += 1
  198. counter = _counter[0]
  199. defaults = {
  200. "filename": f"test_print_{counter}.3mf",
  201. "print_name": f"Test Print {counter}",
  202. "file_path": f"/tmp/test_print_{counter}.3mf",
  203. "file_size": 1024,
  204. "content_hash": f"testhash{counter:08d}",
  205. "status": "completed",
  206. }
  207. defaults.update(kwargs)
  208. archive = PrintArchive(**defaults)
  209. db_session.add(archive)
  210. await db_session.commit()
  211. await db_session.refresh(archive)
  212. return archive
  213. return _create_archive
  214. @pytest.fixture
  215. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  216. """Factory to create test queue items."""
  217. _counter = [0]
  218. async def _create_queue_item(**kwargs):
  219. from backend.app.models.print_queue import PrintQueueItem
  220. _counter[0] += 1
  221. counter = _counter[0]
  222. if "printer_id" not in kwargs:
  223. printer = await printer_factory()
  224. kwargs["printer_id"] = printer.id
  225. if "archive_id" not in kwargs:
  226. archive = await archive_factory()
  227. kwargs["archive_id"] = archive.id
  228. defaults = {
  229. "status": "pending",
  230. "position": counter,
  231. }
  232. defaults.update(kwargs)
  233. item = PrintQueueItem(**defaults)
  234. db_session.add(item)
  235. await db_session.commit()
  236. await db_session.refresh(item)
  237. return item
  238. return _create_queue_item
  239. @pytest.mark.asyncio
  240. @pytest.mark.integration
  241. async def test_start_staged_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  242. """Verify starting a staged (manual_start=True) queue item clears the flag."""
  243. item = await queue_item_factory(manual_start=True)
  244. assert item.manual_start is True
  245. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  246. assert response.status_code == 200
  247. result = response.json()
  248. assert result["manual_start"] is False
  249. assert result["status"] == "pending"
  250. @pytest.mark.asyncio
  251. @pytest.mark.integration
  252. async def test_start_non_staged_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  253. """Verify starting a non-staged queue item still works (idempotent)."""
  254. item = await queue_item_factory(manual_start=False)
  255. assert item.manual_start is False
  256. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  257. assert response.status_code == 200
  258. result = response.json()
  259. assert result["manual_start"] is False
  260. @pytest.mark.asyncio
  261. @pytest.mark.integration
  262. async def test_start_queue_item_not_found(self, async_client: AsyncClient):
  263. """Verify 404 for non-existent queue item."""
  264. response = await async_client.post("/api/v1/queue/9999/start")
  265. assert response.status_code == 404
  266. @pytest.mark.asyncio
  267. @pytest.mark.integration
  268. async def test_start_non_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  269. """Verify 400 error when trying to start a non-pending queue item."""
  270. item = await queue_item_factory(status="printing", manual_start=True)
  271. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  272. assert response.status_code == 400
  273. assert "pending" in response.json()["detail"].lower()
  274. @pytest.mark.asyncio
  275. @pytest.mark.integration
  276. async def test_start_completed_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  277. """Verify 400 error when trying to start a completed queue item."""
  278. item = await queue_item_factory(status="completed", manual_start=True)
  279. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  280. assert response.status_code == 400
  281. class TestQueueCancelEndpoint:
  282. """Tests for the /queue/{item_id}/cancel endpoint."""
  283. @pytest.fixture
  284. async def printer_factory(self, db_session):
  285. """Factory to create test printers."""
  286. async def _create_printer(**kwargs):
  287. from backend.app.models.printer import Printer
  288. defaults = {
  289. "name": "Cancel Test Printer",
  290. "ip_address": "192.168.1.200",
  291. "serial_number": "TESTCANCEL001",
  292. "access_code": "12345678",
  293. "model": "X1C",
  294. }
  295. defaults.update(kwargs)
  296. printer = Printer(**defaults)
  297. db_session.add(printer)
  298. await db_session.commit()
  299. await db_session.refresh(printer)
  300. return printer
  301. return _create_printer
  302. @pytest.fixture
  303. async def archive_factory(self, db_session):
  304. """Factory to create test archives."""
  305. async def _create_archive(**kwargs):
  306. from backend.app.models.archive import PrintArchive
  307. defaults = {
  308. "filename": "cancel_test.3mf",
  309. "print_name": "Cancel Test Print",
  310. "file_path": "/tmp/cancel_test.3mf",
  311. "file_size": 1024,
  312. "content_hash": "cancelhash001",
  313. "status": "completed",
  314. }
  315. defaults.update(kwargs)
  316. archive = PrintArchive(**defaults)
  317. db_session.add(archive)
  318. await db_session.commit()
  319. await db_session.refresh(archive)
  320. return archive
  321. return _create_archive
  322. @pytest.fixture
  323. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  324. """Factory to create test queue items."""
  325. async def _create_queue_item(**kwargs):
  326. from backend.app.models.print_queue import PrintQueueItem
  327. if "printer_id" not in kwargs:
  328. printer = await printer_factory()
  329. kwargs["printer_id"] = printer.id
  330. if "archive_id" not in kwargs:
  331. archive = await archive_factory()
  332. kwargs["archive_id"] = archive.id
  333. defaults = {
  334. "status": "pending",
  335. "position": 1,
  336. }
  337. defaults.update(kwargs)
  338. item = PrintQueueItem(**defaults)
  339. db_session.add(item)
  340. await db_session.commit()
  341. await db_session.refresh(item)
  342. return item
  343. return _create_queue_item
  344. @pytest.mark.asyncio
  345. @pytest.mark.integration
  346. async def test_cancel_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  347. """Verify cancelling a pending queue item."""
  348. item = await queue_item_factory(status="pending")
  349. response = await async_client.post(f"/api/v1/queue/{item.id}/cancel")
  350. assert response.status_code == 200
  351. assert response.json()["message"] == "Queue item cancelled"
  352. @pytest.mark.asyncio
  353. @pytest.mark.integration
  354. async def test_cancel_non_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  355. """Verify 400 error when trying to cancel a non-pending queue item."""
  356. item = await queue_item_factory(status="printing")
  357. response = await async_client.post(f"/api/v1/queue/{item.id}/cancel")
  358. assert response.status_code == 400