-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_models.py
163 lines (138 loc) · 5.48 KB
/
test_models.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import logging as test_logging
import pytest # type: ignore
from deta import Deta # type: ignore
from decouple import config # type: ignore
import constants as c
import model as model
DETA_KEY = config("DETA_KEY")
deta = Deta(DETA_KEY) # type: ignore
test_logging.basicConfig(
filename="tests.log", encoding="utf-8", level=test_logging.DEBUG
)
temp_config = c.SensorConfig(sensor_type=c.SensorTypes(1))
humidity_config = c.SensorConfig(sensor_type=c.SensorTypes(2))
air_quality_config = c.SensorConfig(sensor_type=c.SensorTypes(3))
@pytest.fixture
def example_temperature_event() -> model.SensorLogEvent:
temp = 95
readings4 = model.SensorLogReading(
sensor_name="arduino_1", sensor_reading=temp, sensor_type=c.SensorTypes(1)
)
readings5 = model.SensorLogReading(
sensor_name="arduino_2", sensor_reading=temp, sensor_type=c.SensorTypes(2)
)
readings = model.SensorLogEvent(
datetime=1665021239,
event="f3ec6e7b-382b-472b-ad13-c52d7327cf76",
best_lat=45.5728875,
best_long=-122.66610937499999,
readings=[readings4, readings5],
)
return readings
def test_event_parsing_2(example_temperature_event):
parsed_response = example_temperature_event.parse_event()
assert all(isinstance(i, model.ParsedReading) for i in parsed_response)
for i in range(len(parsed_response)):
assert parsed_response[i] == model.ParsedReading(
datetime=example_temperature_event.datetime,
event=example_temperature_event.event,
best_lat=example_temperature_event.best_lat,
best_long=example_temperature_event.best_long,
sensor_name=example_temperature_event.readings[i].sensor_name,
sensor_config=model.SensorConfig(
example_temperature_event.readings[i].sensor_type
),
sensor_reading=example_temperature_event.readings[i].sensor_reading,
recent_average=example_temperature_event.compute_recent_sensor_averages(
example_temperature_event.readings[i].sensor_name,
example_temperature_event.readings[i].sensor_reading,
),
)
@pytest.fixture
def example_temperature_reading(temp, average) -> model.ParsedReading:
return model.ParsedReading(
datetime=1665021239,
event="f3ec6e7b-382b-472b-ad13-c52d7327cf76",
best_lat=45.5728875,
best_long=-122.66610937499999,
sensor_name="arduino1",
sensor_config=c.SensorConfig(c.SensorTypes(1)),
sensor_reading=temp,
recent_average=average,
)
@pytest.mark.parametrize(
"temp,average",
[
(
# temp reading is above threshold
5 + temp_config.thresholds["single_reading"],
# recent average is below the average alert threshold
temp_config.thresholds["average"] - 5,
),
# testing extremely large numbers
(2000000000000000000, -5600000000),
# testing decimals
(1500.52323, -342.4),
],
)
def test_temperature_too_high(example_temperature_reading):
notification_event = model.Notifications(queued_notifications=[])
res = notification_event._evaluate_for_notify_logic(example_temperature_reading)
test_logging.debug(f"res = {res}")
assert res[1] == c.NotificationType.TOO_HIGH_SINGLE
@pytest.mark.parametrize(
"temp,average",
[
(
# single reading below threshold
temp_config.thresholds["single_reading"] - 4,
# average reading above threshold
temp_config.thresholds["average"] + 5,
),
# testing large numbers
(-54, 200000000000000),
],
)
def test_average_temperature_too_high(example_temperature_reading):
notification_event = model.Notifications(queued_notifications=[])
res = notification_event._evaluate_for_notify_logic(example_temperature_reading)
test_logging.debug(f"res = {res}")
assert res[1] == c.NotificationType.TOO_HIGH_AVERAGE
@pytest.mark.skip(reason="feature disabled")
@pytest.mark.parametrize(
"temp,average",
[
(
# below single reading threshold
temp_config.thresholds["single_reading"] - 1,
# subtract single reading threshold from the increase change threshold + 1 to ensure that it detects when the temperature change happens fast enough
temp_config.thresholds["single_reading"]
- (temp_config.thresholds["single_increase_change"] + 1),
),
],
)
def test_increase_temperature_too_high(example_temperature_reading):
notification_event = model.Notifications(queued_notifications=[])
res = notification_event._evaluate_for_notify_logic(example_temperature_reading)
print(f"res = {res}")
assert res[1] == c.NotificationType.RAPID_INCREASE
@pytest.mark.parametrize(
"temp,average",
[("alex", 45), ("alex", "sasg")],
)
def test_temperature_exception_handling(example_temperature_reading):
with pytest.raises(TypeError):
notification_event = model.Notifications(queued_notifications=[])
res = notification_event._evaluate_for_notify_logic(example_temperature_reading)
print(f"res = {res}")
@pytest.mark.parametrize(
"temp,average",
[(90, 45)],
)
def test_database_error_handling(example_temperature_reading):
test_db = deta.Base("test_db")
db_res = example_temperature_reading.insert_parsed_reading_into_db(test_db)
assert db_res == True
res = test_db.fetch()
for i in res.items:
test_db.delete(i["key"])