Skip to content

Commit

Permalink
actionpack#141: "adds another test" (#143)
Browse files Browse the repository at this point in the history
* moves Result tests to ResultTest suite

* adds return type for partialaction

* refactors partialaction test

* adds partialaction string representation test
  • Loading branch information
withtwoemms committed Oct 10, 2022
1 parent c1c7ec0 commit dbfa858
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion actionpack/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def instruction():
return instance


def partialaction(name, parent: ActionType, **kwargs):
def partialaction(name, parent: ActionType, **kwargs) -> ActionType:
partial__init__ = partialmethod(parent.__init__, **kwargs)
return type(name, (parent,), {'__init__': partial__init__})

Expand Down
57 changes: 35 additions & 22 deletions tests/actionpack/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ def test_Action_can_raise_exception(self):
FakeAction(instruction_provider=self.raise_failure).perform(should_raise=True)

def test_can_create_partial_Action(self):
def instruction_provider():
return some_result_value

some_result_value = 'success!'
instruction_provider = lambda: some_result_value
FakeActionWithInstruction = partialaction(
PartialAction = partialaction(
'FakeActionWithInstruction',
FakeAction,
instruction_provider=instruction_provider
)
self.assertEqual(FakeActionWithInstruction().perform().value, some_result_value)
self.assertEqual(PartialAction().perform().value, some_result_value)

def test_can_determine_if_Result_was_successful(self):
success = FakeAction().perform()
Expand Down Expand Up @@ -83,24 +85,6 @@ def fill():
self.assertFalse(result.successful)
self.assertIn(contents, vessel)

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_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_Action_Construct(self):
construct = FakeAction(typecheck='Action instantiation fails.')
result = construct.perform()
Expand Down Expand Up @@ -178,6 +162,15 @@ def test_Action_Construct_has_string_representation(self):
self.assertIsInstance(failed_action_instantiation, Action.Construct)
self.assertEqual(repr(failed_action_instantiation), f'<Action.Construct[{result.value.__class__.__name__}]>')

def test_partial_Action_has_string_representation(self):
partial_action_name = 'FakeActionWithInstruction'
PartialAction = partialaction(
partial_action_name,
FakeAction,
instruction_provider=lambda: 'success!'
)
self.assertEqual(repr(PartialAction()), f'<{partial_action_name}>')

def test_DependencyCheck_fails_if_package_missing(self):
FakeAction.requirements = ('not-a-real-packing-never-will-be',)
with self.assertRaises(Action.DependencyCheck.PackageMissing):
Expand All @@ -190,6 +183,27 @@ def test_DependencyCheck_fails_if_requirement_absent(self):

class ResultTest(TestCase):

def raise_failure(self):
raise self.exception

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_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_cannot_instantiate_without_Either(self):
with self.assertRaises(Result.OutcomeMustBeOfTypeEither):
Result('not an Either type')
Expand Down Expand Up @@ -228,4 +242,3 @@ def test_can_serialize_result(self):

confused_result = Result(Left(successful_outcome))
self.assertFalse(confused_result.successful)

0 comments on commit dbfa858

Please sign in to comment.