Ejemplo n.º 1
0
def test_arbitrary_chars(fn):
    z = File(fn)

    # Avoid hitting the Windows max filename length
    chunk = 16
    for i in range(1, 128, chunk):
        key = "".join(["foo_"] +
                      [chr(i) for i in range(i, min(128, i + chunk))])
        with pytest.raises(KeyError):
            z[key]
        z[key] = b"foo"
        assert z[key] == b"foo"
        assert list(z) == [key]
        assert list(z.keys()) == [key]
        assert list(z.items()) == [(key, b"foo")]
        assert list(z.values()) == [b"foo"]

        zz = File(fn)
        assert zz[key] == b"foo"
        assert list(zz) == [key]
        assert list(zz.keys()) == [key]
        assert list(zz.items()) == [(key, b"foo")]
        assert list(zz.values()) == [b"foo"]
        del zz

        del z[key]
        with pytest.raises(KeyError):
            z[key]
Ejemplo n.º 2
0
def test_arbitrary_chars(fn):
    z = File(fn)

    # Avoid hitting the Windows max filename length
    chunk = 16
    for i in range(1, 128, chunk):
        key = ''.join(['foo_'] + [chr(i) for i in range(i, min(128, i + chunk))])
        with pytest.raises(KeyError):
            z[key]
        z[key] = b'foo'
        assert z[key] == b'foo'
        assert list(z) == [key]
        assert list(z.keys()) == [key]
        assert list(z.items()) == [(key, b'foo')]
        assert list(z.values()) == [b'foo']

        zz = File(fn)
        assert zz[key] == b'foo'
        assert list(zz) == [key]
        assert list(zz.keys()) == [key]
        assert list(zz.items()) == [(key, b'foo')]
        assert list(zz.values()) == [b'foo']
        del zz

        del z[key]
        with pytest.raises(KeyError):
            z[key]
Ejemplo n.º 3
0
def test_item_with_very_long_name_can_be_read_and_deleted_and_restored(fn):
    z = File(fn)
    long_key1 = 'a' + 'a'.join(str(i) for i in range(500))
    long_key2 = 'b' + 'a'.join(str(i) for i in range(500))
    z[long_key1] = b'key1'
    z[long_key2] = b'key2'
    assert z[long_key1] == b'key1'
    assert z[long_key2] == b'key2'
    z2 = File(fn)
    assert z2[long_key1] == b'key1'
    assert z2[long_key2] == b'key2'
    del z2[long_key1]
    z3 = File(fn)
    assert long_key1 not in z3
    assert z3[long_key2] == b'key2'
Ejemplo n.º 4
0
def test_delitem(fn):
    z = File(fn)

    z["x"] = b"123"
    assert os.path.exists(os.path.join(z.directory, "x"))
    del z["x"]
    assert not os.path.exists(os.path.join(z.directory, "x"))
Ejemplo n.º 5
0
def test_delitem(fn):
    z = File(fn)

    z['x'] = b'123'
    assert os.path.exists(os.path.join(z.directory, 'x'))
    del z['x']
    assert not os.path.exists(os.path.join(z.directory, 'x'))
Ejemplo n.º 6
0
def test_implementation(fn):
    z = File(fn)
    assert not z

    z['x'] = b'123'
    assert os.listdir(fn) == ['x']
    with open(os.path.join(fn, 'x'), 'rb') as f:
        assert f.read() == b'123'
Ejemplo n.º 7
0
def test_implementation(fn):
    z = File(fn)
    assert not z

    z["x"] = b"123"
    assert os.listdir(fn) == ["x"]
    with open(os.path.join(fn, "x"), "rb") as f:
        assert f.read() == b"123"

    assert "x" in z
Ejemplo n.º 8
0
def test_arbitrary_chars(fn):
    z = File(fn)

    # Avoid hitting the Windows max filename length
    chunk = 16
    for i in range(1, 128, chunk):
        key = ''.join(['foo_'] + [chr(i) for i in range(i, min(128, i + chunk))])
        with pytest.raises(KeyError):
            z[key]
        z[key] = b'foo'
        assert z[key] == b'foo'
        del z[key]
        with pytest.raises(KeyError):
            z[key]
Ejemplo n.º 9
0
def test_simple(fn):
    z = File(fn)
    assert isinstance(z, MutableMapping)
    assert not z

    assert list(z) == list(z.keys()) == []
    assert list(z.values()) == []
    assert list(z.items()) == []

    z['x'] = b'123'
    assert list(z) == list(z.keys()) == ['x']
    assert list(z.values()) == [b'123']
    assert list(z.items()) == [('x', b'123')]
    assert z['x'] == b'123'

    assert os.listdir(fn) == ['x']
    with open(os.path.join(fn, 'x'), 'rb') as f:
        assert f.read() == b'123'

    z['y'] = b'456'
    assert z['y'] == b'456'
Ejemplo n.º 10
0
def test_write_list_of_bytes(fn):
    z = File(fn)

    z["x"] = [b"123", b"4567"]
    assert z["x"] == b"1234567"
Ejemplo n.º 11
0
def test_missing_key(fn):
    z = File(fn)

    with pytest.raises(KeyError):
        z["x"]
Ejemplo n.º 12
0
def test_contextmanager(fn):
    with File(fn) as z:
        z["x"] = b"123"

    with open(os.path.join(fn, "x"), "rb") as f:
        assert f.read() == b"123"
Ejemplo n.º 13
0
def test_setitem_typeerror(fn):
    z = File(fn)
    with pytest.raises(TypeError):
        z["x"] = 123
Ejemplo n.º 14
0
def test_str(fn):
    z = File(fn)
    assert fn in str(z)
    assert fn in repr(z)
    assert z.mode in str(z)
    assert z.mode in repr(z)
Ejemplo n.º 15
0
def test_mapping(fn):
    """
    Test mapping interface for File().
    """
    z = File(fn)
    utils_test.check_mapping(z)
Ejemplo n.º 16
0
def test_contextmanager(fn):
    with File(fn) as z:
        z['x'] = b'123'

    with open(os.path.join(fn, 'x'), 'rb') as f:
        assert f.read() == b'123'
Ejemplo n.º 17
0
def test_write_list_of_bytes(fn):
    z = File(fn)

    z['x'] = [b'123', b'4567']
    assert z['x'] == b'1234567'