TestPrinterOutputModel.py 2.6 KB

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