Skip to content

Commit 10831d7

Browse files
committed
Fix overlength error tests.
1 parent c4a31a4 commit 10831d7

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/iris/fileformats/netcdf/_bytecoding_datasets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ def encode_stringarray_as_bytearray(
146146
from iris.exceptions import TranslationError
147147

148148
msg = (
149-
f"Non-ascii string {string!r} written to netcdf exceeds string "
150-
f"dimension : {n_bytes} > {string_dimension_length}."
149+
f"String {string!r} written to netcdf exceeds string dimension after "
150+
f"encoding : {n_bytes} > {string_dimension_length}."
151151
)
152152
raise TranslationError(msg)
153153

lib/iris/tests/unit/fileformats/netcdf/test_bytecoding_datasets.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
import pytest
1111

12+
from iris.exceptions import TranslationError
1213
from iris.fileformats.netcdf._bytecoding_datasets import (
1314
DECODE_TO_STRINGS_ON_READ,
1415
EncodedDataset,
@@ -224,17 +225,28 @@ def test_overlength(self, tempdir):
224225
strlen = 5
225226
ds = make_encoded_dataset(path, strlen=strlen, encoding="ascii")
226227
v = ds.variables["vxs"]
227-
v[:] = ["1", "123456789", "two"]
228-
expected_bytes = make_bytearray(["1", "12345", "two"], strlen)
229-
check_raw_content(path, "vxs", expected_bytes)
228+
msg = r"String .* written to netcdf exceeds string dimension .* : [0-9]* > 5\."
229+
with pytest.raises(TranslationError, match=msg):
230+
v[:] = ["1", "123456789", "two"]
230231

231232
def test_overlength_splitcoding(self, tempdir):
232233
# Check expected behaviour when non-ascii multibyte coding gets truncated
233234
path = tempdir / "test_writestrings_overlength_splitcoding.nc"
234235
strlen = 5
235236
ds = make_encoded_dataset(path, strlen=strlen, encoding="utf-8")
236237
v = ds.variables["vxs"]
237-
v[:] = ["1", "1234ü", "two"]
238+
# Note: we must do the assignment as a single byte array, to avoid hitting the
239+
# safety check for this exact problem : see previous check.
240+
byte_arrays = [
241+
string.encode("utf-8")[:strlen] for string in ("1", "1234ü", "two")
242+
]
243+
nd_bytes_array = np.array(
244+
[
245+
[bytes[i : i + 1] if i < len(bytes) else b"\0" for i in range(strlen)]
246+
for bytes in byte_arrays
247+
]
248+
)
249+
v[:] = nd_bytes_array
238250
# This creates a problem: it won't read back
239251
msg = (
240252
"Character data in variable 'vxs' could not be decoded "

0 commit comments

Comments
 (0)