Пример #1
0
def test_decorator():
    """ Calling the lazy object will call function's return value """
    fn = mock.Mock()
    lazy_fn = lazy(fn)
    val = lazy_fn()
    assert fn.called == False, "Should not be called before value is accessed"
    assert isinstance(val, Lazy), "Return value should be a Lazy instance"
Пример #2
0
def test_lazy_class():
    fn = mock.Mock()
    fn.return_value = 1  # int
    lazy_fn = lazy(fn)
    lazy_obj = lazy_fn()
    assert isinstance(lazy_obj, int)
    fn.return_value = 'str'  # str
    assert isinstance(lazy_obj, str)
Пример #3
0
def test_caching_vs_noncaching():
    """ Testing use of different Lazy classes """
    fn1 = mock.Mock()
    fn2 = mock.Mock()
    lazy_fn = lazy(fn1)
    lazy_caching_fn = lazy(fn2, CachingLazy)
    val1 = lazy_fn()
    val1._eval()
    val1._eval()
    val1._eval()
    val2 = lazy_caching_fn()
    val2._eval()
    val2._eval()
    val2._eval()
    assert fn1.call_count == 3, "Expected 3 calls, got %s" % fn1.call_count
    assert fn2.call_count == 1, "Expected one call, got %s" % fn2.call_count
    assert isinstance(val1, Lazy), "Expected Lazy instance"
    assert isinstance(val2, CachingLazy), "Expected CachingLazy instance"
Пример #4
0
def test_methods():
    fn = mock.Mock()
    fn.return_value = 'foobar'
    lazy_fn = lazy(fn)
    lazy_obj = lazy_fn()
    assert lazy_obj.replace('bar', 'foo') == 'foofoo'
Пример #5
0
def test_subscript():
    fn = mock.Mock()
    fn.return_value = 'foo'
    lazy_fn = lazy(fn)
    lazy_obj = lazy_fn()
    assert lazy_obj[:2] == 'fo'
Пример #6
0
def test_decorator_with_kwargs():
    fn = mock.Mock()
    fn.return_value = 'foo'
    lazy_fn = lazy(fn)
    lazy_fn(1, 2, foo=3)._eval()
    fn.assert_called_once_with(1, 2, foo=3)
Пример #7
0
def test_call():
    """ Calling the lazy object """
    fn, lazy = get_lazy()
    lazy()
    fn.return_value.assert_called_once()