|
| 1 | +"""arbin_res's two-stage entry points (#560). |
| 2 | +
|
| 3 | +arbin_res reads an Access ``.res`` database — ODBC on Windows, mdbtools on |
| 4 | +posix — so these tests skip where no backend is available, exactly as the |
| 5 | +loader golden does. Where a backend exists (CI has mdbtools), they run. |
| 6 | +
|
| 7 | +The vendor stage is unusually thin here: ``get_headers_normal()`` is already a |
| 8 | +``{cellpy attr → Arbin column}`` map, so ``declarations()`` reuses the same |
| 9 | +derivation the configuration loaders use. What ``parse()`` adds is only the |
| 10 | +database read, stopping before the rename and the datetime conversion that |
| 11 | +``harmonize()`` now owns. |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import warnings |
| 17 | + |
| 18 | +import polars as pl |
| 19 | +import pytest |
| 20 | + |
| 21 | +from cellpy.exceptions import LoaderError |
| 22 | +from cellpy.readers import data_structures as ds |
| 23 | + |
| 24 | +SOURCE = "testdata/data/20160805_test001_45_cc_01.res" |
| 25 | + |
| 26 | + |
| 27 | +def _loader(): |
| 28 | + return ds.generate_default_factory().create("arbin_res") |
| 29 | + |
| 30 | + |
| 31 | +def _parsed_or_skip(): |
| 32 | + loader = _loader() |
| 33 | + try: |
| 34 | + with warnings.catch_warnings(): |
| 35 | + warnings.simplefilter("ignore") |
| 36 | + vendor = loader.parse(SOURCE) |
| 37 | + except Exception as exc: # noqa: BLE001 — unavailable backend, not a bug |
| 38 | + pytest.skip(f"arbin_res backend unavailable here: {exc}") |
| 39 | + return loader, vendor |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.essential |
| 43 | +def test_parse_returns_arbin_vendor_names(): |
| 44 | + _, vendor = _parsed_or_skip() |
| 45 | + |
| 46 | + # Arbin's own spellings, not native — renaming is harmonize's job. |
| 47 | + for arbin in ("Voltage", "Current", "Charge_Capacity", "Cycle_Index"): |
| 48 | + assert arbin in vendor.columns, f"{arbin} missing from the parsed frame" |
| 49 | + assert "potential" not in vendor.columns |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.essential |
| 53 | +def test_declarations_derive_from_get_headers_normal(): |
| 54 | + loader, _ = _parsed_or_skip() |
| 55 | + declarations = loader.declarations() |
| 56 | + |
| 57 | + assert declarations.column_map["Voltage"] == "potential" |
| 58 | + assert declarations.column_map["Current"] == "current" |
| 59 | + assert ( |
| 60 | + declarations.column_map["Charge_Capacity"] == "cumulative_charge_capacity" |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.essential |
| 65 | +def test_the_vendor_test_id_is_not_mapped_onto_the_framework_test_id(): |
| 66 | + """``Test_ID`` is provenance; mapping it onto native ``test_id`` (the |
| 67 | + grouping key) would let the vendor value win. The shared derivation drops |
| 68 | + it, same as for the configuration loaders.""" |
| 69 | + loader, _ = _parsed_or_skip() |
| 70 | + declarations = loader.declarations() |
| 71 | + |
| 72 | + assert "Test_ID" not in declarations.column_map |
| 73 | + assert "test_id" not in declarations.column_map.values() |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.essential |
| 77 | +def test_declarations_before_parse_raises(): |
| 78 | + loader = _loader() |
| 79 | + with pytest.raises(LoaderError, match="before parse"): |
| 80 | + loader.declarations() |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.essential |
| 84 | +def test_two_stage_capacities_match_the_legacy_frame(): |
| 85 | + """The assertion the switchover rests on, through the public entry points.""" |
| 86 | + import cellpy |
| 87 | + |
| 88 | + loader, vendor = _parsed_or_skip() |
| 89 | + from cellpy.readers.instruments.harmonize import harmonize |
| 90 | + |
| 91 | + with warnings.catch_warnings(): |
| 92 | + warnings.simplefilter("ignore") |
| 93 | + raw = harmonize(vendor, loader.declarations(), strict=False) |
| 94 | + cell = cellpy.get(SOURCE, instrument="arbin_res", mass=1.0, testing=True) |
| 95 | + |
| 96 | + legacy = pl.from_pandas(cell.data.raw.reset_index(drop=True)) |
| 97 | + for column in ( |
| 98 | + "potential", |
| 99 | + "current", |
| 100 | + "cumulative_charge_capacity", |
| 101 | + "cumulative_discharge_capacity", |
| 102 | + "test_time", |
| 103 | + ): |
| 104 | + difference = ( |
| 105 | + raw[column].cast(pl.Float64) - legacy[column].cast(pl.Float64) |
| 106 | + ).abs().max() |
| 107 | + assert difference is not None and difference < 1e-9, ( |
| 108 | + f"{column} differs by {difference}" |
| 109 | + ) |
0 commit comments