Skip to content

Commit

Permalink
[Python] Fix tests to work with latest pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
amadio committed Sep 3, 2024
1 parent b57af3b commit 4998116
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 139 deletions.
2 changes: 1 addition & 1 deletion bindings/python/tests/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
smallcopy = SERVER_URL + '/tmp/eggs'
smallbuffer = 'gre\0en\neggs\nand\nham\n'
bigfile = SERVER_URL + '/tmp/bigfile'
bigcopy = SERVER_URL + '/tmp/bigcopy'
bigcopy = SERVER_URL + '/tmp/bigcopy'
33 changes: 25 additions & 8 deletions bindings/python/tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
from env import *

def test_copy_smallfile():

f = client.File()
s, r = f.open(smallfile, OpenFlags.DELETE )
s, r = f.open(smallfile, OpenFlags.DELETE)
assert s.ok
f.write(smallbuffer)
size1 = f.stat(force=True)[1].size
f.close()
s, r = f.close()
assert s.ok

c = client.CopyProcess()
c.add_job( source=smallfile, target=smallcopy, force=True )
c.add_job(source=smallfile, target=smallcopy, force=True)
s = c.prepare()
assert s.ok
s, __ = c.run()
Expand All @@ -25,6 +25,17 @@ def test_copy_smallfile():
assert size1 == size2
f.close()

def test_create_bigfile():
f = client.File()
s, r = f.open(bigfile, OpenFlags.DELETE)
assert s.ok

for i in range(1000):
f.write(smallbuffer)
size1 = f.stat(force=True)[1].size
s, r = f.close()
assert s.ok

def test_copy_bigfile():
f = client.File()
s, r = f.open(bigfile)
Expand All @@ -33,7 +44,7 @@ def test_copy_bigfile():
f.close()

c = client.CopyProcess()
c.add_job( source=bigfile, target=bigcopy, force=True )
c.add_job(source=bigfile, target=bigcopy, force=True)
s = c.prepare()
assert s.ok
s, __ = c.run()
Expand All @@ -55,7 +66,7 @@ def test_copy_nojobs():

def test_copy_noprep():
c = client.CopyProcess()
c.add_job( source=bigfile, target=bigcopy, force=True )
c.add_job(source=bigfile, target=bigcopy, force=True)
s, __ = c.run()
assert s.ok

Expand All @@ -71,10 +82,16 @@ def end(self, jobId, status):
def update(self, jobId, processed, total):
print('+++ update(): jobid: %s, processed: %d, total: %d' % (jobId, processed, total))

def should_cancel(self, jobId):
print('+++ should_cancel(): jobid: %s' % (jobId))
return False

def test_copy_progress_handler():
c = client.CopyProcess()
c.add_job( source=bigfile, target=bigcopy, force=True )
c.prepare()
s = c.prepare()
assert s.ok

h = TestProgressHandler()
c.run(handler=h)
s, _ = c.run(handler=h)
assert s.ok
157 changes: 51 additions & 106 deletions bindings/python/tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

def test_write_sync():
f = client.File()
pytest.raises(ValueError, "f.write(smallbuffer)")
status, __ = f.open(smallfile, OpenFlags.DELETE, open_mode )
pytest.raises(ValueError, f.write, smallbuffer)
status, __ = f.open(smallfile, OpenFlags.DELETE, open_mode)
assert status.ok

# Write
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_write_async():

def test_open_close_sync():
f = client.File()
pytest.raises(ValueError, "f.stat()")
pytest.raises(ValueError, f.stat)
status, __ = f.open(smallfile, OpenFlags.READ)
assert status.ok
assert f.is_open()
Expand All @@ -63,7 +63,7 @@ def test_open_close_sync():
status, __ = f.close()
assert status.ok
assert f.is_open() == False
pytest.raises(ValueError, "f.stat()")
pytest.raises(ValueError, f.stat)
f.close()
f.close()

Expand All @@ -86,64 +86,64 @@ def test_open_close_async():

def test_io_limits():
f = client.File()
pytest.raises(ValueError, 'f.read()')
pytest.raises(ValueError, f.read)
status, __ = f.open(smallfile, OpenFlags.UPDATE)
assert status.ok
status, __ = f.stat()
assert status.ok

# Test read limits
pytest.raises(TypeError, 'f.read(0, [1, 2])')
pytest.raises(TypeError, 'f.read([1, 2], 0)')
pytest.raises(TypeError, 'f.read(0, 10, [0, 1, 2])')
pytest.raises(OverflowError, 'f.read(0, -10)')
pytest.raises(OverflowError, 'f.read(-1, 1)')
pytest.raises(OverflowError, 'f.read(0, 1, -1)')
pytest.raises(OverflowError, 'f.read(0, 10**11)')
pytest.raises(OverflowError, 'f.read(0, 10, 10**6)')
pytest.raises(TypeError, f.read, 0, [1, 2])
pytest.raises(TypeError, f.read, [1, 2], 0)
pytest.raises(TypeError, f.read, 0, 10, [0, 1, 2])
pytest.raises(OverflowError, f.read, 0, -10)
pytest.raises(OverflowError, f.read, -1, 1)
pytest.raises(OverflowError, f.read, 0, 1, -1)
pytest.raises(OverflowError, f.read, 0, 10**11)
pytest.raises(OverflowError, f.read, 0, 10, 10**6)

