def test_get(self): mock_resource = asynctest.MagicMock() resource = Resource(lambda: either.Right(mock_resource)) effect = resource.get() assert effect.run(None) == mock_resource mock_resource.__aenter__.assert_called_once() assert resource.resource is None
def test_resources_are_unique(self): mock_resource = asynctest.MagicMock() resource = Resource(lambda: either.Right(mock_resource)) r1, r2 = effect.sequence_async( (resource.get(), resource.get()) ).run(None) assert r1 is r2 mock_resource.__aenter__.assert_called_once()
def test_absolve(self): right = either.Right(1) left = either.Left('error') right_effect = effect.success(right) left_effect = effect.success(left) assert effect.absolve(right_effect).run(None) == 1 with pytest.raises(Exception): # todo effect.absolve(left_effect).run(None)
def test_try_modify(self): int_ref = ref.Ref(0) int_ref.try_modify(lambda _: either.Left('')).either().run(None) assert int_ref.value == 0 int_ref.try_modify(lambda _: either.Right(1)).run(None) assert int_ref.value == 1
def f(s: str) -> either.Either[str, str]: return either.Right(s)
def test_either(self): success = effect.success(1) error = effect.error('error') assert success.either().run(None) == either.Right(1) error.either().run(None) == either.Left('error')