Skip to content

Commit a0c505b

Browse files
authored
Merge pull request #226 from plugwise/neg-testing-4
Improve async testing
2 parents 069a53d + 0ba88c0 commit a0c505b

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def _add_log_record(
468468
return False
469469

470470
# Drop useless log records when we have at least 4 logs
471-
if self.collected_logs > 4 and log_record.timestamp < (
471+
if self.collected_logs > 4 and log_record.timestamp <= (
472472
datetime.now(tz=UTC) - timedelta(hours=MAX_LOG_HOURS)
473473
):
474474
return False

tests/test_usb.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -939,15 +939,13 @@ async def fake_get_missing_energy_logs(address: int) -> None:
939939
)
940940
await stick.disconnect()
941941

942-
@freeze_time(dt.now())
942+
@freeze_time("2025-04-03 22:00:00")
943943
def test_pulse_collection_consumption(
944944
self, monkeypatch: pytest.MonkeyPatch
945945
) -> None:
946946
"""Testing pulse collection class."""
947947
monkeypatch.setattr(pw_energy_pulses, "MAX_LOG_HOURS", 24)
948-
949-
fixed_timestamp_utc = dt.now(UTC)
950-
fixed_this_hour = fixed_timestamp_utc.replace(minute=0, second=0, microsecond=0)
948+
fixed_this_hour = dt.now(UTC)
951949

952950
# Test consumption logs
953951
tst_consumption = pw_energy_pulses.PulseCollection(mac="0098765432101234")
@@ -1085,7 +1083,6 @@ def test_pulse_collection_consumption(
10851083

10861084
assert not tst_consumption.log_rollover
10871085
# add missing logs
1088-
test_timestamp = fixed_this_hour - td(hours=3)
10891086
tst_consumption.add_log(99, 2, (fixed_this_hour - td(hours=3)), 1000)
10901087
tst_consumption.add_log(99, 1, (fixed_this_hour - td(hours=4)), 1000)
10911088
tst_consumption.add_log(98, 4, (fixed_this_hour - td(hours=5)), 1000)
@@ -1112,31 +1109,42 @@ def test_pulse_collection_consumption(
11121109

11131110

11141111
# Test rollover by updating pulses before log record
1115-
pulse_update_3 = fixed_this_hour + td(hours=0, minutes=0, seconds=30)
1112+
pulse_update_3 = fixed_this_hour + td(hours=1, minutes=0, seconds=30)
1113+
test_timestamp = fixed_this_hour + td(hours=1)
11161114
tst_consumption.update_pulse_counter(2500, 0, pulse_update_3)
1117-
assert not tst_consumption.log_rollover
1115+
assert tst_consumption.log_rollover
1116+
# Collected pulses last hour:
11181117
assert tst_consumption.collected_pulses(
11191118
test_timestamp, is_consumption=True
1120-
) == (2500 + 1111 + 1000 + 750, pulse_update_3)
1121-
pulse_update_4 = fixed_this_hour + td(hours=1, minutes=1, seconds=3)
1119+
) == (2500, pulse_update_3)
1120+
# Collected pulses last day:
1121+
assert tst_consumption.collected_pulses(
1122+
test_timestamp - td(hours=24), is_consumption=True
1123+
) == (2500 + 22861, pulse_update_3)
1124+
pulse_update_4 = fixed_this_hour + td(hours=1, minutes=1)
11221125
tst_consumption.update_pulse_counter(45, 0, pulse_update_4)
11231126
assert tst_consumption.log_rollover
1124-
test_timestamp = fixed_this_hour + td(hours=1, minutes=1, seconds=4)
1127+
# Collected pulses last hour:
11251128
assert tst_consumption.collected_pulses(
11261129
test_timestamp, is_consumption=True
11271130
) == (45, pulse_update_4)
1128-
tst_consumption.add_log(100, 2, (fixed_this_hour + td(hours=1, minutes=1, seconds=5)), 2222)
1129-
assert tst_consumption.log_rollover
1131+
# Collected pulses last day:
1132+
assert tst_consumption.collected_pulses(
1133+
test_timestamp - td(hours=24), is_consumption=True
1134+
) == (45 + 22861, pulse_update_4)
1135+
# pulse-count of 2500 is ignored, the code does not export this incorrect value
1136+
1137+
tst_consumption.add_log(100, 2, (fixed_this_hour + td(hours=1)), 2222)
1138+
assert not tst_consumption.log_rollover
11301139
# Test collection of the last full hour
11311140
assert tst_consumption.collected_pulses(
11321141
fixed_this_hour, is_consumption=True
11331142
) == (45 + 2222, pulse_update_4)
11341143
pulse_update_5 = fixed_this_hour + td(hours=1, minutes=1, seconds=18)
1135-
test_timestamp_2 = fixed_this_hour + td(hours=1, minutes=1, seconds=20)
11361144
tst_consumption.update_pulse_counter(145, 0, pulse_update_5)
11371145
# Test collection of the last new hour
11381146
assert tst_consumption.collected_pulses(
1139-
test_timestamp_2, is_consumption=True
1147+
test_timestamp, is_consumption=True
11401148
) == (145, pulse_update_5)
11411149

11421150
# Test log rollover by updating log first before updating pulses

0 commit comments

Comments
 (0)