TestPrintJobOutputModel.py 2.6 KB

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