def __init__(self): self.clock = Mock() self.clock.sleep.return_value = effect.success(None) self.clock.now.return_value = effect.success(datetime.fromtimestamp(0)) self.random = Mock() self.random.random.return_value = effect.success(0.0) self.random.randint.return_value = effect.success(0)
def test_gather(self): assert effect.gather_async( [effect.success(v) for v in range(3)] ).run(None) == (0, 1, 2) assert effect.gather( [effect.success(v) for v in range(3)] ).run(None) == (0, 1, 2)
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_filter_generator(self): e = effect.filter_( lambda v: effect.success(v % 2 == 0), (v for v in range(5)) ) assert e.run(None) == (0, 2, 4) assert e.run(None) == (0, 2, 4)
def test_memoize(self): state = ref.Ref(()) e = state.modify(lambda t: t + ('modify was called', ) ).discard_and_then(effect.success('result')).memoize() double_e = e.discard_and_then(e) assert double_e.run(None) == 'result' assert state.value == ('modify was called', )
def test_stack_safety(self): with recursion_limit(100): effect.sequence_async([effect.success(v) for v in range(500)]).run(None) e = effect.error('') for _ in range(500): e = e.recover(lambda _: effect.error('')) e = e.recover(lambda _: effect.success('')) with recursion_limit(100): e.run(None) e = effect.success('') for _ in range(500): e = e.either() with recursion_limit(100): e.run(None)
def test_for_each_repr(self): f = lambda _: effect.success(True) assert repr(effect.for_each(f, [0])) == f'for_each({repr(f)})((0,))'
def test_filter_repr(self): f = lambda _: effect.success(True) assert repr(effect.filter_(f, [0])) == f'filter_({repr(f)})((0,))'
def test_combine_cpu_bound_repr(self): f = lambda _: _ assert ( repr(effect.combine_cpu_bound(effect.success(0))(f) ) == f'combine_cpu_bound(success(0))({repr(f)})' )
def test_sequence(self): assert effect.sequence_async([effect.success(v) for v in range(3)] ).run(None) == (0, 1, 2)
def test_equality(self, value, env): assert effect.success(value).run(env) == effect.success(value).run(env)
def test_left_identity_law(self, f, value, env): assert ( effect.success(value).and_then(f).run(env) == f(value).run(env) )
def test_memoize_repr(self): e = effect.success(0) assert repr(e.memoize()) == 'success(0).memoize()'
def test_recover_repr(self): e = effect.success(0) f = lambda _: _ assert repr(e.recover(f)) == f'success(0).recover({repr(f)})'
def test_either_repr(self): e = effect.success(0) assert repr(e.either()) == 'success(0).either()'
def test_discard_and_then_repr(self): e = effect.success('value') assert ( repr(e.discard_and_then(e) ) == 'success(\'value\').discard_and_then(success(\'value\'))' )
def test_map_repr(self): f = lambda _: _ assert ( repr(effect.success('value').map(f) ) == f'success(\'value\').map({repr(f)})' )
def test_and_then_repr(self): f = lambda _: _ assert ( repr(effect.success('value').and_then(f) ) == f'success(\'value\').and_then({repr(f)})' )
def test_success_repr(self): assert repr(effect.success('value')) == 'success(\'value\')'
def test_absolve_repr(self): assert repr(effect.absolve(effect.success(0))) == 'absolve(success(0))'
def test_identity_law(self, value, env): assert ( effect.success(value).map(identity).run(env) == effect.success(value).run(env) )
def test_ensure_repr(self): e = effect.success(0) assert repr(e.ensure(e)) == 'success(0).ensure(success(0))'
def test_right_identity_law(self, value, env): assert ( effect.success(value).and_then( effect.success ).run(env) == effect.success(value).run(env) )
def test_composition_law(self, f, g, value, env): h = compose(f, g) assert ( effect.success(value).map(h).run(env) == effect.success(value).map(g).map(f).run(env) )
def test_inequality(self, first, second, env): assume(first != second) assert effect.success(first).run(env) != effect.success(second ).run(env)
def test_combine(self): def f(a, b): return a + b assert effect.combine(effect.success('a'), effect.success('b'))(f).run(None) == 'ab'
def test_sequence_generator(self): e = effect.sequence_async(effect.success(v) for v in range(3)) assert e.run(None) == (0, 1, 2) assert e.run(None) == (0, 1, 2)
def test_lift(self): def f(a, b): return a + b assert effect.lift(f)(effect.success(2), effect.success(2)).run(None) == 4
def test_sequence_repr(self): e = effect.sequence([effect.success(0)]) assert (repr(e)) == 'sequence((success(0),))'
def test_lift_cpu_bound_repr(self): f = lambda _: _ assert ( repr(effect.lift_cpu_bound(f)(effect.success(0)) ) == f'lift_cpu_bound({repr(f)})(success(0))' )