test_spoolman_filament_name_save_3561.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """Saving a Spoolman spool leaves the filament's name alone (#3561).
  2. The update route rebuilt the filament's name as "<material> <subtype>" and
  3. PATCHed it whenever that differed from the stored one -- so saving any change
  4. to a spool whose filament is called "Matte Scarlet Red" renamed it in Spoolman.
  5. With the name now read as subtype "Matte" and colour "Scarlet Red", the same
  6. rule would have cut it down to "PLA Matte" and lost the colour from the name.
  7. The name is only rebuilt when material or subtype is actually changed.
  8. """
  9. import pytest
  10. from httpx import AsyncClient
  11. from backend.tests.integration.test_spoolman_inventory_api import ( # noqa: F401 (fixtures)
  12. SAMPLE_SPOOLMAN_SPOOL,
  13. mock_spoolman_client,
  14. spoolman_settings,
  15. )
  16. PRODUCT_NAMED = {
  17. **SAMPLE_SPOOLMAN_SPOOL,
  18. "filament": {**SAMPLE_SPOOLMAN_SPOOL["filament"], "name": "Matte Scarlet Red", "material": "PLA"},
  19. }
  20. @pytest.fixture
  21. def product_named(mock_spoolman_client): # noqa: F811
  22. mock_spoolman_client.get_spool.return_value = PRODUCT_NAMED
  23. return mock_spoolman_client
  24. @pytest.mark.asyncio
  25. @pytest.mark.integration
  26. async def test_saving_the_form_unchanged_does_not_rename_the_filament(
  27. async_client: AsyncClient,
  28. spoolman_settings, # noqa: F811
  29. product_named,
  30. ):
  31. """What the form sends back: the subtype and colour it was shown."""
  32. response = await async_client.patch(
  33. "/api/v1/spoolman/inventory/spools/42",
  34. json={"material": "PLA", "subtype": "Matte", "color_name": "Scarlet Red", "note": "moved to shelf 2"},
  35. )
  36. assert response.status_code == 200
  37. product_named.patch_filament.assert_not_called()
  38. product_named.find_or_create_filament.assert_not_called()
  39. @pytest.mark.asyncio
  40. @pytest.mark.integration
  41. async def test_a_colour_change_keeps_the_name_too(
  42. async_client: AsyncClient,
  43. spoolman_settings, # noqa: F811
  44. product_named,
  45. ):
  46. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json={"rgba": "0000FFFF"})
  47. assert response.status_code == 200
  48. product_named.patch_filament.assert_called_once()
  49. assert product_named.patch_filament.call_args.args[1]["name"] == "Matte Scarlet Red"
  50. @pytest.mark.asyncio
  51. @pytest.mark.integration
  52. async def test_changing_the_subtype_still_renames_it(
  53. async_client: AsyncClient,
  54. spoolman_settings, # noqa: F811
  55. product_named,
  56. ):
  57. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json={"subtype": "Silk"})
  58. assert response.status_code == 200
  59. product_named.patch_filament.assert_called_once()
  60. assert product_named.patch_filament.call_args.args[1]["name"] == "PLA Silk"