test_filaments_api.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Integration tests for Filaments API endpoints."""
  2. import pytest
  3. from httpx import AsyncClient
  4. class TestFilamentsAPI:
  5. """Integration tests for /api/v1/filaments/ endpoints."""
  6. @pytest.fixture
  7. async def filament_factory(self, db_session):
  8. """Factory to create test filaments."""
  9. async def _create_filament(**kwargs):
  10. from backend.app.models.filament import Filament
  11. defaults = {
  12. "name": "Test PLA",
  13. "type": "PLA",
  14. "color": "Red",
  15. "color_hex": "#FF0000",
  16. "brand": "Generic",
  17. "cost_per_kg": 25.0,
  18. }
  19. defaults.update(kwargs)
  20. filament = Filament(**defaults)
  21. db_session.add(filament)
  22. await db_session.commit()
  23. await db_session.refresh(filament)
  24. return filament
  25. return _create_filament
  26. @pytest.mark.asyncio
  27. @pytest.mark.integration
  28. async def test_list_filaments_empty(self, async_client: AsyncClient):
  29. """Verify empty list when no filaments exist."""
  30. response = await async_client.get("/api/v1/filaments/")
  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_filaments_with_data(
  36. self, async_client: AsyncClient, filament_factory, db_session
  37. ):
  38. """Verify list returns existing filaments."""
  39. await filament_factory(name="Test Filament")
  40. response = await async_client.get("/api/v1/filaments/")
  41. assert response.status_code == 200
  42. data = response.json()
  43. assert any(f["name"] == "Test Filament" for f in data)
  44. @pytest.mark.asyncio
  45. @pytest.mark.integration
  46. async def test_create_filament(self, async_client: AsyncClient):
  47. """Verify filament can be created."""
  48. data = {
  49. "name": "New PETG",
  50. "type": "PETG",
  51. "color": "Blue",
  52. "color_hex": "#0000FF",
  53. "brand": "Bambu",
  54. "cost_per_kg": 30.0,
  55. }
  56. response = await async_client.post("/api/v1/filaments/", json=data)
  57. assert response.status_code == 200
  58. result = response.json()
  59. assert result["name"] == "New PETG"
  60. assert result["type"] == "PETG"
  61. @pytest.mark.asyncio
  62. @pytest.mark.integration
  63. async def test_get_filament(
  64. self, async_client: AsyncClient, filament_factory, db_session
  65. ):
  66. """Verify single filament can be retrieved."""
  67. filament = await filament_factory(name="Get Test")
  68. response = await async_client.get(f"/api/v1/filaments/{filament.id}")
  69. assert response.status_code == 200
  70. assert response.json()["name"] == "Get Test"
  71. @pytest.mark.asyncio
  72. @pytest.mark.integration
  73. async def test_get_filament_not_found(self, async_client: AsyncClient):
  74. """Verify 404 for non-existent filament."""
  75. response = await async_client.get("/api/v1/filaments/9999")
  76. assert response.status_code == 404
  77. @pytest.mark.asyncio
  78. @pytest.mark.integration
  79. async def test_update_filament(
  80. self, async_client: AsyncClient, filament_factory, db_session
  81. ):
  82. """Verify filament can be updated."""
  83. filament = await filament_factory(name="Original")
  84. response = await async_client.patch(
  85. f"/api/v1/filaments/{filament.id}",
  86. json={"name": "Updated", "cost_per_kg": 35.0}
  87. )
  88. assert response.status_code == 200
  89. result = response.json()
  90. assert result["name"] == "Updated"
  91. assert result["cost_per_kg"] == 35.0
  92. @pytest.mark.asyncio
  93. @pytest.mark.integration
  94. async def test_delete_filament(
  95. self, async_client: AsyncClient, filament_factory, db_session
  96. ):
  97. """Verify filament can be deleted."""
  98. filament = await filament_factory()
  99. response = await async_client.delete(f"/api/v1/filaments/{filament.id}")
  100. assert response.status_code == 200
  101. # Verify deleted
  102. response = await async_client.get(f"/api/v1/filaments/{filament.id}")
  103. assert response.status_code == 404