# Test readline limits
pytest.raises(TypeError, 'f.readline([0, 1], 1)')
pytest.raises(TypeError, 'f.readline(0, [0, 1])')
pytest.raises(TypeError, 'f.readline(0, 10, [0, 1])')
pytest.raises(OverflowError, 'f.readline(-1, 1)')
pytest.raises(OverflowError, 'f.readline(0, -1)')
pytest.raises(OverflowError, 'f.readline(0, 1, -1)')
pytest.raises(OverflowError, 'f.readline(0, 10**11)')
pytest.raises(OverflowError, 'f.readline(0, 10, 10**11)')
pytest.raises(TypeError, f.readline, [0, 1], 1)
pytest.raises(TypeError, f.readline, 0, [0, 1])
pytest.raises(TypeError, f.readline, 0, 10, [0, 1])
pytest.raises(OverflowError, f.readline, -1, 1)
pytest.raises(OverflowError, f.readline, 0, -1)
pytest.raises(OverflowError, f.readline, 0, 1, -1)
pytest.raises(OverflowError, f.readline, 0, 10**11)
pytest.raises(OverflowError, f.readline, 0, 10, 10**11)

# Test write limits
data = "data that will never get written"
pytest.raises(TypeError, 'f.write(data, 0, [1, 2])')
pytest.raises(TypeError, 'f.write(data, [1, 2], 0)')
pytest.raises(TypeError, 'f.write(data, 0, 10, [0, 1, 2])')
pytest.raises(OverflowError, 'f.write(data, 0, -10)')
pytest.raises(OverflowError, 'f.write(data, -1, 1)')
pytest.raises(OverflowError, 'f.write(data, 0, 1, -1)')
pytest.raises(OverflowError, 'f.write(data, 0, 10**11)')
pytest.raises(OverflowError, 'f.write(data, 0, 10, 10**6)')
pytest.raises(TypeError, f.write, data, 0, [1, 2])
pytest.raises(TypeError, f.write, data, [1, 2], 0)
pytest.raises(TypeError, f.write, data, 0, 10, [0, 1, 2])
pytest.raises(OverflowError, f.write, data, 0, -10)
pytest.raises(OverflowError, f.write, data, -1, 1)
pytest.raises(OverflowError, f.write, data, 0, 1, -1)
pytest.raises(OverflowError, f.write, data, 0, 10**11)
pytest.raises(OverflowError, f.write, data, 0, 10, 10**6)

# Test vector_read limits
pytest.raises(TypeError, 'f.vector_read(chunks=100)')
pytest.raises(TypeError, 'f.vector_read(chunks=[1,2,3])')
pytest.raises(TypeError, 'f.vector_read(chunks=[("lol", "cakes")])')
pytest.raises(TypeError, 'f.vector_read(chunks=[(1), (2)])')
pytest.raises(TypeError, 'f.vector_read(chunks=[(1, 2), (3)])')
pytest.raises(OverflowError, 'f.vector_read(chunks=[(-1, -100), (-100, -100)])')
pytest.raises(OverflowError, 'f.vector_read(chunks=[(0, 10**10*10)])')
pytest.raises(TypeError, f.vector_read, chunks=100)
pytest.raises(TypeError, f.vector_read, chunks=[1,2,3])
pytest.raises(TypeError, f.vector_read, chunks=[("lol", "cakes")])
pytest.raises(TypeError, f.vector_read, chunks=[(1), (2)])
pytest.raises(TypeError, f.vector_read, chunks=[(1, 2), (3)])
pytest.raises(OverflowError, f.vector_read, chunks=[(-1, -100), (-100, -100)])
pytest.raises(OverflowError, f.vector_read, chunks=[(0, 10**10*10)])

# Test truncate limits
pytest.raises(TypeError, 'f.truncate(0, [1, 2])')
pytest.raises(TypeError, 'f.truncate([1, 2], 0)')
pytest.raises(OverflowError, 'f.truncate(-1)')
pytest.raises(OverflowError, 'f.truncate(100, -10)')
pytest.raises(OverflowError, 'f.truncate(0, 10**6)')
pytest.raises(TypeError, f.truncate, 0, [1, 2])
pytest.raises(TypeError, f.truncate, [1, 2], 0)
pytest.raises(OverflowError, f.truncate, -1)
pytest.raises(OverflowError, f.truncate, 100, -10)
pytest.raises(OverflowError, f.truncate, 0, 10**6)
status, __ = f.close()
assert status.ok

def test_write_big_async():
f = client.File()
pytest.raises(ValueError, 'f.read()')
pytest.raises(ValueError, f.read)
status, __ = f.open(bigfile, OpenFlags.DELETE, open_mode)
assert status.ok

Expand All @@ -170,7 +170,7 @@ def test_write_big_async():

