|
| 1 | +from atmotube import InvalidByteData |
1 | 2 | from atmotube import StatusPacket, SPS30Packet, BME280Packet, SGPC3Packet |
2 | 3 |
|
| 4 | +import pytest |
| 5 | +import time |
| 6 | + |
| 7 | +# Status Packet tests |
| 8 | +timestamp = 1766865019.792235 |
3 | 9 | status_test_byte = bytearray(b'Ad') |
4 | | -sps30_test_byte = bytearray(b'd\x00\x00\xb9\x00\x00J\x01\x00o\x00\x00') |
5 | | -bme280_test_byte = bytearray(b'\x0e\x17\x8ao\x01\x00\x1a\t') |
6 | | -sgpc3_test_byte = bytearray(b'\x02\x00\x00\x00') |
| 10 | +status_test_invalid_byte = bytearray(b'A') |
| 11 | +status_packet_str = (f"StatusPacket(timestamp={time.ctime(timestamp)}, " |
| 12 | + f"pm_sensor_status=True, " |
| 13 | + f"error_flag=False, bonding_flag=False, " |
| 14 | + f"charging=False, charging_timer=False, " |
| 15 | + f"pre_heating=True, battery_level=100%)") |
7 | 16 |
|
8 | 17 |
|
9 | 18 | def test_status_packet(): |
10 | | - packet = StatusPacket(status_test_byte) |
| 19 | + packet = StatusPacket(status_test_byte, ts=timestamp) |
11 | 20 | assert packet.pm_sensor_status is True |
12 | 21 | assert packet.error_flag is False |
13 | 22 | assert packet.bonding_flag is False |
14 | 23 | assert packet.charging is False |
15 | 24 | assert packet.charging_timer is False |
16 | 25 | assert packet.pre_heating is True |
17 | 26 | assert packet.battery_level == 100 |
18 | | - assert str(packet) == ("StatusPacket(pm_sensor_status=True, " |
19 | | - "error_flag=False, bonding_flag=False, " |
20 | | - "charging=False, charging_timer=False, " |
21 | | - "pre_heating=True, battery_level=100%)") |
| 27 | + assert str(packet) == status_packet_str |
| 28 | + assert repr(packet) == status_packet_str |
| 29 | + with pytest.raises(InvalidByteData): |
| 30 | + StatusPacket(status_test_invalid_byte, ts=timestamp) |
| 31 | + |
| 32 | + |
| 33 | +# SPS30 Packet tests |
| 34 | +sps30_test_byte = bytearray(b'd\x00\x00\xb9\x00\x00J\x01\x00o\x00\x00') |
| 35 | +sps30_test_invalid_byte = bytearray(b'd\x00\x00\xb9\x00\x00J\x01') |
| 36 | +sps30_test_str = (f"SPS30Packet(timestamp={time.ctime(timestamp)}, " |
| 37 | + f"pm1=1.0µg/m³, pm2_5=1.85µg/m³, " |
| 38 | + f"pm10=3.3µg/m³, pm4=1.11µg/m³)") |
22 | 39 |
|
23 | 40 |
|
24 | 41 | def test_sps30_packet(): |
25 | | - packet = SPS30Packet(sps30_test_byte) |
| 42 | + packet = SPS30Packet(sps30_test_byte, ts=timestamp) |
26 | 43 | assert packet.pm1 == 1.0 |
27 | 44 | assert packet.pm2_5 == 1.85 |
28 | 45 | assert packet.pm10 == 3.3 |
29 | 46 | assert packet.pm4 == 1.11 |
30 | | - assert str(packet) == ("SPS30Packet(pm1=1.0µg/m³, pm2_5=1.85µg/m³, " |
31 | | - "pm10=3.3µg/m³, pm4=1.11µg/m³)") |
| 47 | + assert str(packet) == sps30_test_str |
| 48 | + assert repr(packet) == sps30_test_str |
| 49 | + with pytest.raises(InvalidByteData): |
| 50 | + SPS30Packet(sps30_test_invalid_byte, ts=timestamp) |
| 51 | + |
| 52 | + |
| 53 | +# BME280 Packet tests |
| 54 | +bme280_test_byte = bytearray(b'\x0e\x17\x8ao\x01\x00\x1a\t') |
| 55 | +bme280_test_invalid_byte = bytearray(b'\x0e\x17\x8ao\x01') |
| 56 | +bme280_test_str = (f"BME280Packet(timestamp={time.ctime(timestamp)}, " |
| 57 | + f"humidity=14%, temperature=23.3°C, " |
| 58 | + f"pressure=940.9mbar)") |
32 | 59 |
|
33 | 60 |
|
34 | 61 | def test_bme280_packet(): |
35 | | - packet = BME280Packet(bme280_test_byte) |
| 62 | + packet = BME280Packet(bme280_test_byte, ts=timestamp) |
36 | 63 | assert packet.humidity == 14 |
37 | 64 | assert packet.temperature == 23.3 |
38 | 65 | assert packet.pressure == 940.9 |
39 | | - assert str(packet) == ("BME280Packet(humidity=14%, temperature=23.3°C, " |
40 | | - "pressure=940.9mbar)") |
| 66 | + assert str(packet) == bme280_test_str |
| 67 | + assert repr(packet) == bme280_test_str |
| 68 | + with pytest.raises(InvalidByteData): |
| 69 | + BME280Packet(bme280_test_invalid_byte, ts=timestamp) |
| 70 | + |
| 71 | + |
| 72 | +# SGPC3 Packet tests |
| 73 | +sgpc3_test_byte = bytearray(b'\x02\x00\x00\x00') |
| 74 | +sgpc3_test_invalid_byte = bytearray(b'\x02\x00\x00') |
| 75 | +sgpc3_test_str = (f"SGPC3Packet(timestamp={time.ctime(timestamp)}, " |
| 76 | + f"tvoc=0.002ppb)") |
41 | 77 |
|
42 | 78 |
|
43 | 79 | def test_sgpc3_packet(): |
44 | | - packet = SGPC3Packet(sgpc3_test_byte) |
| 80 | + packet = SGPC3Packet(sgpc3_test_byte, ts=timestamp) |
45 | 81 | assert packet.tvoc == 0.002 |
46 | | - assert str(packet) == "SGPC3Packet(tvoc=0.002ppb)" |
| 82 | + assert str(packet) == sgpc3_test_str |
| 83 | + assert repr(packet) == sgpc3_test_str |
| 84 | + with pytest.raises(InvalidByteData): |
| 85 | + SGPC3Packet(sgpc3_test_invalid_byte, ts=timestamp) |
0 commit comments