|
4 | 4 |
|
5 | 5 | import numpy as np |
6 | 6 | import pytest |
| 7 | +from numpy.testing import assert_array_equal |
7 | 8 |
|
8 | 9 | import zarr |
9 | 10 | from tests.conftest import Expect, ExpectFail |
| 11 | +from zarr.codecs import BytesCodec, TransposeCodec |
10 | 12 | from zarr.codecs.cast_value import CastValue |
| 13 | +from zarr.storage import MemoryStore |
11 | 14 |
|
12 | 15 | try: |
13 | 16 | import cast_value_rs # noqa: F401 |
@@ -477,3 +480,134 @@ def test_parse_scalar_map(case: Expect[Any, Any]) -> None: |
477 | 480 | from zarr.codecs.cast_value import parse_scalar_map |
478 | 481 |
|
479 | 482 | assert parse_scalar_map(case.input) == case.output |
| 483 | + |
| 484 | + |
| 485 | +# --------------------------------------------------------------------------- |
| 486 | +# Backend version guard |
| 487 | +# --------------------------------------------------------------------------- |
| 488 | + |
| 489 | + |
| 490 | +@pytest.mark.parametrize( |
| 491 | + "case", |
| 492 | + [ |
| 493 | + Expect(input="0.4.2", output=None, id="exactly-minimum"), |
| 494 | + Expect(input="0.4.3", output=None, id="newer-patch"), |
| 495 | + Expect(input="0.5.0", output=None, id="newer-minor"), |
| 496 | + Expect(input="1.0.0", output=None, id="newer-major"), |
| 497 | + Expect(input="0.4.2.post1", output=None, id="post-release"), |
| 498 | + Expect(input="0.4.0", output="0.4.0", id="known-corrupting"), |
| 499 | + Expect(input="0.4.1", output="0.4.1", id="one-patch-below"), |
| 500 | + Expect(input="0.3.0", output="0.3.0", id="older-minor"), |
| 501 | + Expect(input="0.4.2.dev1", output="0.4.2.dev1", id="pre-release-of-minimum"), |
| 502 | + ], |
| 503 | + ids=lambda c: c.id, |
| 504 | +) |
| 505 | +def test_check_backend_version( |
| 506 | + case: Expect[str, str | None], monkeypatch: pytest.MonkeyPatch |
| 507 | +) -> None: |
| 508 | + """Versions at or above the minimum pass; older ones report the installed version.""" |
| 509 | + from zarr.codecs import cast_value as mod |
| 510 | + |
| 511 | + monkeypatch.setattr(mod, "version", lambda _: case.input) |
| 512 | + result = mod._check_backend_version() |
| 513 | + |
| 514 | + if case.output is None: |
| 515 | + assert result is None |
| 516 | + else: |
| 517 | + assert result is not None |
| 518 | + assert case.output in result |
| 519 | + assert mod.CAST_VALUE_RS_MIN_VERSION in result |
| 520 | + |
| 521 | + |
| 522 | +def test_check_backend_version_allows_missing_metadata(monkeypatch: pytest.MonkeyPatch) -> None: |
| 523 | + """A backend without distribution metadata is allowed: there is no version to compare.""" |
| 524 | + from importlib.metadata import PackageNotFoundError |
| 525 | + |
| 526 | + from zarr.codecs import cast_value as mod |
| 527 | + |
| 528 | + def raise_not_found(_: str) -> str: |
| 529 | + raise PackageNotFoundError |
| 530 | + |
| 531 | + monkeypatch.setattr(mod, "version", raise_not_found) |
| 532 | + assert mod._check_backend_version() is None |
| 533 | + |
| 534 | + |
| 535 | +def test_encode_rejects_outdated_backend(monkeypatch: pytest.MonkeyPatch) -> None: |
| 536 | + """Using the codec with an outdated backend raises instead of corrupting data.""" |
| 537 | + from zarr.codecs import cast_value as mod |
| 538 | + |
| 539 | + monkeypatch.setattr(mod, "_BACKEND_ERROR", "outdated backend") |
| 540 | + codec = CastValue(data_type="uint16") |
| 541 | + |
| 542 | + with pytest.raises(ImportError, match="outdated backend"): |
| 543 | + codec._do_cast( |
| 544 | + np.arange(4, dtype=np.float32), target_dtype=np.dtype("uint16"), scalar_map=None |
| 545 | + ) |
| 546 | + |
| 547 | + |
| 548 | +def test_min_version_matches_pyproject() -> None: |
| 549 | + """The runtime floor and the packaging floor must not drift apart.""" |
| 550 | + import re |
| 551 | + import tomllib |
| 552 | + from pathlib import Path |
| 553 | + |
| 554 | + from zarr.codecs.cast_value import CAST_VALUE_RS_MIN_VERSION |
| 555 | + |
| 556 | + pyproject = Path(__file__).parents[2] / "pyproject.toml" |
| 557 | + if not pyproject.is_file(): |
| 558 | + pytest.skip("pyproject.toml is not available in an installed checkout") |
| 559 | + |
| 560 | + with pyproject.open("rb") as f: |
| 561 | + extras = tomllib.load(f)["project"]["optional-dependencies"] |
| 562 | + |
| 563 | + (requirement,) = extras["cast-value-rs"] |
| 564 | + match = re.fullmatch(r"cast-value-rs>=(?P<version>[\w.]+)", requirement) |
| 565 | + assert match is not None, f"unexpected requirement form: {requirement!r}" |
| 566 | + assert match.group("version") == CAST_VALUE_RS_MIN_VERSION |
| 567 | + |
| 568 | + |
| 569 | +# --------------------------------------------------------------------------- |
| 570 | +# Non-contiguous input (regression for #4237) |
| 571 | +# --------------------------------------------------------------------------- |
| 572 | + |
| 573 | + |
| 574 | +@requires_cast_value_rs |
| 575 | +def test_enforce_contiguous_arrays() -> None: |
| 576 | + """ |
| 577 | + Transpose codec produces non-contiguous arrays. |
| 578 | + Ensure cast_value makes them contiguous before processing. |
| 579 | + """ |
| 580 | + data = np.arange(20, dtype=np.float32).reshape(5, 2, 2) |
| 581 | + |
| 582 | + def make_array(filters: list[Any]) -> Any: |
| 583 | + return zarr.create_array( |
| 584 | + store=MemoryStore(), |
| 585 | + shape=data.shape, |
| 586 | + dtype=data.dtype, |
| 587 | + chunks=data.shape, |
| 588 | + filters=filters, |
| 589 | + serializer=BytesCodec(endian="little"), |
| 590 | + compressors=None, |
| 591 | + zarr_format=3, |
| 592 | + ) |
| 593 | + |
| 594 | + # Cast before transpose |
| 595 | + array = make_array( |
| 596 | + [ |
| 597 | + CastValue(data_type="uint16"), |
| 598 | + TransposeCodec(order=(1, 2, 0)), |
| 599 | + ] |
| 600 | + ) |
| 601 | + array[:] = data |
| 602 | + assert_array_equal(array[:], data) |
| 603 | + |
| 604 | + # Cast after transpose |
| 605 | + array = make_array( |
| 606 | + [ |
| 607 | + TransposeCodec(order=(1, 2, 0)), |
| 608 | + CastValue(data_type="uint16"), |
| 609 | + ] |
| 610 | + ) |
| 611 | + |
| 612 | + array[:] = data |
| 613 | + assert_array_equal(array[:], data) |
0 commit comments