TestPrinterOutputModel.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from unittest.mock import MagicMock
  2. import pytest
  3. from cura.PrinterOutput.Models.PrintJobOutputModel import PrintJobOutputModel
  4. from cura.PrinterOutput.Models.PrinterConfigurationModel import PrinterConfigurationModel
  5. from cura.PrinterOutput.Models.PrinterOutputModel import PrinterOutputModel
  6. from cura.PrinterOutput.Peripheral import Peripheral
  7. test_validate_data_get_set = [
  8. {"attribute": "name", "value": "YAY"},
  9. {"attribute": "targetBedTemperature", "value": 192},
  10. {"attribute": "cameraUrl", "value": "YAY!"}
  11. ]
  12. test_validate_data_get_update = [
  13. {"attribute": "isPreheating", "value": True},
  14. {"attribute": "type", "value": "WHOO"},
  15. {"attribute": "buildplate", "value": "NFHA"},
  16. {"attribute": "key", "value": "YAY"},
  17. {"attribute": "name", "value": "Turtles"},
  18. {"attribute": "bedTemperature", "value": 200},
  19. {"attribute": "targetBedTemperature", "value": 9001},
  20. {"attribute": "activePrintJob", "value": PrintJobOutputModel(MagicMock())},
  21. {"attribute": "state", "value": "BEEPBOOP"},
  22. ]
  23. @pytest.mark.parametrize("data", test_validate_data_get_set)
  24. def test_getAndSet(data):
  25. model = PrinterOutputModel(MagicMock())
  26. # Convert the first letter into a capital
  27. attribute = list(data["attribute"])
  28. attribute[0] = attribute[0].capitalize()
  29. attribute = "".join(attribute)
  30. # mock the correct emit
  31. setattr(model, data["attribute"] + "Changed", MagicMock())
  32. # Attempt to set the value
  33. getattr(model, "set" + attribute)(data["value"])
  34. # Check if signal fired.
  35. signal = getattr(model, data["attribute"] + "Changed")
  36. assert signal.emit.call_count == 1
  37. # Ensure that the value got set
  38. assert getattr(model, data["attribute"]) == data["value"]
  39. # Attempt to set the value again
  40. getattr(model, "set" + attribute)(data["value"])
  41. # The signal should not fire again
  42. assert signal.emit.call_count == 1
  43. @pytest.mark.parametrize("data", test_validate_data_get_update)
  44. def test_getAndUpdate(data):
  45. model = PrinterOutputModel(MagicMock())
  46. # Convert the first letter into a capital
  47. attribute = list(data["attribute"])
  48. attribute[0] = attribute[0].capitalize()
  49. attribute = "".join(attribute)
  50. # mock the correct emit
  51. setattr(model, data["attribute"] + "Changed", MagicMock())
  52. # Attempt to set the value
  53. getattr(model, "update" + attribute)(data["value"])
  54. # Check if signal fired.
  55. signal = getattr(model, data["attribute"] + "Changed")
  56. assert signal.emit.call_count == 1
  57. # Ensure that the value got set
  58. assert getattr(model, data["attribute"]) == data["value"]
  59. # Attempt to set the value again
  60. getattr(model, "update" + attribute)(data["value"])
  61. # The signal should not fire again
  62. assert signal.emit.call_count == 1
  63. def test_peripherals():
  64. model = PrinterOutputModel(MagicMock())
  65. model.peripheralsChanged = MagicMock()
  66. peripheral = MagicMock(spec=Peripheral)
  67. peripheral.name = "test"
  68. peripheral2 = MagicMock(spec=Peripheral)
  69. peripheral2.name = "test2"
  70. model.addPeripheral(peripheral)
  71. assert model.peripheralsChanged.emit.call_count == 1
  72. model.addPeripheral(peripheral2)
  73. assert model.peripheralsChanged.emit.call_count == 2
  74. assert model.peripherals == "test, test2"
  75. model.removePeripheral(peripheral)
  76. assert model.peripheralsChanged.emit.call_count == 3
  77. assert model.peripherals == "test2"
  78. def test_availableConfigurations_addConfiguration():
  79. model = PrinterOutputModel(MagicMock())
  80. configuration = MagicMock(spec = PrinterConfigurationModel)
  81. model.addAvailableConfiguration(configuration)
  82. assert model.availableConfigurations == [configuration]
  83. def test_availableConfigurations_addConfigTwice():
  84. model = PrinterOutputModel(MagicMock())
  85. configuration = MagicMock(spec=PrinterConfigurationModel)
  86. model.setAvailableConfigurations([configuration])
  87. assert model.availableConfigurations == [configuration]
  88. # Adding it again should not have any effect
  89. model.addAvailableConfiguration(configuration)
  90. assert model.availableConfigurations == [configuration]
  91. def test_availableConfigurations_removeConfig():
  92. model = PrinterOutputModel(MagicMock())
  93. configuration = MagicMock(spec=PrinterConfigurationModel)
  94. model.addAvailableConfiguration(configuration)
  95. model.removeAvailableConfiguration(configuration)
  96. assert model.availableConfigurations == []
  97. def test_removeAlreadyRemovedConfiguration():
  98. model = PrinterOutputModel(MagicMock())
  99. configuration = MagicMock(spec=PrinterConfigurationModel)
  100. model.availableConfigurationsChanged = MagicMock()
  101. model.removeAvailableConfiguration(configuration)
  102. assert model.availableConfigurationsChanged.emit.call_count == 0
  103. assert model.availableConfigurations == []