def test_read_sync():
f = client.File()
pytest.raises(ValueError, 'f.read()')
pytest.raises(ValueError, f.read)
status, response = f.open(bigfile, OpenFlags.READ)
assert status.ok
status, response = f.stat()
Expand All @@ -196,32 +196,15 @@ def test_read_async():
assert len(response) == size
f.close()

def test_iter_small():
def test_iteration():
f = client.File()
status, __ = f.open(smallfile, OpenFlags.DELETE)
assert status.ok
status, __ = f.write(smallbuffer)
assert status.ok

size = f.stat(force=True)[1].size
pylines = open('/tmp/spam').readlines()
total = 0

for i, line in enumerate(f):
total += len(line)
if pylines[i].endswith('\n'):
assert line.endswith('\n')

assert total == size
f.close()

def test_iter_big():
f = client.File()
status, __ = f.open(bigfile, OpenFlags.READ)
assert status.ok

size = f.stat()[1].size
pylines = open('/tmp/bigfile').readlines()
pylines = smallbuffer.split('\n')
total = 0

for i, line in enumerate(f):
Expand Down Expand Up @@ -257,55 +240,17 @@ def test_readline():
assert response == 'ham'
f.close()

def test_readlines_small():
def test_readchunks_small():
f = client.File()
f.open(smallfile, OpenFlags.DELETE, open_mode)
f.open(smallfile, OpenFlags.DELETE)
f.write(smallbuffer)
f.close()
pylines = open('/tmp/spam').readlines()

for i in range(1, 100):
f = client.File()
f.open(smallfile)
response = f.readlines(offset=0, chunksize=i)
assert len(response) == 4
for j, line in enumerate(response):
if pylines[j].endswith('\n'):
assert line.endswith('\n')
f.close()

def test_readlines_big():
f = client.File()
f.open(bigfile, OpenFlags.READ)
size = f.stat()[1].size

lines = f.readlines()
pylines = open('/tmp/bigfile').readlines()
assert len(lines) == len(pylines)

nlines = len(pylines)

total = 0
for i, l in enumerate(lines):
total += len(l)
if l != pylines[i]:
print('!!!!!', total, i)
print('+++++ py: %r' % pylines[i])
print('+++++ me: %r' % l)
break
if pylines[i].endswith('\n'):
assert l.endswith('\n')

assert total == size
f.close()

def test_readchunks_small():
f = client.File()
f.open(smallfile, OpenFlags.READ)
size = f.stat()[1].size

total = 0
chunks = ['gre', '\0en', '\neg', 'gs\n', 'and', '\nha', 'm\n']
chunks = [b'gre', b'\0en', b'\neg', b'gs\n', b'and', b'\nha', b'm\n']
for i, chunk in enumerate(f.readchunks(chunksize=3)):
assert chunk == chunks[i]
total += len(chunk)
Expand Down Expand Up @@ -372,7 +317,7 @@ def test_vector_read_async():

def test_stat_sync():
f = client.File()
pytest.raises(ValueError, 'f.stat()')
pytest.raises(ValueError, f.stat)
status, __ = f.open(bigfile)
assert status.ok
status, __ = f.stat()
Expand All @@ -393,7 +338,7 @@ def test_stat_async():

def test_sync_sync():
f = client.File()
pytest.raises(ValueError, 'f.sync()')
pytest.raises(ValueError, f.sync)
status, __ = f.open(bigfile)
assert status.ok
status, __ = f.sync()
Expand All @@ -413,7 +358,7 @@ def test_sync_async():

def test_truncate_sync():
f = client.File()
pytest.raises(ValueError, 'f.truncate(10000)')
pytest.raises(ValueError, f.truncate, 10000)
status, __ = f.open(smallfile, OpenFlags.DELETE)
assert status.ok

Expand Down
13 changes: 4 additions & 9 deletions bindings/python/tests/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ def test_mkdir_flags():

def test_args():
c = client.FileSystem(url=SERVER_URL)
assert c
assert c is not None

pytest.raises(TypeError, "c = client.FileSystem(foo='root://localhost')")
pytest.raises(TypeError, "c = client.FileSystem(path='root://localhost', foo='bar')")
pytest.raises(TypeError, client.FileSystem, foo='root://localhost')
pytest.raises(TypeError, client.FileSystem, path='root://localhost', foo='bar')

def test_creation():
c = client.FileSystem(SERVER_URL)
Expand All @@ -193,9 +193,4 @@ def test_creation():
def test_deletion():
c = client.FileSystem(SERVER_URL)
del c

if sys.hexversion > 0x03000000:
pytest.raises(UnboundLocalError, 'assert c')
else:
pytest.raises(NameError, 'assert c')

pytest.raises(NameError, eval, "c")
2 changes: 1 addition & 1 deletion bindings/python/tests/test_glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_glob_local(tmptree):

def test_glob_remote(tmptree):
assert len(glob.glob("root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoad/")) == 0
assert len(glob.glob("root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoa*")) == 1
assert len(glob.glob("root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoa*")) == 2
assert len(glob.glob("root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoaod/*")) > 0
assert len(glob.glob("root://eospublic.cern.ch//eos/root-*/cms_opendata_2012_nanoaod/*")) > 0

Expand Down
Loading

0 comments on commit 4998116

Please sign in to comment.