Example #1
0
def test_prefetch():
    with tmp_chest(on_miss=raise_KeyError) as c:
        c[1] = 1
        c[2] = 2
        c[3] = 3
        assert not raises(KeyError, lambda: c[1])
        c.flush()
        assert raises(KeyError, lambda: c[1])
        c.prefetch(1)
        assert not raises(KeyError, lambda: c[1])
        c.prefetch([1, 2])
        assert not raises(KeyError, lambda: c[2])
Example #2
0
def test_prefetch():
    with tmp_chest(on_miss=raise_KeyError) as c:
        c[1] = 1
        c[2] = 2
        c[3] = 3
        assert not raises(KeyError, lambda: c[1])
        c.flush()
        assert raises(KeyError, lambda: c[1])
        c.prefetch(1)
        assert not raises(KeyError, lambda: c[1])
        c.prefetch([1, 2])
        assert not raises(KeyError, lambda: c[2])
Example #3
0
def test_undumpable_values_stay_in_memory():
    class A(object):
        def __getstate__(self):
            raise TypeError()

    with tmp_chest(available_memory=100) as c:
        a = A()
        fn = 'tmp'
        with open(fn, 'w') as f:
            assert raises(TypeError, lambda: c.dump(a, f))
        os.remove(fn)

        c['a'] = a

        # Add enough data to try to flush out a
        for i in range(20):
            c[i] = i

        assert 'a' in c.inmem
        assert not os.path.exists(c.key_to_filename('a'))
Example #4
0
def test_undumpable_values_stay_in_memory():
    class A(object):
        def __getstate__(self):
            raise TypeError()

    with tmp_chest(available_memory=100) as c:
        a = A()
        fn = 'tmp'
        with open(fn, 'w') as f:
            assert raises(TypeError, lambda: c.dump(a, f))
        os.remove(fn)

        c['a'] = a

        # Add enough data to try to flush out a
        for i in range(20):
            c[i] = i

        assert 'a' in c.inmem
        assert not os.path.exists(c.key_to_filename('a'))
Example #5
0
def test_errors():
    with tmp_chest() as c:
        assert raises(KeyError, lambda: c[1])
        assert raises(KeyError, lambda: c[(1, 2)])
Example #6
0
def test_errors():
    with tmp_chest() as c:
        assert raises(KeyError, lambda: c[1])
        assert raises(KeyError, lambda: c[(1, 2)])
Example #7
0
def test_raises():
    assert raises(KeyError, lambda: {}[1])
    assert not raises(KeyError, lambda: {1: 2}[1])