test_cell.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import pytest
  2. from datetime import datetime, timedelta
  3. from battery_measure_ctrl.src.models.cell import Cell, CellLimits, MeasureValues
  4. @pytest.fixture
  5. def cell_limits():
  6. return CellLimits(min_volt=3000, max_volt=4200, max_curr=1000)
  7. @pytest.fixture
  8. def test_cell(cell_limits):
  9. return Cell(id=1, cell_limits=cell_limits, nom_capacity=2000.0) # 2000mAh capacity
  10. def generate_measurement_sequence(current_pattern: list[int],
  11. interval_seconds: float = 1.0,
  12. voltage: int = 3700,
  13. temperature: int = 25):
  14. """Helper to generate a sequence of measurements with specified timing"""
  15. measurements = []
  16. start_time = datetime.now()
  17. last_time = start_time
  18. for current in current_pattern:
  19. measurement = MeasureValues(voltage=voltage,
  20. current=current,
  21. temperature=temperature)
  22. # Set up timing for this measurement
  23. current_time = last_time + timedelta(seconds=interval_seconds)
  24. duration = (current_time - last_time).total_seconds()
  25. measurements.append((measurement, duration))
  26. last_time = current_time
  27. return measurements
  28. def test_normal_cycles(test_cell:Cell):
  29. """Test capacity estimation with normal charge/discharge cycles"""
  30. # Create pattern for 2 complete cycles at 0.25C (500mA)
  31. # Each charge should take ~4 hours = 14400 seconds
  32. current_pattern = (
  33. [500] * 14400 + # 4h charge at 0.25C
  34. [-500] * 14400 + # 4h discharge
  35. [500] * 14400 + # 4h charge
  36. [-500] * 14400 # 4h discharge
  37. )
  38. # Add measurements
  39. for measurement, duration in generate_measurement_sequence(current_pattern):
  40. test_cell.measurements_duration.append(duration)
  41. test_cell.measurements.append(measurement)
  42. capacity = test_cell.estimate_capacity()
  43. assert 95.0 <= capacity <= 100.0, "Expected near 100% capacity for normal cycles"
  44. def test_degraded_capacity(test_cell:Cell):
  45. """Test capacity estimation with shorter charging cycles (degraded cell)"""
  46. # Create pattern for 2 cycles at 0.25C but with shorter duration (~75% capacity)
  47. current_pattern = (
  48. [500] * 10800 + # 3h charge at 0.25C
  49. [-500] * 10800 + # 3h discharge
  50. [500] * 10800 + # 3h charge
  51. [-500] * 10800 # 3h discharge
  52. )
  53. for measurement, duration in generate_measurement_sequence(current_pattern):
  54. test_cell.measurements_duration.append(duration)
  55. test_cell.measurements.append(measurement)
  56. capacity = test_cell.estimate_capacity()
  57. assert 70.0 <= capacity <= 80.0, "Expected ~75% capacity for degraded cell"
  58. def test_empty_measurements(test_cell:Cell):
  59. """Test capacity estimation with no measurements"""
  60. capacity = test_cell.estimate_capacity()
  61. assert capacity == -1.0, "Expected -1.0 for no measurements"
  62. def test_no_charge_cycles(test_cell:Cell):
  63. """Test capacity estimation with no charging cycles"""
  64. # Only discharge cycles
  65. current_pattern = [-500] * 1000
  66. for measurement, duration in generate_measurement_sequence(current_pattern):
  67. test_cell.measurements_duration.append(duration)
  68. test_cell.measurements.append(measurement)
  69. capacity = test_cell.estimate_capacity()
  70. assert capacity == -1.0, "Expected -1.0 for no charge cycles"
  71. def test_incomplete_cycle(test_cell:Cell):
  72. """Test capacity estimation with incomplete cycle"""
  73. # One complete cycle and one incomplete
  74. current_pattern = (
  75. [500] * 14400 + # 4h charge
  76. [-500] * 14400 + # 4h discharge
  77. [500] * 7200 # 2h partial charge
  78. )
  79. for measurement, duration in generate_measurement_sequence(current_pattern):
  80. test_cell.measurements_duration.append(duration)
  81. test_cell.measurements.append(measurement)
  82. capacity = test_cell.estimate_capacity()
  83. assert 70.0 <= capacity <= 80.0, "Expected capacity calculation from incomplete cycle"