################### Beam Weight Helpers ################### There are a number of helpers for interfacing with and testing the beam weight store. There are some client helpers in :py:mod:`ska_low_mccs_common.beam_weight_store_client_utils` providing :py:func:`~ska_low_mccs_common.beam_weight_store_client_utils.insert`, :py:func:`~ska_low_mccs_common.beam_weight_store_client_utils.update` and :py:func:`~ska_low_mccs_common.beam_weight_store_client_utils.select` methods. And there are test helper methods in :py:mod:`ska_low_mccs_common.testing.beam_weight_store_testing_utils` to assist with safe interactions with the database while testing. Weighting keys with the prefix ``thorn-test-`` (``RESERVED_TEST_KEY_PREFIX``) are reserved for testing and must never be used for production weighting data. This convention is assisted by helpers in :py:mod:`ska_low_mccs_common.testing.beam_weight_store_testing_utils`: :py:func:`~ska_low_mccs_common.testing.beam_weight_store_testing_utils.generate_test_weighting_key` creates a unique, reserved key per test run, embedding a timestamp (so a later sweep can determine its age) and a UUID (to avoid collisions between concurrent runs against the same shared database). :py:func:`~ska_low_mccs_common.testing.beam_weight_store_testing_utils.require_test_weighting_key` is a fail-closed guard: it raises immediately if a key does not have the reserved prefix, and is intended to be called before any ``Insert``/``Update``/``Delete`` that a test issues directly. :py:func:`~ska_low_mccs_common.testing.beam_weight_store_testing_utils.tmp_test_weighting_key` is a context manager that generates a key, inserts weights under it, yields the key, and guarantees a scoped ``Delete`` of that one key in a ``finally`` block, whether or not the test body raises. If the insert itself raises, it still attempts a best-effort delete, in case the insert committed but its reply was lost, and re-raises the original error. Cleanup is always a targeted delete of the single key it created, never a broader operation such as a table truncation. :py:func:`~ska_low_mccs_common.testing.beam_weight_store_testing_utils.sweep_stale_test_weighting_keys` is an orphan sweep: given a device proxy and a maximum age, it finds reserved keys older than that age and deletes them, returning the keys removed. It never inspects or deletes a non-``thorn-test-`` key, and is safe to call repeatedly or concurrently. These helpers live in this repository so that both MCCS and its functional test suite share a single definition of the reserved prefix. The end-to-end tests that exercise a persistent deployment of ``MccsBeamWeightStore`` live in the separate ska-low-mccs-hw-tests repository; that suite is expected to import these helpers directly into a session-scoped pytest fixture to clean up keys left behind by crashed or interrupted runs. Testing against the production database via a reserved key prefix, rather than reconfiguring the deployment to point at a separate test backend, was not a trivial choice. It was determined to be a blast-radius problem. Repointing the backend risks the reconfiguration never being reverted, leaving production silently pointed at a test backend. Testing against the production database directly risks polluting production data instead; the utilities above mitigate that risk by enforcing the reserved naming convention and only ever deleting entries that carry the prefix.