Example #1
0
def test_try_raise():
    s = Success(0)
    try:
        s.raise_for_error()
        assert True
    except Exception:
        assert False, "raise_for_error should have no effect for a Success"

    failure_message = 'it failed!'
    f = Failure(failure_message)
    try:
        f.raise_for_error()
        assert False, "raise_for_error should raise an exception for a Failure"
    except tryme.FailureError as e:
        assert e.__str__() == failure_message

    failure_message = 'it failed!'
    f = Failure(RuntimeError(failure_message))
    try:
        f.raise_for_error()
        assert False, "raise_for_error should raise a RuntimeError when RuntimeError is the original argument"
    except RuntimeError as e:
        assert e.__str__() == failure_message

    failure_message = 'it failed!'
    f = Failure(failure_message)
    try:
        f.raise_for_error(exception=RuntimeError)
        assert False, "raise_for_error should raise a RuntimeError when RuntimeError is the original argument"
    except RuntimeError as e:
        assert str(e) == failure_message
Example #2
0
def test_try_to_console(capsys):
    success_message = 'It worked!'
    Success(success_message).to_console()
    stdout, stderr = capsys.readouterr()
    assert stdout == success_message + os.linesep
    assert stderr == ''

    Success(success_message).to_console(nl=False)
    stdout, stderr = capsys.readouterr()
    assert stdout == success_message

    failure_message = 'It failed!'
    Failure(failure_message).to_console()
    stdout, stderr = capsys.readouterr()
    assert stderr == failure_message + os.linesep
    assert stdout == ''

    with pytest.raises(SystemExit) as sys_exit:
        Failure(failure_message).to_console(exit_err=True)
        assert sys_exit.value.code == 1

    # test using a custom exit status
    with pytest.raises(SystemExit) as sys_exit:
        Failure(failure_message).to_console(exit_err=True, exit_status=6)
        assert sys_exit.value.code == 6
Example #3
0
def test_try_get():
    assert Success(0).get() == 0
    with pytest.raises(tryme.NoSuchElementError):
        Failure(0).get()

    assert Failure(0).get_failure() == 0
    with pytest.raises(tryme.NoSuchElementError):
        Success(0).get_failure()
Example #4
0
def test_try_map_failure():
    inc = lambda n: n + 1
    result = Success(0).map_failure(inc)
    assert isinstance(result, Success)
    assert result.get() == 0

    result = Failure(0).map_failure(inc)
    assert isinstance(result, Failure)
    assert result.get_failure() == 1
Example #5
0
def test_try_comparisons():
    s0 = Success(0)
    s1 = Success(1)
    f0 = Failure(0)
    f1 = Failure(1)

    assert s0 < s1
    assert s1 > s0
    assert s0 == Success(0)
    assert s0 > f0 and s0 > f1
    assert f0 < f1
    assert f0 == Failure(0)
    assert f1 > f0
Example #6
0
def test_retry_success_after_3_tries(stopped_clock):
    stopped_clock.set_times([0, 400, 800, 900, 950])

    success_iterator = iter([Failure('failed!'), Failure('failed!'), Success('success!')])
    
    @retry(timeout=1000)
    def success_on_3rd_attempt():
        return next(success_iterator)

    result = success_on_3rd_attempt()
    assert result.start != result.end
    assert result.count == 3
    assert result.elapsed == 900
    assert result.succeeded()

    stopped_clock.set_times([0, 100, 200, 300, 400])
    
    @retry
    def never_ready():
        return tryme.Again('not ready!')

    result = never_ready()
    assert result.start != result.end
    assert result.count == 3
    assert result.elapsed == result.end - result.start
    assert result.failed()
Example #7
0
def test_try_time_accounting():
    s = Success(0)
    assert s.start is None
    assert s.end is None
    assert s.elapsed is None
    assert s.count == 1

    s = Success(0, start=1, end=10, count=3)
    assert s.start == 1
    assert s.end == 10
    assert s.elapsed == 9
    assert s.count == 3

    with pytest.raises(tryme.InvalidTryError):
        Success(0, start=None, end=10)

    with pytest.raises(tryme.InvalidTryError):
        Success(0, start=0, end=None)
Example #8
0
def test_try_update():
    s0 = Success(0)
    assert s0.start is None
    assert s0.end is None
    assert s0.elapsed is None
    assert s0.count == 1
    assert s0.message == '0'

    s1 = s0.update(start=1, end=10, count=3, message='Succeeded!')
    assert s1.start == 1
    assert s1.end == 10
    assert s1.elapsed == 9
    assert s1.count == 3
    assert s1.message == 'Succeeded!'
    assert s0.message == '0'

    s2 = s1.update(count=5, message='It worked!')
    assert s2.start == 1
    assert s2.end == 10
    assert s2.elapsed == 9
    assert s2.count == 5
    assert s2.message == 'It worked!'
Example #9
0
def test_try_filter():
    is_even = lambda n: n % 2 == 0

    s0 = Success(2)
    assert s0.filter(is_even).succeeded()
    s1 = Success(1)
    assert s1.filter(is_even).failed()

    assert Failure(1).filter(is_even).failed()
Example #10
0
def test_try_fail_for_error(capsys):
    # no SystemExit should be raised
    assert Success('It worked!').fail_for_error() is None
    stdout, stderr = capsys.readouterr()
    assert stdout == '' and stderr == ''

    failure_message = 'It failed!'
    with pytest.raises(SystemExit):
        Failure(failure_message).fail_for_error()
    stdout, stderr = capsys.readouterr()
    assert stderr == failure_message + os.linesep
    assert stdout == ''

    with pytest.raises(SystemExit) as sys_exit:
        Failure(failure_message).fail_for_error(exit_status=6)
        assert sys_exit.value.code == 1
Example #11
0
def test_retry_logging_callback(capsys, stopped_clock):
    stopped_clock.set_times([0, 100, 200, 300, 400])
    
    def logging_callback(result):
        print("Retrying after %d seconds and %d attempts" % (result.elapsed, result.count))
    
    success_iterator = iter([Failure('failed!'), Failure('failed!'), Success('success!')])
    
    @retry(status_callback=logging_callback)
    def success_on_3rd_attempt():
        return next(success_iterator)

    success_on_3rd_attempt()
    stdout, stderr = capsys.readouterr()

    lines = stdout.strip().split('\n')
    assert len(lines) >= 3
    for l in lines:
        assert 'Retrying after' in l
Example #12
0
def test_try_bool():
    assert Success('it worked!')
    assert not (Failure('it failed!'))
Example #13
0
def test_try_message():
    assert Failure(0).message == '0'
    assert Failure(0, message="fubar").message == "fubar"

    assert Success(1).message == '1'
    assert Success(1, message="ok").message == "ok"
Example #14
0
def test_try_get_or_else():
    assert Success(0).get_or_else(1) == 0
    assert Failure(0).get_or_else(1) == 1
Example #15
0
def test_try_failed_succeeded():
    assert Success(0).succeeded()
    assert not (Success(0).failed())

    assert Failure(0).failed()
    assert not (Failure(0).succeeded())