-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_powermeterapatorec3.py
143 lines (114 loc) · 5.02 KB
/
test_powermeterapatorec3.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
__author__ = 'Holger Fleischmann'
__copyright__ = 'Copyright 2021, Holger Fleischmann, Bavaria/Germany'
__license__ = 'Apache License 2.0'
from unittest import TestCase, main
from powermeterapatorec3 import PowerMeterReading, PowerMeterApatorEC3Repeating, PowerMeterApatorEC3
class MockPowerMeterApatorEC3:
def start(self):
pass
def close(self):
pass
def read(self):
return PowerMeterReading(False, None, None, None)
class TestPowerMeterApatorEC3(TestCase):
def test__parse_line_str(self):
pm = PowerMeterApatorEC3("none")
self.assertEqual("008482.46", pm._parse_line_str("1.8.1*00(008482.46) "))
def test__parse_(self):
pm = PowerMeterApatorEC3("none")
self.assertEqual(8482.46, pm._parse_line_float("1.8.1*00(008482.46) "))
class TestPowerMeterApatorEC3Repeating(TestCase):
def setUp(self):
self.pm = PowerMeterApatorEC3Repeating(MockPowerMeterApatorEC3(), 1, 30)
def test_update_high_power(self):
self.pm.reading_ts = 1e9 - 200
self.pm.reading = PowerMeterReading(False, None, None, None)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertIsNone(self.pm.high.power)
self.pm.reading_ts = 1e9 - 100
self.pm.reading = PowerMeterReading(True, 3000, 1999.9, 1000)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertIsNone(self.pm.high.power)
self.pm.reading_ts = 1e9
self.pm.reading = PowerMeterReading(True, 3000, 2000, 1000)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertIsNone(self.pm.high.power)
self.pm.reading_ts = 1e9 + 3600
self.pm.reading = PowerMeterReading(True, 3000, 2000.1, 1000)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.high.power, 100.)
self.pm.reading_ts += 600
self.pm.reading = PowerMeterReading(True, 3000, 2000.1, 1000)
self.pm.low.power = 123
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.high.power, 100.)
self.assertEqual(self.pm.low.power, 123)
self.pm.reading_ts += 600
self.pm.reading = PowerMeterReading(True, 3000, 2000.2, 1000)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.high.power, 300.)
self.pm.reading_ts += 100
self.pm.reading = PowerMeterReading(False, None, None, None)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.high.power, 300.)
self.pm.reading_ts += 500
self.pm.reading = PowerMeterReading(True, 3000, 2001.2, 1000)
self.pm.low.power = 123
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.high.power, 3600 * 1000 / 600.)
self.assertEqual(self.pm.low.power, 0)
def test_update_low_power(self):
self.pm.reading_ts = 1e9 - 60
self.pm.reading = PowerMeterReading(False, None, None, None)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertIsNone(self.pm.low.power)
self.pm.reading_ts = 1e9 - 30
self.pm.reading = PowerMeterReading(True, 3000, 2000, 999.9)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertIsNone(self.pm.low.power)
self.pm.reading_ts = 1e9
self.pm.reading = PowerMeterReading(True, 3000, 2000, 1000)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertIsNone(self.pm.low.power)
self.pm.reading_ts = 1e9 + 3600
self.pm.reading = PowerMeterReading(True, 3000, 2000, 1000.1)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.low.power, 100.)
self.pm.reading_ts += 600
self.pm.reading = PowerMeterReading(True, 3000, 2000, 1000.1)
self.pm.high.power = 123
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.low.power, 100.)
self.assertEqual(self.pm.high.power, 123)
self.pm.reading_ts += 600
self.pm.reading = PowerMeterReading(True, 3000, 2000, 1000.2)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.low.power, 300.)
self.pm.reading_ts += 100
self.pm.reading = PowerMeterReading(False, None, None, None)
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.low.power, 300.)
self.pm.reading_ts += 500
self.pm.reading = PowerMeterReading(True, 3000, 2000, 1001.2)
self.pm.high.power = 123
self.pm._update_high_power()
self.pm._update_low_power()
self.assertAlmostEqual(self.pm.low.power, 3600 * 1000 / 600.)
self.assertEqual(self.pm.high.power, 0)
if __name__ == '__main__':
main()