TestMachineAction.py 3.3 KB

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