TestMachineAction.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #Todo: Write tests
  2. import pytest
  3. # QtApplication needs to be imported first to prevent import errors.
  4. from UM.Qt.QtApplication import QtApplication
  5. from cura.MachineAction import MachineAction
  6. from cura.MachineActionManager import MachineActionManager, NotUniqueMachineActionError, UnknownMachineActionError
  7. class Machine:
  8. def __init__(self, key = ""):
  9. self._key = key
  10. def getKey(self):
  11. return self._key
  12. def test_addMachineAction():
  13. machine_manager = MachineActionManager()
  14. test_action = MachineAction(key = "test_action")
  15. test_action_2 = MachineAction(key = "test_action_2")
  16. test_machine = Machine("test_machine")
  17. machine_manager.addMachineAction(test_action)
  18. machine_manager.addMachineAction(test_action_2)
  19. assert machine_manager.getMachineAction("test_action") == test_action
  20. assert machine_manager.getMachineAction("key_that_doesnt_exist") is None
  21. # Adding the same machine action is not allowed.
  22. with pytest.raises(NotUniqueMachineActionError):
  23. machine_manager.addMachineAction(test_action)
  24. # Check that the machine has no supported actions yet.
  25. assert machine_manager.getSupportedActions(test_machine) == list()
  26. # Check if adding a supported action works.
  27. machine_manager.addSupportedAction(test_machine, "test_action")
  28. assert machine_manager.getSupportedActions(test_machine) == [test_action, ]
  29. # Check that adding a unknown action doesn't change anything.
  30. machine_manager.addSupportedAction(test_machine, "key_that_doesnt_exist")
  31. assert machine_manager.getSupportedActions(test_machine) == [test_action, ]
  32. # Check if adding multiple supported actions works.
  33. machine_manager.addSupportedAction(test_machine, "test_action_2")
  34. assert machine_manager.getSupportedActions(test_machine) == [test_action, test_action_2]
  35. # Check that the machine has no required actions yet.
  36. assert machine_manager.getRequiredActions(test_machine) == set()
  37. ## Ensure that only known actions can be added.
  38. with pytest.raises(UnknownMachineActionError):
  39. machine_manager.addRequiredAction(test_machine, "key_that_doesnt_exist")
  40. ## Check if adding single required action works
  41. machine_manager.addRequiredAction(test_machine, "test_action")
  42. assert machine_manager.getRequiredActions(test_machine) == [test_action, ]
  43. # Check if adding multiple required actions works.
  44. machine_manager.addRequiredAction(test_machine, "test_action_2")
  45. assert machine_manager.getRequiredActions(test_machine) == [test_action, test_action_2]
  46. # Ensure that firstStart actions are empty by default.
  47. assert machine_manager.getFirstStartActions(test_machine) == []
  48. # Check if adding multiple (the same) actions to first start actions work.
  49. machine_manager.addFirstStartAction(test_machine, "test_action")
  50. machine_manager.addFirstStartAction(test_machine, "test_action")
  51. assert machine_manager.getFirstStartActions(test_machine) == [test_action, test_action]
  52. # Check if inserting an action works
  53. machine_manager.addFirstStartAction(test_machine, "test_action_2", index = 1)
  54. assert machine_manager.getFirstStartActions(test_machine) == [test_action, test_action_2, test_action]
  55. # Check that adding a unknown action doesn't change anything.
  56. machine_manager.addFirstStartAction(test_machine, "key_that_doesnt_exist", index = 1)
  57. assert machine_manager.getFirstStartActions(test_machine) == [test_action, test_action_2, test_action]