async def test_exception():
    m = {}
    t = automock(T, memory=m, locked=False)(1)
    a = None
    try:
        t.some_exception_sync()
    except TException as e:
        a = e

    t = automock(T, memory=m, locked=True)(1)
    b = None
    try:
        t.some_exception_sync()
    except TException as e:
        b = e

    assert a.time == b.time

    m = {}
    t = automock(T, memory=m, locked=False)(1)
    a = None
    try:
        await t.some_exception_async()
    except TException as e:
        a = e

    t = automock(T, memory=m, locked=True)(1)
    b = None
    try:
        await t.some_exception_async()
    except TException as e:
        b = e

    assert a.time == b.time
Ejemplo n.º 2
0
def test_missed_key():
    m = {}
    t = automock(T, memory=m, locked=False)(1)
    t.some_sync(1)

    t = automock(T, memory=m, locked=True)(1)
    t.some_sync(1)
    with pytest.raises(RuntimeError):
        t.some_sync(1)
Ejemplo n.º 3
0
def test_broken_memory():
    m = {}
    t = automock(T, memory=m, locked=False)(1)
    t.some_sync(1)

    m[0, 1].type = "foo"

    t = automock(T, memory=m, locked=True)(1)
    with pytest.raises(ValueError):
        t.some_sync(1)
def test_out_of_sequence():
    m = {}
    t = automock(T, memory=m, locked=False)(1)
    assert t.some_sync(1) == 2
    assert t.some_sync(2) == 3
    assert len(m) == 3

    with pytest.raises(RuntimeError):
        t = automock(None, memory=m, locked=True)(2)

    t = automock(None, memory=m, locked=True)(1)
    with pytest.raises(RuntimeError):
        t.some_sync(2)
async def test_simple():
    m = {}
    t = automock(T, memory=m, locked=False)(1)
    assert t.some_sync(1) == 2
    assert await t.some_async(2) == 3
    assert await t.some_coro(3) == 4
    assert t.some_sync(1) == 2
    assert t.some_sync(4) == 5
    assert len(m) == 6

    t = automock(None, memory=m, locked=True)(1)
    assert t.some_sync(1) == 2
    assert await t.some_async(2) == 3
    assert await t.some_coro(3) == 4
    assert t.some_sync(1) == 2
    assert t.some_sync(4) == 5
    assert len(m) == 6
Ejemplo n.º 6
0
def test_multiple_instance():
    m = {}
    t = automock(T, memory=m, locked=False)
    t11 = t(1)
    t12 = t(2)
    r11 = t11.side_sync()
    r12 = t12.side_sync()
    assert r11 != r12

    t = automock(T, memory=m, locked=True)
    t21 = t(1)
    t22 = t(2)
    r22 = t22.side_sync()
    r21 = t21.side_sync()
    assert r21 != r22

    assert r11 == r21
    assert r12 == r22
Ejemplo n.º 7
0
def test_debug():
    called = False

    def debug(m, c1, c2):
        nonlocal called
        called = True
        assert isinstance(m, dict)
        assert len(m) == 2
        assert c1.args == (2,)
        assert c2.args == (1,)

    m = {}
    t = automock(T, memory=m, locked=False)(1)
    t.some_sync(1)

    t = automock(T, memory=m, locked=True, debug=debug)(1)
    with pytest.raises(RuntimeError):
        t.some_sync(2)
    assert called
async def test_function_mock():
    m = {}
    f = automock(T.some_static_sync, memory=m, locked=False)
    assert f(1, 2) == 3
    assert f(1, 2) == 3
    assert f(1, 3) == 4
    assert len(m) == 4

    f = automock(T.some_static_sync, memory=m, locked=True)
    assert f(1, 2) == 3
    assert f(1, 2) == 3
    assert f(1, 3) == 4
    assert len(m) == 4

    m = {}
    f = automock(T.some_static_async, memory=m, locked=False)
    assert await f(1, 2) == 3
    assert await f(1, 2) == 3
    assert await f(1, 3) == 4
    assert len(m) == 4
def test_lock():
    m = {}
    with pytest.raises(RuntimeError):
        automock(T, memory=m, locked=True)(1)
Ejemplo n.º 10
0
def test_bad_attribute():
    m = {}
    t = automock(T, memory=m, locked=False)(1)
    with pytest.raises(ValueError):
        t.some_property()