def test_all_execute_when_no_errors_are_thrown(self): # Setup command chain with mocked __do__ functions c2 = Command() c1 = Command(next=c2) c1.__do__ = Mock() c2.__do__ = Mock() # Action c1.run() # Assert all functions called c1.__do__.assert_called_once() c2.__do__.assert_called_once()
def test_chained_command_does_not_execute_if_parent_throws_exception(self): # Assemble command chain with mocked __do__ functions # Initial command triggered will throw exception c1 = Command() c2 = Command(previous=c1) c1.__do__ = Mock(side_effect=Exception('Oh No!')) c2.__do__ = Mock() # Action c1.run() # Assert command one was called (Threw an exception) and c2 was not called c1.__do__.assert_called_once() c2.__do__.assert_not_called()
def test_set_action_overrides_with_lambda(self): # Assemble c1 = Command() # Action c1.set_action(lambda x, y: x + y) # Assert self.assertEqual(3, c1.__do__(1, 2))
def test_set_action_overrides_with_function(self): c1 = Command() def my_action(): return 1 c1.set_action(my_action) self.assertEqual(1, c1.__do__())
def test_set_action_overrides_with_function_and_allows_params(self): # Assemble c1 = Command() def my_action(x, y): return x + y # Action c1.set_action(my_action) # Assert self.assertEqual(3, c1.__do__(1, 2))
def test_child_command_rolls_back_self_on_exception(self): # Assemble command chain with mocked __do__ functions # Initial command triggered will throw exception c1 = Command() c2 = Command(previous=c1) c2.__do__ = Mock(side_effect=Exception('Oh No!')) c2.__undo__ = Mock() # Action c1.run() # Assert command 2 was rolled back c2.__undo__.assert_called_once()