Files
python3-damaris/tests/test_Drawable.py
T
markusro 947c4c7f8d normalize list/array handling in data classes
Establish one invariant across the Drawable hierarchy: x is always a
numpy ndarray, y is always a list of per-channel ndarrays, and empty
data is the same types with zero length.

- Drawable: x starts as empty ndarray; min/max helpers return 0 on
  empty data instead of raising; drop contradictory isinstance(self.y,
  list) branches
- ADC_Result/Accumulation: empty constructors build ndarray x; full
  constructors coerce x/y/y_square with numpy.asarray; Accumulation
  get_ydata/get_yerr return empty ndarrays and no longer crash on
  empty statistics-less accumulations
- MeasurementResult: fix get_lineplotdata to return the accumulated
  means (previously the never-populated self.y, so the GUI line plot
  could never draw); remove dead float/int branches in CSV and HDF
  writers
- Error_Result: get_xdata/get_ydata return ndarrays

Add unittest coverage for the invariant, empty-state accessors,
constructor coercion, line plot data, CSV output and HDF round-trip.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-10-18 16:38:41 +02:00

55 lines
1.7 KiB
Python

import unittest
import numpy as np
from damaris.data.Drawable import Drawable
class TestDrawable(unittest.TestCase):
def test_initial_types(self):
"""x must start as an empty ndarray, y as a list of arrays."""
d = Drawable()
self.assertIsInstance(d.x, np.ndarray)
self.assertEqual(d.x.size, 0)
self.assertIsInstance(d.y, list)
def test_empty_min_max_helpers(self):
"""min/max helpers must return 0 instead of raising on empty data."""
d = Drawable()
self.assertEqual(d.get_xmin(), 0)
self.assertEqual(d.get_xmax(), 0)
self.assertEqual(d.get_xminpos(), 0)
self.assertEqual(d.get_ymin(), 0)
self.assertEqual(d.get_ymax(), 0)
self.assertEqual(d.get_yminpos(), 0)
def test_min_max_helpers_with_data(self):
d = Drawable()
d.x = np.array([-1.0, 0.0, 2.0, 5.0])
d.y = [np.array([1.0, -2.0, 3.0]), np.array([0.5, 4.0, -0.5])]
self.assertEqual(d.get_xmin(), -1.0)
self.assertEqual(d.get_xmax(), 5.0)
self.assertEqual(d.get_xminpos(), 2.0)
self.assertEqual(d.get_ymin(), -2.0)
self.assertEqual(d.get_ymax(), 4.0)
self.assertEqual(d.get_yminpos(), 0.5)
def test_yminpos_without_positive_values(self):
d = Drawable()
d.x = np.array([1.0])
d.y = [np.array([-1.0, -2.0])]
self.assertEqual(d.get_yminpos(), 0)
def test_set_xdata_set_ydata(self):
d = Drawable()
d.x = np.zeros(3)
d.y = [np.zeros(3)]
d.set_xdata(1, 5.0)
d.set_ydata(0, 1, 7.0)
self.assertEqual(d.get_xdata()[1], 5.0)
self.assertEqual(d.get_ydata(0)[1], 7.0)
if __name__ == "__main__":
unittest.main()