test_projects_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. """Integration tests for Projects API endpoints."""
  2. import pytest
  3. from httpx import AsyncClient
  4. class TestProjectsAPI:
  5. """Integration tests for /api/v1/projects endpoints."""
  6. @pytest.fixture
  7. async def project_factory(self, db_session):
  8. """Factory to create test projects."""
  9. _counter = [0]
  10. async def _create_project(**kwargs):
  11. from backend.app.models.project import Project
  12. _counter[0] += 1
  13. counter = _counter[0]
  14. defaults = {
  15. "name": f"Test Project {counter}",
  16. "description": "Test project description",
  17. "color": "#FF0000",
  18. }
  19. defaults.update(kwargs)
  20. project = Project(**defaults)
  21. db_session.add(project)
  22. await db_session.commit()
  23. await db_session.refresh(project)
  24. return project
  25. return _create_project
  26. @pytest.mark.asyncio
  27. @pytest.mark.integration
  28. async def test_list_projects_empty(self, async_client: AsyncClient):
  29. """Verify empty list when no projects exist."""
  30. response = await async_client.get("/api/v1/projects/")
  31. assert response.status_code == 200
  32. assert isinstance(response.json(), list)
  33. @pytest.mark.asyncio
  34. @pytest.mark.integration
  35. async def test_list_projects_with_data(self, async_client: AsyncClient, project_factory, db_session):
  36. """Verify list returns existing projects."""
  37. await project_factory(name="My Project")
  38. response = await async_client.get("/api/v1/projects/")
  39. assert response.status_code == 200
  40. data = response.json()
  41. assert any(p["name"] == "My Project" for p in data)
  42. @pytest.mark.asyncio
  43. @pytest.mark.integration
  44. async def test_create_project(self, async_client: AsyncClient):
  45. """Verify project can be created."""
  46. data = {
  47. "name": "New Project",
  48. "description": "A new project",
  49. "color": "#00FF00",
  50. }
  51. response = await async_client.post("/api/v1/projects/", json=data)
  52. assert response.status_code == 200
  53. result = response.json()
  54. assert result["name"] == "New Project"
  55. assert result["color"] == "#00FF00"
  56. @pytest.mark.asyncio
  57. @pytest.mark.integration
  58. async def test_get_project(self, async_client: AsyncClient, project_factory, db_session):
  59. """Verify single project can be retrieved."""
  60. project = await project_factory(name="Get Test Project")
  61. response = await async_client.get(f"/api/v1/projects/{project.id}")
  62. assert response.status_code == 200
  63. assert response.json()["name"] == "Get Test Project"
  64. @pytest.mark.asyncio
  65. @pytest.mark.integration
  66. async def test_get_project_not_found(self, async_client: AsyncClient):
  67. """Verify 404 for non-existent project."""
  68. response = await async_client.get("/api/v1/projects/9999")
  69. assert response.status_code == 404
  70. @pytest.mark.asyncio
  71. @pytest.mark.integration
  72. async def test_update_project(self, async_client: AsyncClient, project_factory, db_session):
  73. """Verify project can be updated."""
  74. project = await project_factory(name="Original")
  75. response = await async_client.patch(
  76. f"/api/v1/projects/{project.id}", json={"name": "Updated", "description": "Updated description"}
  77. )
  78. assert response.status_code == 200
  79. result = response.json()
  80. assert result["name"] == "Updated"
  81. assert result["description"] == "Updated description"
  82. @pytest.mark.asyncio
  83. @pytest.mark.integration
  84. async def test_delete_project(self, async_client: AsyncClient, project_factory, db_session):
  85. """Verify project can be deleted."""
  86. project = await project_factory()
  87. response = await async_client.delete(f"/api/v1/projects/{project.id}")
  88. assert response.status_code == 200
  89. data = response.json()
  90. assert data["message"] == "Project deleted"
  91. @pytest.mark.asyncio
  92. @pytest.mark.integration
  93. async def test_delete_project_not_found(self, async_client: AsyncClient):
  94. """Verify 404 for deleting non-existent project."""
  95. response = await async_client.delete("/api/v1/projects/9999")
  96. assert response.status_code == 404
  97. class TestProjectPartsTracking:
  98. """Tests for project parts tracking feature."""
  99. @pytest.fixture
  100. async def project_factory(self, db_session):
  101. """Factory to create test projects."""
  102. async def _create_project(**kwargs):
  103. from backend.app.models.project import Project
  104. defaults = {
  105. "name": "Parts Test Project",
  106. "description": "Test project",
  107. "color": "#FF0000",
  108. }
  109. defaults.update(kwargs)
  110. project = Project(**defaults)
  111. db_session.add(project)
  112. await db_session.commit()
  113. await db_session.refresh(project)
  114. return project
  115. return _create_project
  116. @pytest.fixture
  117. async def archive_factory(self, db_session):
  118. """Factory to create test archives."""
  119. async def _create_archive(**kwargs):
  120. from backend.app.models.archive import PrintArchive
  121. defaults = {
  122. "filename": "test.3mf",
  123. "file_path": "test/test.3mf",
  124. "file_size": 1000,
  125. "print_name": "Test Print",
  126. "status": "completed",
  127. "quantity": 1,
  128. }
  129. defaults.update(kwargs)
  130. archive = PrintArchive(**defaults)
  131. db_session.add(archive)
  132. await db_session.commit()
  133. await db_session.refresh(archive)
  134. return archive
  135. return _create_archive
  136. @pytest.mark.asyncio
  137. @pytest.mark.integration
  138. async def test_create_project_with_target_parts_count(self, async_client: AsyncClient):
  139. """Verify project can be created with target_parts_count."""
  140. data = {
  141. "name": "Parts Project",
  142. "target_count": 10, # 10 plates
  143. "target_parts_count": 50, # 50 parts total
  144. }
  145. response = await async_client.post("/api/v1/projects/", json=data)
  146. assert response.status_code == 200
  147. result = response.json()
  148. assert result["target_count"] == 10
  149. assert result["target_parts_count"] == 50
  150. @pytest.mark.asyncio
  151. @pytest.mark.integration
  152. async def test_update_project_target_parts_count(self, async_client: AsyncClient, project_factory, db_session):
  153. """Verify target_parts_count can be updated."""
  154. project = await project_factory()
  155. response = await async_client.patch(
  156. f"/api/v1/projects/{project.id}",
  157. json={"target_parts_count": 100},
  158. )
  159. assert response.status_code == 200
  160. assert response.json()["target_parts_count"] == 100
  161. @pytest.mark.asyncio
  162. @pytest.mark.integration
  163. async def test_project_parts_progress_calculation(
  164. self, async_client: AsyncClient, project_factory, archive_factory, db_session
  165. ):
  166. """Verify parts progress is calculated from archive quantities."""
  167. # Create project with target of 20 parts
  168. project = await project_factory(target_parts_count=20)
  169. # Create archives with different quantities
  170. await archive_factory(project_id=project.id, quantity=3, status="completed") # 3 parts
  171. await archive_factory(project_id=project.id, quantity=5, status="completed") # 5 parts
  172. await archive_factory(project_id=project.id, quantity=2, status="completed") # 2 parts
  173. # Total: 10 parts completed out of 20 = 50%
  174. response = await async_client.get(f"/api/v1/projects/{project.id}")
  175. assert response.status_code == 200
  176. data = response.json()
  177. # Check stats
  178. assert data["stats"]["completed_prints"] == 10 # Sum of quantities
  179. assert data["stats"]["parts_progress_percent"] == 50.0 # 10/20 = 50%
  180. assert data["stats"]["remaining_parts"] == 10 # 20 - 10 = 10
  181. @pytest.mark.asyncio
  182. @pytest.mark.integration
  183. async def test_project_list_shows_parts_count(
  184. self, async_client: AsyncClient, project_factory, archive_factory, db_session
  185. ):
  186. """Verify project list returns correct completed_count (parts sum)."""
  187. project = await project_factory(name="List Parts Project", target_parts_count=100)
  188. # Create archives with quantities
  189. await archive_factory(project_id=project.id, quantity=4, status="completed")
  190. await archive_factory(project_id=project.id, quantity=6, status="completed")
  191. # Total: 10 parts, 2 plates
  192. response = await async_client.get("/api/v1/projects/")
  193. assert response.status_code == 200
  194. data = response.json()
  195. # Find our project
  196. our_project = next((p for p in data if p["name"] == "List Parts Project"), None)
  197. assert our_project is not None
  198. assert our_project["archive_count"] == 2 # 2 plates
  199. assert our_project["completed_count"] == 10 # 10 parts (sum of quantities)
  200. assert our_project["target_parts_count"] == 100
  201. @pytest.mark.asyncio
  202. @pytest.mark.integration
  203. async def test_plates_vs_parts_progress(
  204. self, async_client: AsyncClient, project_factory, archive_factory, db_session
  205. ):
  206. """Verify plates and parts progress are calculated separately."""
  207. # Project needs 5 plates producing 25 parts total (5 parts per plate)
  208. project = await project_factory(target_count=5, target_parts_count=25)
  209. # Complete 2 plates, each with 5 parts
  210. await archive_factory(project_id=project.id, quantity=5, status="completed")
  211. await archive_factory(project_id=project.id, quantity=5, status="completed")
  212. # Plates: 2/5 = 40%, Parts: 10/25 = 40%
  213. response = await async_client.get(f"/api/v1/projects/{project.id}")
  214. assert response.status_code == 200
  215. data = response.json()
  216. assert data["stats"]["total_archives"] == 2 # 2 plates
  217. assert data["stats"]["completed_prints"] == 10 # 10 parts
  218. assert data["stats"]["progress_percent"] == 40.0 # plates: 2/5
  219. assert data["stats"]["parts_progress_percent"] == 40.0 # parts: 10/25
  220. class TestProjectArchivesAPI:
  221. """Tests for project-archive relationships."""
  222. @pytest.fixture
  223. async def project_factory(self, db_session):
  224. """Factory to create test projects."""
  225. async def _create_project(**kwargs):
  226. from backend.app.models.project import Project
  227. defaults = {
  228. "name": "Archive Test Project",
  229. "description": "Test project",
  230. "color": "#0000FF",
  231. }
  232. defaults.update(kwargs)
  233. project = Project(**defaults)
  234. db_session.add(project)
  235. await db_session.commit()
  236. await db_session.refresh(project)
  237. return project
  238. return _create_project
  239. @pytest.mark.asyncio
  240. @pytest.mark.integration
  241. async def test_get_project_with_archives(self, async_client: AsyncClient, project_factory, db_session):
  242. """Verify project can be retrieved with archive count."""
  243. project = await project_factory()
  244. response = await async_client.get(f"/api/v1/projects/{project.id}")
  245. assert response.status_code == 200
  246. # Project should have an archive count (may be 0)
  247. data = response.json()
  248. assert "name" in data