test_print_queue_api.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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_add_to_queue_with_ams_mapping(
  124. self, async_client: AsyncClient, printer_factory, archive_factory, db_session
  125. ):
  126. """Verify item can be added to queue with ams_mapping."""
  127. printer = await printer_factory()
  128. archive = await archive_factory()
  129. data = {
  130. "printer_id": printer.id,
  131. "archive_id": archive.id,
  132. "ams_mapping": [5, -1, 2, -1], # Slot 1 -> tray 5, slot 3 -> tray 2
  133. }
  134. response = await async_client.post("/api/v1/queue/", json=data)
  135. assert response.status_code == 200
  136. result = response.json()
  137. assert result["printer_id"] == printer.id
  138. assert result["archive_id"] == archive.id
  139. assert result["ams_mapping"] == [5, -1, 2, -1]
  140. @pytest.mark.asyncio
  141. @pytest.mark.integration
  142. async def test_get_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  143. """Verify single queue item can be retrieved."""
  144. item = await queue_item_factory()
  145. response = await async_client.get(f"/api/v1/queue/{item.id}")
  146. assert response.status_code == 200
  147. assert response.json()["id"] == item.id
  148. @pytest.mark.asyncio
  149. @pytest.mark.integration
  150. async def test_get_queue_item_not_found(self, async_client: AsyncClient):
  151. """Verify 404 for non-existent queue item."""
  152. response = await async_client.get("/api/v1/queue/9999")
  153. assert response.status_code == 404
  154. @pytest.mark.asyncio
  155. @pytest.mark.integration
  156. async def test_update_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  157. """Verify queue item can be updated."""
  158. item = await queue_item_factory()
  159. response = await async_client.patch(f"/api/v1/queue/{item.id}", json={"auto_off_after": True})
  160. assert response.status_code == 200
  161. result = response.json()
  162. assert result["auto_off_after"] is True
  163. @pytest.mark.asyncio
  164. @pytest.mark.integration
  165. async def test_update_queue_item_manual_start(self, async_client: AsyncClient, queue_item_factory, db_session):
  166. """Verify queue item manual_start can be updated."""
  167. item = await queue_item_factory(manual_start=False)
  168. response = await async_client.patch(f"/api/v1/queue/{item.id}", json={"manual_start": True})
  169. assert response.status_code == 200
  170. result = response.json()
  171. assert result["manual_start"] is True
  172. @pytest.mark.asyncio
  173. @pytest.mark.integration
  174. async def test_delete_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  175. """Verify queue item can be deleted."""
  176. item = await queue_item_factory()
  177. response = await async_client.delete(f"/api/v1/queue/{item.id}")
  178. assert response.status_code == 200
  179. assert response.json()["message"] == "Queue item deleted"
  180. @pytest.mark.asyncio
  181. @pytest.mark.integration
  182. async def test_delete_queue_item_not_found(self, async_client: AsyncClient):
  183. """Verify 404 for deleting non-existent queue item."""
  184. response = await async_client.delete("/api/v1/queue/9999")
  185. assert response.status_code == 404
  186. class TestQueueStartEndpoint:
  187. """Tests for the /queue/{item_id}/start endpoint."""
  188. @pytest.fixture
  189. async def printer_factory(self, db_session):
  190. """Factory to create test printers."""
  191. _counter = [0]
  192. async def _create_printer(**kwargs):
  193. from backend.app.models.printer import Printer
  194. _counter[0] += 1
  195. counter = _counter[0]
  196. defaults = {
  197. "name": f"Test Printer {counter}",
  198. "ip_address": f"192.168.1.{100 + counter}",
  199. "serial_number": f"TESTSERIAL{counter:04d}",
  200. "access_code": "12345678",
  201. "model": "X1C",
  202. }
  203. defaults.update(kwargs)
  204. printer = Printer(**defaults)
  205. db_session.add(printer)
  206. await db_session.commit()
  207. await db_session.refresh(printer)
  208. return printer
  209. return _create_printer
  210. @pytest.fixture
  211. async def archive_factory(self, db_session):
  212. """Factory to create test archives."""
  213. _counter = [0]
  214. async def _create_archive(**kwargs):
  215. from backend.app.models.archive import PrintArchive
  216. _counter[0] += 1
  217. counter = _counter[0]
  218. defaults = {
  219. "filename": f"test_print_{counter}.3mf",
  220. "print_name": f"Test Print {counter}",
  221. "file_path": f"/tmp/test_print_{counter}.3mf",
  222. "file_size": 1024,
  223. "content_hash": f"testhash{counter:08d}",
  224. "status": "completed",
  225. }
  226. defaults.update(kwargs)
  227. archive = PrintArchive(**defaults)
  228. db_session.add(archive)
  229. await db_session.commit()
  230. await db_session.refresh(archive)
  231. return archive
  232. return _create_archive
  233. @pytest.fixture
  234. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  235. """Factory to create test queue items."""
  236. _counter = [0]
  237. async def _create_queue_item(**kwargs):
  238. from backend.app.models.print_queue import PrintQueueItem
  239. _counter[0] += 1
  240. counter = _counter[0]
  241. if "printer_id" not in kwargs:
  242. printer = await printer_factory()
  243. kwargs["printer_id"] = printer.id
  244. if "archive_id" not in kwargs:
  245. archive = await archive_factory()
  246. kwargs["archive_id"] = archive.id
  247. defaults = {
  248. "status": "pending",
  249. "position": counter,
  250. }
  251. defaults.update(kwargs)
  252. item = PrintQueueItem(**defaults)
  253. db_session.add(item)
  254. await db_session.commit()
  255. await db_session.refresh(item)
  256. return item
  257. return _create_queue_item
  258. @pytest.mark.asyncio
  259. @pytest.mark.integration
  260. async def test_start_staged_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  261. """Verify starting a staged (manual_start=True) queue item clears the flag."""
  262. item = await queue_item_factory(manual_start=True)
  263. assert item.manual_start is True
  264. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  265. assert response.status_code == 200
  266. result = response.json()
  267. assert result["manual_start"] is False
  268. assert result["status"] == "pending"
  269. @pytest.mark.asyncio
  270. @pytest.mark.integration
  271. async def test_start_non_staged_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  272. """Verify starting a non-staged queue item still works (idempotent)."""
  273. item = await queue_item_factory(manual_start=False)
  274. assert item.manual_start is False
  275. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  276. assert response.status_code == 200
  277. result = response.json()
  278. assert result["manual_start"] is False
  279. @pytest.mark.asyncio
  280. @pytest.mark.integration
  281. async def test_start_queue_item_not_found(self, async_client: AsyncClient):
  282. """Verify 404 for non-existent queue item."""
  283. response = await async_client.post("/api/v1/queue/9999/start")
  284. assert response.status_code == 404
  285. @pytest.mark.asyncio
  286. @pytest.mark.integration
  287. async def test_start_non_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  288. """Verify 400 error when trying to start a non-pending queue item."""
  289. item = await queue_item_factory(status="printing", manual_start=True)
  290. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  291. assert response.status_code == 400
  292. assert "pending" in response.json()["detail"].lower()
  293. @pytest.mark.asyncio
  294. @pytest.mark.integration
  295. async def test_start_completed_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  296. """Verify 400 error when trying to start a completed queue item."""
  297. item = await queue_item_factory(status="completed", manual_start=True)
  298. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  299. assert response.status_code == 400
  300. class TestQueueCancelEndpoint:
  301. """Tests for the /queue/{item_id}/cancel endpoint."""
  302. @pytest.fixture
  303. async def printer_factory(self, db_session):
  304. """Factory to create test printers."""
  305. async def _create_printer(**kwargs):
  306. from backend.app.models.printer import Printer
  307. defaults = {
  308. "name": "Cancel Test Printer",
  309. "ip_address": "192.168.1.200",
  310. "serial_number": "TESTCANCEL001",
  311. "access_code": "12345678",
  312. "model": "X1C",
  313. }
  314. defaults.update(kwargs)
  315. printer = Printer(**defaults)
  316. db_session.add(printer)
  317. await db_session.commit()
  318. await db_session.refresh(printer)
  319. return printer
  320. return _create_printer
  321. @pytest.fixture
  322. async def archive_factory(self, db_session):
  323. """Factory to create test archives."""
  324. async def _create_archive(**kwargs):
  325. from backend.app.models.archive import PrintArchive
  326. defaults = {
  327. "filename": "cancel_test.3mf",
  328. "print_name": "Cancel Test Print",
  329. "file_path": "/tmp/cancel_test.3mf",
  330. "file_size": 1024,
  331. "content_hash": "cancelhash001",
  332. "status": "completed",
  333. }
  334. defaults.update(kwargs)
  335. archive = PrintArchive(**defaults)
  336. db_session.add(archive)
  337. await db_session.commit()
  338. await db_session.refresh(archive)
  339. return archive
  340. return _create_archive
  341. @pytest.fixture
  342. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  343. """Factory to create test queue items."""
  344. async def _create_queue_item(**kwargs):
  345. from backend.app.models.print_queue import PrintQueueItem
  346. if "printer_id" not in kwargs:
  347. printer = await printer_factory()
  348. kwargs["printer_id"] = printer.id
  349. if "archive_id" not in kwargs:
  350. archive = await archive_factory()
  351. kwargs["archive_id"] = archive.id
  352. defaults = {
  353. "status": "pending",
  354. "position": 1,
  355. }
  356. defaults.update(kwargs)
  357. item = PrintQueueItem(**defaults)
  358. db_session.add(item)
  359. await db_session.commit()
  360. await db_session.refresh(item)
  361. return item
  362. return _create_queue_item
  363. @pytest.mark.asyncio
  364. @pytest.mark.integration
  365. async def test_cancel_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  366. """Verify cancelling a pending queue item."""
  367. item = await queue_item_factory(status="pending")
  368. response = await async_client.post(f"/api/v1/queue/{item.id}/cancel")
  369. assert response.status_code == 200
  370. assert response.json()["message"] == "Queue item cancelled"
  371. @pytest.mark.asyncio
  372. @pytest.mark.integration
  373. async def test_cancel_non_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  374. """Verify 400 error when trying to cancel a non-pending queue item."""
  375. item = await queue_item_factory(status="printing")
  376. response = await async_client.post(f"/api/v1/queue/{item.id}/cancel")
  377. assert response.status_code == 400