-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_thlogger.py
217 lines (183 loc) · 7.31 KB
/
test_thlogger.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from collections import namedtuple
from unittest.mock import MagicMock, patch
import tempfile
import json
import pytest
import os
import sys
from requests.exceptions import ConnectionError
sys.modules["Adafruit_DHT"] = MagicMock()
from thlogger import THLogger # noqa
CONFIG_FILE_PATH = "thlogger.conf.example"
NETWORK_RESTARTED = False
def init_logger(config_file_path=None):
Args = namedtuple("Args", ["CONFIG_FILE", "MAX_CONNECTION_RETRIES"])
if not config_file_path:
config_file_path = CONFIG_FILE_PATH
args = Args(config_file_path, 1)
thlogger = THLogger(args)
# reduce sleep to speed up tests runtime
thlogger.SLEEP_BETWEEN_READINGS = 0.1
return thlogger
def raise_keyboardinterrupt():
raise KeyboardInterrupt
def conditionally_raise_connectionerror(bucket):
if not NETWORK_RESTARTED:
raise ConnectionError
return True
def raise_exception():
raise Exception
def set_network_restarted(*args):
global NETWORK_RESTARTED
NETWORK_RESTARTED = True
@patch("thlogger.THLogger.init_db_connection", return_value=None)
def test_init(mock_init_db):
thlogger = init_logger()
assert isinstance(thlogger, THLogger)
assert thlogger.CONFIG_FILE == CONFIG_FILE_PATH
assert thlogger.SENSOR_MODEL == 11
assert thlogger.GPIO_PIN == 17
assert thlogger.HOST == "localhost"
assert thlogger.PORT == 8086
assert thlogger.ORG == "isberg"
assert thlogger.BUCKET == "thlogger"
assert thlogger.LOCATION == "garage"
assert thlogger.TOKEN == "8wunTOywHGlfynE96WRM3z7nySZpetDOlxRvdKAi83lt4-EyYWqZpG1o267fwOfZMAwRF2qRE9SyV-OtAc5a7g=="
assert thlogger.SLEEP_BETWEEN_READINGS == 0.1
assert thlogger.measurements == []
# test exception if config path is wrong
assert mock_init_db.called
with pytest.raises(Exception) as e:
thlogger = init_logger("does_not_exist")
assert "Error opening" in str(e.value)
# test fallback to basic config for logging
with open(CONFIG_FILE_PATH) as f:
config = json.load(f)
del config["LOG_CONFIG"]
temp_file_name = None
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tf:
tf.write(json.dumps(config))
temp_file_name = tf.name
thlogger = init_logger(temp_file_name)
os.remove(temp_file_name)
#if not buckets_api.find_bucket_by_name(self.BUCKET):
@patch("Adafruit_DHT.read_retry", return_value=(50, 20))
@patch("influxdb_client.BucketsApi.find_bucket_by_name", return_value=True)
@patch("influxdb_client.BucketsApi.create_bucket", return_value=True)
@patch("influxdb_client.WriteApi.write", return_value=None)
def test_read_write(
mock_write, mock_create_bucket, mock_find_bucket, mock_read
):
thlogger = init_logger()
thlogger.work(max_iterations=1)
assert mock_read.called
assert mock_find_bucket.called
assert not mock_create_bucket.called
assert mock_write.called
assert len(thlogger.measurements) == 0
@patch("Adafruit_DHT.read_retry", return_value=(50, 20))
@patch("influxdb_client.BucketsApi.find_bucket_by_name", return_value=None)
@patch("influxdb_client.BucketsApi.create_bucket", return_value=True)
@patch("influxdb_client.WriteApi.write", return_value=None)
def test_create_bucket(
mock_write, mock_create_bucket, mock_find_bucket, mock_read
):
thlogger = init_logger()
thlogger.work(max_iterations=1)
assert mock_read.called
assert mock_find_bucket.called
assert mock_create_bucket.called
assert mock_write.called
assert len(thlogger.measurements) == 0
@patch("Adafruit_DHT.read_retry", return_value=(None, None))
@patch("influxdb_client.BucketsApi.find_bucket_by_name", return_value=True)
@patch("influxdb_client.BucketsApi.create_bucket", return_value=True)
@patch("influxdb_client.WriteApi.write", return_value=None)
def test_handle_failed_read(
mock_write, mock_create_bucket, mock_find_bucket, mock_read
):
thlogger = init_logger()
thlogger.work(max_iterations=2)
assert mock_read.call_count == 2
assert mock_find_bucket.called
assert not mock_create_bucket.called
assert mock_write.call_count == 0
assert len(thlogger.measurements) == 0
@patch("Adafruit_DHT.read_retry", return_value=(50, 20))
@patch("influxdb_client.BucketsApi.find_bucket_by_name", return_value=True)
@patch("influxdb_client.BucketsApi.create_bucket", return_value=True)
@patch("influxdb_client.WriteApi.write", side_effect=raise_exception)
def test_handle_failed_write(
mock_write, mock_create_bucket, mock_find_bucket, mock_read
):
thlogger = init_logger()
thlogger.work(max_iterations=2)
assert mock_find_bucket.call_count == 1
assert not mock_create_bucket.called
assert mock_read.call_count == 2
assert mock_write.call_count == 2
assert len(thlogger.measurements) == 2
@patch("Adafruit_DHT.read_retry", return_value=(50, 20))
@patch("influxdb_client.BucketsApi.find_bucket_by_name", return_value=True)
@patch("influxdb_client.BucketsApi.create_bucket", return_value=True)
@patch("influxdb_client.WriteApi.write", side_effect=raise_exception)
@patch("thlogger.THLogger.restart_networking", return_value=None)
def test_write_failure_threshold(
mock_restart_networking,
mock_write,
mock_create_bucket,
mock_find_bucket,
mock_read,
):
thlogger = init_logger()
thlogger.work(max_iterations=2, write_failure_threshold=1)
assert mock_find_bucket.call_count == 1
assert not mock_create_bucket.called
assert mock_read.call_count == 2
assert mock_write.call_count == 2
assert len(thlogger.measurements) == 2
assert mock_restart_networking.call_count == 1
@patch("Adafruit_DHT.read_retry", return_value=(50, 20))
@patch("influxdb_client.BucketsApi.find_bucket_by_name", return_value=True)
@patch("influxdb_client.BucketsApi.create_bucket", return_value=True)
@patch("thlogger.THLogger.write_measurements", side_effect=raise_keyboardinterrupt)
def test_keyboard_interrupt(
mock_write, mock_create_bucket, mock_find_bucket, mock_read
):
thlogger = init_logger()
with pytest.raises(KeyboardInterrupt):
thlogger.work(max_iterations=2)
@patch("Adafruit_DHT.read_retry", return_value=(50, 20))
@patch("influxdb_client.BucketsApi.find_bucket_by_name", return_value=True)
@patch("influxdb_client.BucketsApi.create_bucket", return_value=True)
@patch("thlogger.THLogger.write_measurements", side_effect=raise_exception)
def test_other_exception_handling(
mock_write, mock_create_bucket, mock_find_bucket, mock_read
):
thlogger = init_logger()
thlogger.work(max_iterations=2)
assert mock_read.call_count == 2
assert mock_find_bucket.called
assert not mock_create_bucket.called
assert mock_write.call_count == 2
assert len(thlogger.measurements) == 2
@patch("Adafruit_DHT.read_retry", return_value=(50, 20))
@patch(
"influxdb_client.BucketsApi.find_bucket_by_name",
side_effect=conditionally_raise_connectionerror,
)
@patch("influxdb_client.BucketsApi.create_bucket", return_value=True)
@patch("influxdb_client.WriteApi.write", return_value=None)
@patch("subprocess.call", side_effect=set_network_restarted)
def test_network_restart(
mock_system_call,
mock_write,
mock_create_bucket,
mock_find_bucket,
mock_read,
):
thlogger = init_logger()
thlogger.work(max_iterations=1)
assert mock_find_bucket.call_count == 2
assert mock_create_bucket.call_count == 0
assert mock_system_call.call_count == 2