Skip to content

Commit 44d3554

Browse files
Add test for autocommit
1 parent 21e56af commit 44d3554

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

s3fs/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ class S3File(AbstractBufferedFile):
900900
def __init__(self, s3, path, mode='rb', block_size=5 * 2 ** 20, acl="",
901901
version_id=None, fill_cache=True, s3_additional_kwargs=None,
902902
autocommit=True, cache_type='bytes'):
903-
bucket, key = split_path(self.path)
903+
bucket, key = split_path(path)
904904
if not key:
905905
raise ValueError('Attempt to open non key-like path: %s' % path)
906906
self.bucket = bucket

s3fs/tests/test_s3fs.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,3 +1228,35 @@ def test_cache_after_copy(s3):
12281228
assert 'test/afile' in s3.ls('s3://test', False)
12291229
s3.cp('test/afile', 'test/bfile')
12301230
assert 'test/bfile' in s3.ls('s3://test', False)
1231+
1232+
1233+
def test_autocommit(s3):
1234+
auto_file = test_bucket_name + '/auto_file'
1235+
committed_file = test_bucket_name + '/commit_file'
1236+
aborted_file = test_bucket_name + '/aborted_file'
1237+
s3 = S3FileSystem(anon=False, version_aware=True)
1238+
1239+
def write_and_flush(path, autocommit):
1240+
with s3.open(path, 'wb', autocommit=autocommit) as fo:
1241+
fo.write(b'1')
1242+
return fo
1243+
1244+
# regular behavior
1245+
fo = write_and_flush(auto_file, autocommit=True)
1246+
assert fo.autocommit
1247+
assert s3.exists(auto_file)
1248+
1249+
fo = write_and_flush(committed_file, autocommit=False)
1250+
assert not fo.autocommit
1251+
assert not s3.exists(committed_file)
1252+
fo.commit()
1253+
assert s3.exists(committed_file)
1254+
1255+
fo = write_and_flush(aborted_file,autocommit=False)
1256+
assert not s3.exists(aborted_file)
1257+
fo.discard()
1258+
assert not s3.exists(aborted_file)
1259+
# Cannot commit a file that was discarded
1260+
with pytest.raises(Exception):
1261+
fo.commit()
1262+

0 commit comments

Comments
 (0)