|
| 1 | +import pytest |
| 2 | + |
| 3 | +import tmp119 |
| 4 | + |
| 5 | +# Config register bit masks (mirrors the private constants in the driver) |
| 6 | +AVG_MASK = 0x0060 |
| 7 | +CONV_MASK = 0x0380 |
| 8 | + |
| 9 | + |
| 10 | +def test_device_id(sensor): |
| 11 | + assert sensor.get_device_id() == 0x2117 |
| 12 | + |
| 13 | + |
| 14 | +def test_init_success_clears_avg_and_conv(sensor): |
| 15 | + assert sensor.init() is True |
| 16 | + assert sensor.get_config() & (AVG_MASK | CONV_MASK) == 0 |
| 17 | + |
| 18 | + |
| 19 | +def test_init_preserves_unrelated_config_bits(sensor): |
| 20 | + # Set a bit outside the AVG/CONV masks; init() must leave it untouched. |
| 21 | + sensor._bus.regs[0x01] = [0x82, 0x20] # 0x8220 |
| 22 | + assert sensor.init() is True |
| 23 | + assert sensor.get_config() == 0x8000 |
| 24 | + |
| 25 | + |
| 26 | +def test_init_fails_on_wrong_device_id(sensor): |
| 27 | + sensor._bus.regs[0x0F] = [0x00, 0x00] |
| 28 | + assert sensor.init() is False |
| 29 | + |
| 30 | + |
| 31 | +def test_init_returns_false_without_bus(sensor): |
| 32 | + sensor._bus = None |
| 33 | + assert sensor.init() is False |
| 34 | + |
| 35 | + |
| 36 | +def test_read_returns_false_without_bus(sensor): |
| 37 | + sensor._bus = None |
| 38 | + assert sensor.read() is False |
| 39 | + |
| 40 | + |
| 41 | +def test_read_positive_temperature(sensor): |
| 42 | + sensor._bus.regs[0x00] = [0x0C, 0x80] # 3200 * 0.0078125 = 25.0 C |
| 43 | + assert sensor.read() is True |
| 44 | + assert sensor.temperature() == pytest.approx(25.0) |
| 45 | + |
| 46 | + |
| 47 | +def test_read_negative_temperature(sensor): |
| 48 | + sensor._bus.regs[0x00] = [0xFF, 0x00] # -256 * 0.0078125 = -2.0 C |
| 49 | + sensor.read() |
| 50 | + assert sensor.temperature() == pytest.approx(-2.0) |
| 51 | + |
| 52 | + |
| 53 | +def test_temperature_unit_conversions(sensor): |
| 54 | + sensor._bus.regs[0x00] = [0x0C, 0x80] # 25.0 C |
| 55 | + sensor.read() |
| 56 | + assert sensor.temperature(tmp119.UNITS_Centigrade) == pytest.approx(25.0) |
| 57 | + assert sensor.temperature(tmp119.UNITS_Fahrenheit) == pytest.approx(77.0) |
| 58 | + assert sensor.temperature(tmp119.UNITS_Kelvin) == pytest.approx(298.15) |
| 59 | + |
| 60 | + |
| 61 | +def test_set_averaging(sensor): |
| 62 | + assert sensor.set_averaging(tmp119.TMP119_AVERAGE_64X) is True |
| 63 | + assert sensor.get_config() & AVG_MASK == (3 << 5) |
| 64 | + |
| 65 | + |
| 66 | +def test_set_read_delay(sensor): |
| 67 | + assert sensor.set_read_delay(tmp119.TMP119_DELAY_16000_MS) is True |
| 68 | + assert sensor.get_config() & CONV_MASK == (7 << 7) |
| 69 | + |
| 70 | + |
| 71 | +def test_set_averaging_preserves_read_delay(sensor): |
| 72 | + sensor.set_read_delay(tmp119.TMP119_DELAY_8000_MS) |
| 73 | + sensor.set_averaging(tmp119.TMP119_AVERAGE_32X) |
| 74 | + assert sensor.get_config() & CONV_MASK == (6 << 7) |
| 75 | + assert sensor.get_config() & AVG_MASK == (2 << 5) |
| 76 | + |
| 77 | + |
| 78 | +def test_set_config_round_trips_16_bits(sensor): |
| 79 | + sensor.set_config(0xABCD) |
| 80 | + assert sensor.get_config() == 0xABCD |
0 commit comments