Skip to content

Commit cccfe32

Browse files
Fix tests/test_battery_voltage.py
1 parent 60b68ef commit cccfe32

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

tests/test_battery_voltage.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,39 @@ class TestInitADC(unittest.TestCase):
119119
def setUp(self):
120120
"""Reset module state."""
121121
bv.adc = None
122-
bv.scale_factor = 0
122+
bv.conversion_func = None
123123
bv.adc_pin = None
124124

125125
def test_init_adc1_pin(self):
126126
"""Test initializing with ADC1 pin."""
127-
bv.init_adc(5, 0.00161)
127+
def adc_to_voltage(adc_value):
128+
return adc_value * 0.00161
129+
130+
bv.init_adc(5, adc_to_voltage)
128131

129132
self.assertIsNotNone(bv.adc)
130-
self.assertEqual(bv.scale_factor, 0.00161)
133+
self.assertEqual(bv.conversion_func, adc_to_voltage)
131134
self.assertEqual(bv.adc_pin, 5)
132135
self.assertEqual(bv.adc._atten, MockADC.ATTN_11DB)
133136

134137
def test_init_adc2_pin(self):
135138
"""Test initializing with ADC2 pin (should warn but work)."""
136-
bv.init_adc(13, 0.00197)
139+
def adc_to_voltage(adc_value):
140+
return adc_value * 0.00197
141+
142+
bv.init_adc(13, adc_to_voltage)
137143

138144
self.assertIsNotNone(bv.adc)
139-
self.assertEqual(bv.scale_factor, 0.00197)
145+
self.assertIsNotNone(bv.conversion_func)
140146
self.assertEqual(bv.adc_pin, 13)
141147

142-
def test_scale_factor_stored(self):
143-
"""Test that scale factor is stored correctly."""
144-
bv.init_adc(5, 0.12345)
145-
self.assertEqual(bv.scale_factor, 0.12345)
148+
def test_conversion_func_stored(self):
149+
"""Test that conversion function is stored correctly."""
150+
def my_conversion(adc_value):
151+
return adc_value * 0.12345
152+
153+
bv.init_adc(5, my_conversion)
154+
self.assertEqual(bv.conversion_func, my_conversion)
146155

147156

148157
class TestCaching(unittest.TestCase):
@@ -151,16 +160,18 @@ class TestCaching(unittest.TestCase):
151160
def setUp(self):
152161
"""Reset module state."""
153162
bv.clear_cache()
154-
bv.init_adc(5, 0.00161) # Use ADC1 to avoid WiFi complexity
163+
def adc_to_voltage(adc_value):
164+
return adc_value * 0.00161
165+
bv.init_adc(5, adc_to_voltage) # Use ADC1 to avoid WiFi complexity
155166
MockWifiService.reset()
156167

157168
def tearDown(self):
158169
"""Clean up."""
159170
bv.clear_cache()
160171

161-
def test_cache_miss_on_first_read(self):
162-
"""Test that first read doesn't use cache."""
163-
self.assertIsNone(bv._cached_raw_adc)
172+
def test_cache_hit_on_first_read(self):
173+
"""Test that first read already has a cache (because of read during init) """
174+
self.assertIsNotNone(bv._cached_raw_adc)
164175
raw = bv.read_raw_adc()
165176
self.assertIsNotNone(bv._cached_raw_adc)
166177
self.assertEqual(raw, bv._cached_raw_adc)
@@ -203,7 +214,9 @@ class TestADC1Reading(unittest.TestCase):
203214
def setUp(self):
204215
"""Reset module state."""
205216
bv.clear_cache()
206-
bv.init_adc(5, 0.00161) # GPIO5 is ADC1
217+
def adc_to_voltage(adc_value):
218+
return adc_value * 0.00161
219+
bv.init_adc(5, adc_to_voltage) # GPIO5 is ADC1
207220
MockWifiService.reset()
208221
MockWifiService._connected = True
209222

@@ -240,7 +253,9 @@ class TestADC2Reading(unittest.TestCase):
240253
def setUp(self):
241254
"""Reset module state."""
242255
bv.clear_cache()
243-
bv.init_adc(13, 0.00197) # GPIO13 is ADC2
256+
def adc_to_voltage(adc_value):
257+
return adc_value * 0.00197
258+
bv.init_adc(13, adc_to_voltage) # GPIO13 is ADC2
244259
MockWifiService.reset()
245260

246261
def tearDown(self):
@@ -308,7 +323,9 @@ class TestVoltageCalculations(unittest.TestCase):
308323
def setUp(self):
309324
"""Reset module state."""
310325
bv.clear_cache()
311-
bv.init_adc(5, 0.00161) # ADC1 pin, scale factor for 2:1 divider
326+
def adc_to_voltage(adc_value):
327+
return adc_value * 0.00161
328+
bv.init_adc(5, adc_to_voltage) # ADC1 pin, scale factor for 2:1 divider
312329
bv.adc.set_read_value(2048) # Mid-range
313330

314331
def tearDown(self):
@@ -344,7 +361,8 @@ def test_get_battery_percentage_calculation(self):
344361
"""Test percentage calculation."""
345362
# Set voltage to mid-range between MIN and MAX
346363
mid_voltage = (bv.MIN_VOLTAGE + bv.MAX_VOLTAGE) / 2
347-
raw_adc = mid_voltage / bv.scale_factor
364+
# Inverse of conversion function: if voltage = adc * 0.00161, then adc = voltage / 0.00161
365+
raw_adc = mid_voltage / 0.00161
348366
bv.adc.set_read_value(int(raw_adc))
349367
bv.clear_cache()
350368

@@ -374,7 +392,9 @@ class TestAveragingLogic(unittest.TestCase):
374392
def setUp(self):
375393
"""Reset module state."""
376394
bv.clear_cache()
377-
bv.init_adc(5, 0.00161)
395+
def adc_to_voltage(adc_value):
396+
return adc_value * 0.00161
397+
bv.init_adc(5, adc_to_voltage)
378398

379399
def tearDown(self):
380400
"""Clean up."""
@@ -397,7 +417,9 @@ class TestDesktopMode(unittest.TestCase):
397417
def setUp(self):
398418
"""Disable ADC."""
399419
bv.adc = None
400-
bv.scale_factor = 0.00161
420+
def adc_to_voltage(adc_value):
421+
return adc_value * 0.00161
422+
bv.conversion_func = adc_to_voltage
401423

402424
def test_read_raw_adc_returns_random_value(self):
403425
"""Test that desktop mode returns random ADC value."""

0 commit comments

Comments
 (0)