test_spoolman_filament_name_3561.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Subtype and colour from a Spoolman filament's name (#3561).
  2. Spoolman has neither field, only the filament's name. Bambuddy writes that as
  3. "<material> <subtype>", and that was all it could read back: a filament named
  4. after the product -- "Matte Scarlet Red" -- got the whole name as its subtype
  5. and no colour, so the spool form showed the colour blank.
  6. """
  7. import pytest
  8. from backend.app.api.routes._spoolman_helpers import _map_spoolman_spool, split_spoolman_filament_name
  9. @pytest.mark.parametrize(
  10. ("material", "name", "subtype", "colour"),
  11. [
  12. # The three shapes from the report.
  13. ("PLA", "Matte Scarlet Red", "Matte", "Scarlet Red"),
  14. ("PLA", "Silk+ Baby Blue", "Silk+", "Baby Blue"),
  15. ("PLA", "Gold - Basic PLA", "Basic", "Gold"),
  16. # Punctuation attached to a word is a separator too.
  17. ("PLA", "Scarlet Red (Matte)", "Matte", "Scarlet Red"),
  18. ("PLA", "Matte, Scarlet Red", "Matte", "Scarlet Red"),
  19. # The line in the material, the colour in the name.
  20. ("PLA Matte", "PLA Matte Grey", None, "Grey"),
  21. # A material and a finish at the front.
  22. ("PLA", "PLA Basic Red", "Basic", "Red"),
  23. ("PETG", "PETG HF Black", "HF", "Black"),
  24. ("PLA", "PLA Dual Color Red Blue", "Dual Color", "Red Blue"),
  25. # Only a colour.
  26. ("PLA", "Scarlet Red", None, "Scarlet Red"),
  27. ("", "Jade White", None, "Jade White"),
  28. ("PLA", "PLA", None, None),
  29. ("PLA", "", None, None),
  30. ],
  31. )
  32. def test_the_name_splits_into_subtype_and_colour(material, name, subtype, colour):
  33. assert split_spoolman_filament_name(material, name) == (subtype, colour)
  34. @pytest.mark.parametrize(
  35. ("material", "name", "subtype"),
  36. [
  37. ("PLA", "PLA Matte", "Matte"),
  38. ("PLA Matte", "PLA Matte Matte", "Matte"),
  39. # A subtype that is no finish still comes back as the subtype: the name
  40. # starts with the material, which is how Bambuddy writes it.
  41. ("PLA", "PLA Engineering", "Engineering"),
  42. ("PLA", "PLA Jade White", "Jade White"),
  43. ],
  44. )
  45. def test_a_name_bambuddy_wrote_reads_back_as_it_was_written(material, name, subtype):
  46. assert split_spoolman_filament_name(material, name) == (subtype, None)
  47. def test_the_material_is_only_taken_as_a_whole_word():
  48. """PLA-CF is not PLA followed by "-CF"."""
  49. assert split_spoolman_filament_name("PLA", "Black PLA-CF") == (None, "Black PLA-CF")
  50. def _spool(name, material="PLA", extra=None):
  51. return {
  52. "id": 5,
  53. "filament": {"id": 2, "name": name, "material": material, "color_hex": "C8102E"},
  54. "extra": extra or {},
  55. }
  56. class TestTheMappedSpool:
  57. def test_the_reported_case(self):
  58. mapped = _map_spoolman_spool(_spool("Matte Scarlet Red"))
  59. assert mapped["material"] == "PLA"
  60. assert mapped["subtype"] == "Matte"
  61. assert mapped["color_name"] == "Scarlet Red"
  62. # Named by the user, so the edit form shows it rather than leaving it
  63. # blank as it does for a colour made up from the subtype.
  64. assert mapped["color_name_is_synthesized"] is False
  65. def test_a_colour_bambuddy_stored_still_wins(self):
  66. mapped = _map_spoolman_spool(_spool("Matte Scarlet Red", extra={"bambu_color_name": '"Crimson"'}))
  67. assert mapped["color_name"] == "Crimson"
  68. assert mapped["subtype"] == "Matte"
  69. def test_a_name_without_a_colour_still_synthesises_one(self):
  70. mapped = _map_spoolman_spool(_spool("PLA Matte"))
  71. assert mapped["subtype"] == "Matte"
  72. assert mapped["color_name"] == "Matte"
  73. assert mapped["color_name_is_synthesized"] is True