Skip to content

Commit d19908c

Browse files
committed
Checks for alternate encoding name.
1 parent a7e928a commit d19908c

2 files changed

Lines changed: 38 additions & 28 deletions

File tree

lib/iris/tests/integration/netcdf/test_stringdata.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ def all_lazy_auxcoords():
3838
N_XDIM = 3
3939
N_CHARS_DIM = 64
4040
# TODO: remove (debug)
41-
# PERSIST_TESTFILES: str | None = "~/chararray_testfiles"
42-
PERSIST_TESTFILES: str | None = None
41+
PERSIST_TESTFILES: str | None = "~/chararray_testfiles"
42+
# PERSIST_TESTFILES: str | None = None
4343

4444
NO_ENCODING_STR = "<noencoding>"
45-
TEST_ENCODINGS = [NO_ENCODING_STR] + SUPPORTED_ENCODINGS
45+
ALIAS_UTF8_STR = "UTF8" # an alternative acceptable form (should be written as-is)
46+
TEST_ENCODINGS = [NO_ENCODING_STR, ALIAS_UTF8_STR] + SUPPORTED_ENCODINGS
4647

4748

4849
#
@@ -223,7 +224,7 @@ def readtest_path(
223224
else:
224225
filetag = encoding
225226
dimtag = "diffdims" if use_separate_dims else "samedims"
226-
tempfile_path = tmp_path / f"sample_read_{filetag}_{dimtag}.nc"
227+
tempfile_path = tmp_path / f"sample_stringdata_read_{filetag}_{dimtag}.nc"
227228
return tempfile_path
228229

229230
@pytest.fixture
@@ -408,7 +409,7 @@ def write_bytes(self, request):
408409
return request.param == "dataAsBytes"
409410

410411
@pytest.fixture
411-
def writetest_path(self, encoding, write_bytes, tmp_path):
412+
def writetest_path(self, encoding, write_bytes, lazy_data, tmp_path):
412413
"""Create a suitable test cube, with either string or byte content."""
413414
if PERSIST_TESTFILES:
414415
tmp_path = Path(PERSIST_TESTFILES).expanduser()
@@ -417,7 +418,10 @@ def writetest_path(self, encoding, write_bytes, tmp_path):
417418
else:
418419
filetag = encoding
419420
datatag = "writebytes" if write_bytes else "writestrings"
420-
tempfile_path = tmp_path / f"sample_write_{filetag}_{datatag}.nc"
421+
lazytag = "alllazy" if lazy_data else "smallreal"
422+
tempfile_path = (
423+
tmp_path / f"sample_stringdata_write_{filetag}_{datatag}_{lazytag}.nc"
424+
)
421425
return tempfile_path
422426

423427
@pytest.fixture
@@ -440,11 +444,6 @@ def writetest_data(self, writetest_path, encoding, write_bytes, lazy_data):
440444
def test_valid_encodings(self, encoding, writetest_data, write_bytes):
441445
cube_info = writetest_data
442446
cube, path = cube_info.cube, cube_info.save_path
443-
# TODO: not testing the "byte read/write" yet
444-
# Make a quick check for cube equality : but the presentation depends on the read mode
445-
# with DECODE_TO_STRINGS_ON_READ.context(not write_bytes):
446-
# read_cube = iris.load_cube(path)
447-
# assert read_cube == cube
448447

449448
# N.B. file content should not depend on whether bytes or strings were written
450449
vararray, coordarray = cube_info.datavar_data, cube_info.stringcoord_data

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
from iris.tests.stock.netcdf import ncgen_from_cdl
2929
from iris.warnings import IrisCfLoadWarning, IrisCfSaveWarning
3030

31-
encoding_options = [None] + SUPPORTED_ENCODINGS
31+
# Note: for test options, include "no encoding" and an alias name
32+
ENCODING_NONE = None
33+
ENCODING_UTF8_ALIAS = "UTF8"
34+
encoding_options = [ENCODING_NONE, ENCODING_UTF8_ALIAS] + SUPPORTED_ENCODINGS
3235

3336
samples_3_ascii = np.array(
3437
["one", "", "seven"], # N.B. include empty!
@@ -45,9 +48,17 @@ def encoding(request):
4548
return request.param
4649

4750

51+
# TODO: remove (debug)
52+
PERSIST_TESTFILES: str | None = "~/chararray_testfiles"
53+
# PERSIST_TESTFILES: str | None = None
54+
55+
4856
@pytest.fixture(scope="module")
4957
def tempdir(tmp_path_factory):
50-
path = tmp_path_factory.mktemp("netcdf")
58+
if PERSIST_TESTFILES:
59+
path = Path(PERSIST_TESTFILES).expanduser()
60+
else:
61+
path = tmp_path_factory.mktemp("netcdf")
5162
return path
5263

5364

@@ -134,7 +145,7 @@ class TestWriteStrings:
134145

135146
def test_encodings(self, encoding, tempdir):
136147
# Create a dataset with the variable
137-
path = tempdir / f"test_writestrings_encoding_{encoding!s}.nc"
148+
path = tempdir / f"test_bytecoded_writestrings_encoding_{encoding!s}.nc"
138149

139150
if encoding in [None, "ascii"]:
140151
writedata = samples_3_ascii
@@ -164,7 +175,7 @@ def test_encodings(self, encoding, tempdir):
164175

165176
def test_scalar(self, tempdir):
166177
# Like 'test_write_strings', but the variable has *only* the string dimension.
167-
path = tempdir / "test_writestrings_scalar.nc"
178+
path = tempdir / "test_bytecoded_writestrings_scalar.nc"
168179

169180
strlen = 5
170181
ds_encoded = make_encoded_dataset(path, strlen=strlen)
@@ -180,7 +191,7 @@ def test_scalar(self, tempdir):
180191

181192
def test_multidim(self, tempdir):
182193
# Like 'test_write_strings', but the variable has additional dimensions.
183-
path = tempdir / "test_writestrings_multidim.nc"
194+
path = tempdir / "test_bytecoded_writestrings_multidim.nc"
184195

185196
strlen = 5
186197
ds_encoded = make_encoded_dataset(path, strlen=strlen)
@@ -209,7 +220,7 @@ def test_multidim(self, tempdir):
209220

210221
@pytest.mark.parametrize("encoding", [None, "ascii"])
211222
def test_write_encoding_failure(self, tempdir, encoding):
212-
path = tempdir / f"test_writestrings_encoding_{encoding}_fail.nc"
223+
path = tempdir / f"test_bytecoded_writestrings_encoding_{encoding}_fail.nc"
213224
ds = make_encoded_dataset(path, strlen=5, encoding=encoding)
214225
v = ds.variables["vxs"]
215226
encoding_name = encoding
@@ -223,7 +234,7 @@ def test_write_encoding_failure(self, tempdir, encoding):
223234
v[:] = samples_3_nonascii
224235

225236
def test_write_badencoding_ignore(self, tempdir):
226-
path = tempdir / "test_writestrings_badencoding_ignore.nc"
237+
path = tempdir / "test_bytecoded_writestrings_badencoding_ignore.nc"
227238
ds = make_encoded_dataset(path, strlen=5, encoding="unknown")
228239
v = ds.variables["vxs"]
229240
msg = (
@@ -235,7 +246,7 @@ def test_write_badencoding_ignore(self, tempdir):
235246

236247
def test_overlength(self, tempdir):
237248
# Check expected behaviour with over-length data
238-
path = tempdir / "test_writestrings_overlength.nc"
249+
path = tempdir / "test_bytecoded_writestrings_overlength.nc"
239250
strlen = 6
240251
ds = make_encoded_dataset(path, strlen=strlen, encoding="utf8")
241252
v = ds.variables["vxs"]
@@ -248,7 +259,7 @@ def test_overlength(self, tempdir):
248259

249260
def test_overlength_splitcoding(self, tempdir):
250261
# Check expected behaviour when non-ascii multibyte coding gets truncated
251-
path = tempdir / "test_writestrings_overlength_splitcoding.nc"
262+
path = tempdir / "test_bytecoded_writestrings_overlength_splitcoding.nc"
252263
strlen = 5
253264
ds = make_encoded_dataset(path, strlen=strlen, encoding="utf-8")
254265
v = ds.variables["vxs"]
@@ -291,7 +302,7 @@ def test_write_chars(self, tempdir, write_form):
291302
strlen = strings_maxbytes(write_strings, encoding)
292303
write_bytes = make_bytearray(write_strings, strlen, encoding=encoding)
293304
# NOTE: 'flexi' form util decides the width needs to be 7 !!
294-
path = tempdir / f"test_writechars_{write_form}.nc"
305+
path = tempdir / f"test_bytecoded_writechars_{write_form}.nc"
295306
ds = make_encoded_dataset(path, encoding=encoding, strlen=strlen)
296307
v = ds.variables["vxs"]
297308

@@ -331,7 +342,7 @@ def undecoded_testvar(self, ds_encoded, varname: str):
331342

332343
def test_encodings(self, encoding, tempdir, readmode):
333344
# Create a dataset with the variable
334-
path = tempdir / f"test_read_encodings_{encoding!s}_{readmode}.nc"
345+
path = tempdir / f"test_bytecoded_read_encodings_{encoding!s}_{readmode}.nc"
335346

336347
if encoding in [None, "ascii"]:
337348
write_strings = samples_3_ascii
@@ -352,10 +363,10 @@ def test_encodings(self, encoding, tempdir, readmode):
352363
# Test "normal" read --> string array
353364
result = v[:]
354365
expected = write_strings
355-
if encoding in ("utf-8", "utf-16"):
366+
if encoding in ("utf-8", ENCODING_UTF8_ALIAS, "utf-16"):
356367
# In these cases, with the given non-ascii sample data, the
357368
# "default minimum string length" is overestimated.
358-
if encoding == "utf-8":
369+
if encoding in ["utf-8", ENCODING_UTF8_ALIAS]:
359370
assert strlen == 7
360371
assert result.dtype == "U7"
361372
# correct the result dtype to pass the write_strings comparison below
@@ -378,7 +389,7 @@ def test_encodings(self, encoding, tempdir, readmode):
378389

379390
def test_scalar(self, tempdir, readmode):
380391
# Like 'test_write_strings', but the variable has *only* the string dimension.
381-
path = tempdir / f"test_read_scalar_{readmode}.nc"
392+
path = tempdir / f"test_bytecoded_read_scalar_{readmode}.nc"
382393

383394
strlen = 5
384395
ds_encoded = make_encoded_dataset(path, strlen=strlen)
@@ -404,7 +415,7 @@ def test_scalar(self, tempdir, readmode):
404415

405416
def test_multidim(self, tempdir, readmode):
406417
# Like 'test_write_strings', but the variable has additional dimensions.
407-
path = tempdir / f"test_read_multidim_{readmode}.nc"
418+
path = tempdir / f"test_bytecoded_read_multidim_{readmode}.nc"
408419

409420
strlen = 5
410421
ds_encoded = make_encoded_dataset(path, strlen=strlen)
@@ -440,7 +451,7 @@ def test_multidim(self, tempdir, readmode):
440451
check_array_matching(result, expected)
441452

442453
def test_read_encoding_failure(self, tempdir, readmode):
443-
path = tempdir / f"test_read_encoding_failure_{readmode}.nc"
454+
path = tempdir / f"test_bytecoded_read_encoding_failure_{readmode}.nc"
444455
strlen = 10
445456
ds_encoded = make_encoded_dataset(path, strlen=strlen, encoding="ascii")
446457
v = ds_encoded.variables["vxs"]
@@ -463,7 +474,7 @@ def test_read_encoding_failure(self, tempdir, readmode):
463474
assert np.all(result == test_utf8_bytes)
464475

465476
def test_read_badencoding_ignore(self, tempdir):
466-
path = tempdir / f"test_read_badencoding_ignore.nc"
477+
path = tempdir / f"test_bytecoded_read_badencoding_ignore.nc"
467478
strlen = 10
468479
ds = make_encoded_dataset(path, strlen=strlen, encoding="unknown")
469480
v = ds.variables["vxs"]

0 commit comments

Comments
 (0)