test_filaments_api.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """Integration tests for Filaments API endpoints."""
  2. import pytest
  3. from httpx import AsyncClient
  4. class TestFilamentsAPI:
  5. """Integration tests for /api/v1/filament-catalog/ (material types) 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/filament-catalog/")
  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(self, async_client: AsyncClient, filament_factory, db_session):
  36. """Verify list returns existing filaments."""
  37. await filament_factory(name="Test Filament")
  38. response = await async_client.get("/api/v1/filament-catalog/")
  39. assert response.status_code == 200
  40. data = response.json()
  41. assert any(f["name"] == "Test Filament" for f in data)
  42. @pytest.mark.asyncio
  43. @pytest.mark.integration
  44. async def test_create_filament(self, async_client: AsyncClient):
  45. """Verify filament can be created."""
  46. data = {
  47. "name": "New PETG",
  48. "type": "PETG",
  49. "color": "Blue",
  50. "color_hex": "#0000FF",
  51. "brand": "Bambu",
  52. "cost_per_kg": 30.0,
  53. }
  54. response = await async_client.post("/api/v1/filament-catalog/", json=data)
  55. assert response.status_code == 200
  56. result = response.json()
  57. assert result["name"] == "New PETG"
  58. assert result["type"] == "PETG"
  59. @pytest.mark.asyncio
  60. @pytest.mark.integration
  61. async def test_get_filament(self, async_client: AsyncClient, filament_factory, db_session):
  62. """Verify single filament can be retrieved."""
  63. filament = await filament_factory(name="Get Test")
  64. response = await async_client.get(f"/api/v1/filament-catalog/{filament.id}")
  65. assert response.status_code == 200
  66. assert response.json()["name"] == "Get Test"
  67. @pytest.mark.asyncio
  68. @pytest.mark.integration
  69. async def test_get_filament_not_found(self, async_client: AsyncClient):
  70. """Verify 404 for non-existent filament."""
  71. response = await async_client.get("/api/v1/filament-catalog/9999")
  72. assert response.status_code == 404
  73. @pytest.mark.asyncio
  74. @pytest.mark.integration
  75. async def test_update_filament(self, async_client: AsyncClient, filament_factory, db_session):
  76. """Verify filament can be updated."""
  77. filament = await filament_factory(name="Original")
  78. response = await async_client.patch(
  79. f"/api/v1/filament-catalog/{filament.id}", json={"name": "Updated", "cost_per_kg": 35.0}
  80. )
  81. assert response.status_code == 200
  82. result = response.json()
  83. assert result["name"] == "Updated"
  84. assert result["cost_per_kg"] == 35.0
  85. @pytest.mark.asyncio
  86. @pytest.mark.integration
  87. async def test_delete_filament(self, async_client: AsyncClient, filament_factory, db_session):
  88. """Verify filament can be deleted."""
  89. filament = await filament_factory()
  90. response = await async_client.delete(f"/api/v1/filament-catalog/{filament.id}")
  91. assert response.status_code == 200
  92. # Verify deleted
  93. response = await async_client.get(f"/api/v1/filament-catalog/{filament.id}")
  94. assert response.status_code == 404