TestPrinterOutputModel.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from unittest.mock import MagicMock
  2. import pytest
  3. from cura.PrinterOutput.PrintJobOutputModel import PrintJobOutputModel
  4. from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
  5. test_validate_data_get_set = [
  6. {"attribute": "name", "value": "YAY"},
  7. {"attribute": "targetBedTemperature", "value": 192},
  8. ]
  9. test_validate_data_get_update = [
  10. {"attribute": "isPreheating", "value": True},
  11. {"attribute": "type", "value": "WHOO"},
  12. {"attribute": "buildplate", "value": "NFHA"},
  13. {"attribute": "key", "value": "YAY"},
  14. {"attribute": "name", "value": "Turtles"},
  15. {"attribute": "bedTemperature", "value": 200},
  16. {"attribute": "targetBedTemperature", "value": 9001},
  17. {"attribute": "activePrintJob", "value": PrintJobOutputModel(MagicMock())},
  18. {"attribute": "state", "value": "BEEPBOOP"},
  19. ]
  20. @pytest.mark.parametrize("data", test_validate_data_get_set)
  21. def test_getAndSet(data):
  22. model = PrinterOutputModel(MagicMock())
  23. # Convert the first letter into a capital
  24. attribute = list(data["attribute"])
  25. attribute[0] = attribute[0].capitalize()
  26. attribute = "".join(attribute)
  27. # mock the correct emit
  28. setattr(model, data["attribute"] + "Changed", MagicMock())
  29. # Attempt to set the value
  30. getattr(model, "set" + attribute)(data["value"])
  31. # Check if signal fired.
  32. signal = getattr(model, data["attribute"] + "Changed")
  33. assert signal.emit.call_count == 1
  34. # Ensure that the value got set
  35. assert getattr(model, data["attribute"]) == data["value"]
  36. # Attempt to set the value again
  37. getattr(model, "set" + attribute)(data["value"])
  38. # The signal should not fire again
  39. assert signal.emit.call_count == 1
  40. @pytest.mark.parametrize("data", test_validate_data_get_update)
  41. def test_getAndUpdate(data):
  42. model = PrinterOutputModel(MagicMock())
  43. # Convert the first letter into a capital
  44. attribute = list(data["attribute"])
  45. attribute[0] = attribute[0].capitalize()
  46. attribute = "".join(attribute)
  47. # mock the correct emit
  48. setattr(model, data["attribute"] + "Changed", MagicMock())
  49. # Attempt to set the value
  50. getattr(model, "update" + attribute)(data["value"])
  51. # Check if signal fired.
  52. signal = getattr(model, data["attribute"] + "Changed")
  53. assert signal.emit.call_count == 1
  54. # Ensure that the value got set
  55. assert getattr(model, data["attribute"]) == data["value"]
  56. # Attempt to set the value again
  57. getattr(model, "update" + attribute)(data["value"])
  58. # The signal should not fire again
  59. assert signal.emit.call_count == 1