Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_FileStream():
res = messages.FileStream(TEST_DATA)
leader = res.first()
assert len(leader) > 100
assert sum(1 for _ in res) == leader['count']
assert len(res.index(['paramId'])) == 1
# __file__ is not a GRIB, but contains the "GRIB" string, so it is a very tricky corner case
res = messages.FileStream(str(__file__))
with pytest.raises(EOFError):
res.first()
res = messages.FileStream(str(__file__), errors='ignore')
with pytest.raises(EOFError):
res.first()
def test_FileStream():
res = messages.FileStream(TEST_DATA)
leader = res.first()
assert len(leader) > 100
assert sum(1 for _ in res) == leader['count']
assert len(res.index(['paramId'])) == 1
# __file__ is not a GRIB, but contains the "GRIB" string, so it is a very tricky corner case
res = messages.FileStream(str(__file__))
with pytest.raises(EOFError):
res.first()
res = messages.FileStream(str(__file__), errors='ignore')
with pytest.raises(EOFError):
res.first()
def test_FileIndex():
res = messages.FileIndex.from_filestream(messages.FileStream(TEST_DATA), ['paramId'])
assert res['paramId'] == [129, 130]
assert len(res) == 1
assert list(res) == ['paramId']
assert res.first()
with pytest.raises(ValueError):
res.getone('paramId')
with pytest.raises(KeyError):
res['non-existent-key']
subres = res.subindex(paramId=130)
assert subres.get('paramId') == [130]
assert subres.getone('paramId') == 130
assert len(subres) == 1
def test_FileIndex_from_indexpath_or_filestream(tmpdir):
grib_file = tmpdir.join('file.grib')
with open(TEST_DATA, 'rb') as file:
grib_file.write_binary(file.read())
# create index file
res = messages.FileIndex.from_indexpath_or_filestream(
messages.FileStream(str(grib_file)), ['paramId']
)
assert isinstance(res, messages.FileIndex)
# read index file
res = messages.FileIndex.from_indexpath_or_filestream(
messages.FileStream(str(grib_file)), ['paramId']
)
assert isinstance(res, messages.FileIndex)
# do not read nor create the index file
res = messages.FileIndex.from_indexpath_or_filestream(
messages.FileStream(str(grib_file)), ['paramId'], indexpath=''
)
assert isinstance(res, messages.FileIndex)
# can't create nor read index file
def test_FileIndex_errors():
class MyMessage(messages.ComputedKeysMessage):
computed_keys = {'error_key': lambda m: 1 / 0}
stream = messages.FileStream(TEST_DATA, message_class=MyMessage)
res = messages.FileIndex.from_filestream(stream, ['paramId', 'error_key'])
assert res['paramId'] == [129, 130]
assert len(res) == 2
assert list(res) == ['paramId', 'error_key']
assert res['error_key'] == ['undef']