def test_Action_Construct(self): construct = FakeAction(typecheck='Action instantiation fails.') result = construct.perform() self.assertEqual(construct.failure, result.value) self.assertIsInstance(construct, Action.Construct) self.assertIsInstance(result, Result) self.assertIsInstance(result.value, Exception)
def test_Action_can_be_renamed(self): action = FakeAction() self.assertIsNone(action.name) name1 = 'new' action.set(name=name1) self.assertEqual(action.name, name1) name2 = 'different name' action.name = name2 self.assertEqual(action.name, name2)
def test_Result_has_timestamp(self): result = FakeAction(instruction_provider=lambda: 'succeeded').perform( timestamp_provider=lambda: 0 ) self.assertTrue(result.successful) self.assertEqual(result.produced_at, 0)
def test_can_Pipeline_multiple_calls(self): response = "We can't leave him here. May I keep him?" def first_function(param): return 'first', param def second_function(param): return 'second', param action = FakeAction(instruction_provider=lambda: response) action_types = [ Pipeline.Fitting( action=Call, enclose=first_function, ), Pipeline.Fitting( action=Call, enclose=second_function, ) ] pipeline = Pipeline(action, *action_types, should_raise=True) result = pipeline.perform(should_raise=True) self.assertIsInstance(result, Result) self.assertEqual(result.value, ('second', ('first', response)))
def test_Action_can_be_serialized(self): action = FakeAction() pickled = pickle.dumps(action) unpickled = pickle.loads(pickled) self.assertEqual(pickleable(action), pickled) self.assertEqual(unpickled.result, action.result) self.assertEqual(unpickled.state, action.state)
def test_can_react_to_failure(self): vessel = [] contents = 'contents' def fill(): vessel.append(contents) self.assertNotIn(contents, vessel) reaction = FakeAction(instruction_provider=fill) action = FakeAction( instruction_provider=self.raise_failure, reaction=reaction ) result = action.perform() self.assertFalse(result.successful) self.assertIn(contents, vessel)
def test_can_validate_KeyedProcedure(self): with self.assertRaises(KeyedProcedure.UnnamedAction): KeyedProcedure((FakeAction(), failure)).validate() with self.assertRaises(KeyedProcedure.NotAnAction): KeyedProcedure[str, str](( success, 'not an action', )).validate() self.assertFalse(list(KeyedProcedure(actions=list()).validate()))
def test_Result_success_is_immutable(self): success = FakeAction().perform() failure = FakeAction(instruction_provider=self.raise_failure).perform() with self.assertRaises(AttributeError): success.successful = 'nah.' with self.assertRaises(AttributeError): failure.successful = 'maybe?'
def test_DependencyCheck_fails_if_package_missing(self): FakeAction.requirements = ('not-a-real-packing-never-will-be',) with self.assertRaises(Action.DependencyCheck.PackageMissing): FakeAction().check_dependencies(FakeAction)
def test_can_determine_if_Result_was_successful(self): success = FakeAction().perform() failure = FakeAction(instruction_provider=self.raise_failure).perform() self.assertTrue(success.successful) self.assertFalse(failure.successful)
def test_Action_can_raise_exception(self): with self.assertRaises(type(self.exception)): FakeAction(instruction_provider=self.raise_failure).perform(should_raise=True)
def test_Action_produces_Result_if_exception_raised_when_performed(self): result = FakeAction(instruction_provider=self.raise_failure).perform() self.assertIsInstance(result, Result) self.assertEqual(result.value, self.exception)
def test_Action_produces_Result_result_when_performed(self): result = FakeAction().perform() self.assertIsInstance(result, Result) self.assertEqual(result.value, FakeAction.result)
def test_can_Pipeline_constitutes_iterator_over_Action_types(self): action_types = [FakeAction] * 3 pipeline = Pipeline(FakeAction(), *action_types) self.assertEqual(list(pipeline), action_types)
def test_Pipeline_Fitting_defines_instruction_replacement(self): result = Pipeline.Fitting.instruction(Pipeline.Fitting(FakeAction())) self.assertTrue(result.successful) result = Pipeline.Fitting.instruction(Pipeline.Fitting(FakeAction(), reaction=FakeAction())) self.assertTrue(result.successful)
def test_Action_Construct_has_string_representation(self): failed_action_instantiation = FakeAction(typecheck=True) result = failed_action_instantiation.perform() self.assertIsInstance(failed_action_instantiation, Action.Construct) self.assertEqual(repr(failed_action_instantiation), f'<Action.Construct[{result.value.__class__.__name__}]>')
def test_can_compare_Actions(self): fake_action = FakeAction() self.assertTrue(fake_action == FakeAction()) with self.assertRaises(Action.NotComparable): fake_action == '<fake_action>'
def test_Action_perform_fails_for_non_callable_instruction(self): action = FakeAction(instruction_provider='nope.') result = action.perform() self.assertFalse(result.successful) self.assertIsInstance(result.value, TypeError)
def test_can_delete_Action_name(self): action = FakeAction() action_name = 'gone soon.' action.set(name=action_name) self.assertEqual(action.name, action_name) del action.name
from textwrap import dedent from unittest import TestCase from actionpack import KeyedProcedure from actionpack import Procedure from actionpack.action import Result from tests.actionpack import FakeAction from tests.actionpack import FakeFile from tests.actionpack.actions import FakeWrite def raise_failure(): raise Exception('something went wrong :/') success = FakeAction(name='success') failure = FakeAction(name='failure', instruction_provider=raise_failure) def assertIsIterable(possible_collection): return isinstance(possible_collection, Iterable) class ProcedureTest(TestCase): def setUp(self): self.procedure = Procedure((success, failure)) def test_cannot_instantiate_without_Actions(self): with self.assertRaises(TypeError): Procedure(actions=FakeAction()) Procedure(actions=None)
def test_Action_returning_exception_does_not_have_successful_Result(self): result = FakeAction(instruction_provider=lambda: self.exception).perform() self.assertIsInstance(result, Result) self.assertFalse(result.successful)
def test_cannot_instantiate_without_Actions(self): with self.assertRaises(TypeError): Procedure(actions=FakeAction()) Procedure(actions=None)
def test_Action_initialized_with_Exception_has_unsuccessful_Result(self): result = FakeAction(self.exception).perform() self.assertIsInstance(result, Result) self.assertFalse(result.successful)