MCPcopy
hub / github.com/pydata/xarray / test_encode_decode_errors

Function test_encode_decode_errors

xarray/tests/test_accessor_str.py:2426–2457  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2424
2425
2426def test_encode_decode_errors() -> None:
2427 encodeBase = xr.DataArray(["a", "b", "a\x9d"])
2428
2429 msg = (
2430 r"'charmap' codec can't encode character '\\x9d' in position 1:"
2431 " character maps to <undefined>"
2432 )
2433 with pytest.raises(UnicodeEncodeError, match=msg):
2434 encodeBase.str.encode("cp1252")
2435
2436 f = lambda x: x.encode("cp1252", "ignore")
2437 result = encodeBase.str.encode("cp1252", "ignore")
2438 expected = xr.DataArray([f(x) for x in encodeBase.values.tolist()])
2439
2440 assert result.dtype == expected.dtype
2441 assert_equal(result, expected)
2442
2443 decodeBase = xr.DataArray([b"a", b"b", b"a\x9d"])
2444
2445 msg = (
2446 "'charmap' codec can't decode byte 0x9d in position 1:"
2447 " character maps to <undefined>"
2448 )
2449 with pytest.raises(UnicodeDecodeError, match=msg):
2450 decodeBase.str.decode("cp1252")
2451
2452 f = lambda x: x.decode("cp1252", "ignore")
2453 result = decodeBase.str.decode("cp1252", "ignore")
2454 expected = xr.DataArray([f(x) for x in decodeBase.values.tolist()])
2455
2456 assert result.dtype == expected.dtype
2457 assert_equal(result, expected)
2458
2459
2460def test_partition_whitespace(dtype) -> None:

Callers

nothing calls this directly

Calls 4

assert_equalFunction · 0.90
fFunction · 0.70
encodeMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…