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_codes_grib_new_from_file():
res = bindings.codes_grib_new_from_file(open(TEST_DATA))
assert isinstance(res, bindings.ffi.CData)
assert "'grib_handle *'" in repr(res)
def test_codes_get_array_errors():
grib = bindings.codes_grib_new_from_file(open(TEST_DATA))
with pytest.raises(bindings.GribInternalError) as err:
bindings.codes_get_array(grib, 'values', size=1) # too short
assert err.value.code == bindings.lib.GRIB_ARRAY_TOO_SMALL
with pytest.raises(bindings.GribInternalError) as err:
bindings.codes_get_array(grib, 'values', key_type=int) # wrong type
assert err.value.code == bindings.lib.GRIB_NOT_IMPLEMENTED
def test_codes_clone():
handle = bindings.codes_grib_new_from_file(open(TEST_DATA))
res = bindings.codes_clone(handle)
assert isinstance(res, bindings.ffi.CData)
assert "'grib_handle *'" in repr(res)
def test_codes_keys_iterator():
grib = bindings.codes_grib_new_from_file(open(TEST_DATA))
iterator = bindings.codes_keys_iterator_new(grib)
assert bindings.codes_keys_iterator_next(iterator) == 1
assert bindings.codes_keys_iterator_get_name(iterator) == 'globalDomain'
assert bindings.codes_keys_iterator_next(iterator) == 1
assert bindings.codes_keys_iterator_get_name(iterator) == 'GRIBEditionNumber'
bindings.codes_keys_iterator_delete(iterator)
iterator = bindings.codes_keys_iterator_new(grib, namespace='time')
assert bindings.codes_keys_iterator_next(iterator) == 1
assert bindings.codes_keys_iterator_get_name(iterator) == 'dataDate'
assert bindings.codes_keys_iterator_next(iterator) == 1
assert bindings.codes_keys_iterator_get_name(iterator) == 'dataTime'
def test_codes_grib_new_from_file_errors(tmpdir):
empty_grib = tmpdir.join('empty.grib')
empty_grib.ensure()
with pytest.raises(EOFError):
bindings.codes_grib_new_from_file(open(str(empty_grib)))
garbage_grib = tmpdir.join('garbage.grib')
garbage_grib.write('gargage')
with pytest.raises(EOFError):
bindings.codes_grib_new_from_file(open(str(garbage_grib)))
bad_grib = tmpdir.join('bad.grib')
bad_grib.write('GRIB')
with pytest.raises(bindings.GribInternalError):
bindings.codes_grib_new_from_file(open(str(bad_grib)))
def test_codes_get(key, expected_type, expected_value):
grib = bindings.codes_grib_new_from_file(open(TEST_DATA))
result = bindings.codes_get(grib, key)
assert isinstance(result, expected_type)
assert result == expected_value
def test_codes_get_length():
grib = bindings.codes_grib_new_from_file(open(TEST_DATA))
res = bindings.codes_get_string_length(grib, 'numberOfForecastsInEnsemble')
assert res == 1025
res = bindings.codes_get_string_length(grib, 'marsParam')
assert res == 8
def test_codes_write(tmpdir):
message_id = bindings.codes_new_from_samples('regular_ll_sfc_grib2')
grib_file = tmpdir.join('test.grib')
with open(str(grib_file), 'wb') as file:
bindings.codes_write(message_id, file)
assert grib_file.read_binary()[:4] == b'GRIB'
with open(str(grib_file)) as file:
bindings.codes_grib_new_from_file(file)
def test_codes_get_array(key, expected_value):
grib = bindings.codes_grib_new_from_file(open(TEST_DATA))
result = bindings.codes_get_array(grib, key)
assert result == expected_value