-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_device.py
More file actions
39 lines (26 loc) · 1.15 KB
/
Copy pathtest_device.py
File metadata and controls
39 lines (26 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Unit tests for the PZEM004TDevice simulator."""
from __future__ import annotations
import pytest
from python_grpc.core.device.pzem_004t import PZEM004TDevice
@pytest.fixture
def device() -> PZEM004TDevice:
return PZEM004TDevice(seed=7)
def test_read_produces_realistic_plausible_values(device: PZEM004TDevice) -> None:
report = device.read()
assert report.device_id == "PZEM-004T-0001"
assert report.device_type == "PZEM-004T"
assert report.timestamp_unix_ms > 0
assert report.voltage == pytest.approx(230.0, abs=10.0)
assert report.current == pytest.approx(2.5, abs=0.3)
assert report.active_power > 0.0
assert report.frequency == pytest.approx(50.0, abs=2.0)
assert report.power_factor <= 1.0
def test_seeded_rng_is_reproducible() -> None:
a = PZEM004TDevice(seed=7)
b = PZEM004TDevice(seed=7)
assert a.read().voltage == b.read().voltage
def test_energy_monotonically_increases(device: PZEM004TDevice) -> None:
first = device.read()
assert device.read().energy >= first.energy
def test_read_many_yields_requested_count(device: PZEM004TDevice) -> None:
assert len(list(device.read_many(8))) == 8