Ejemplo n.º 1
0
def test_autocommit(oss):
    auto_file = test_bucket_name + "/auto_file"
    committed_file = test_bucket_name + "/commit_file"
    aborted_file = test_bucket_name + "/aborted_file"
    oss = OSSFileSystem(key=key, secret=secret, endpoint=endpoint, version_aware=True)

    def write_and_flush(path, autocommit):
        with oss.open(path, "wb", autocommit=autocommit) as fo:
            fo.write(b"1")
        return fo

    # regular behavior
    fo = write_and_flush(auto_file, autocommit=True)
    assert fo.autocommit
    assert oss.exists(auto_file)

    fo = write_and_flush(committed_file, autocommit=False)
    assert not fo.autocommit
    assert not oss.exists(committed_file)
    fo.commit()
    assert oss.exists(committed_file)

    fo = write_and_flush(aborted_file, autocommit=False)
    assert not oss.exists(aborted_file)
    fo.discard()
    assert not oss.exists(aborted_file)
    # Cannot commit a file that was discarded
    with pytest.raises(Exception):
        fo.commit()
Ejemplo n.º 2
0
def test_exists_versioned(oss, version_aware):
    """Test to ensure that a prefix exists when using a versioned bucket"""
    n = 2
    oss = OSSFileSystem(
        key=key,
        secret=secret,
        endpoint=endpoint,
        version_aware=version_aware,
    )
    segments = [versioned_bucket_name] + [str(uuid.uuid4()) for _ in range(n)]
    path = "/".join(segments)
    for i in range(2, n + 1):
        assert not oss.exists("/".join(segments[:i]))
    oss.touch(path)
    for i in range(2, n + 1):
        assert oss.exists("/".join(segments[:i]))