Newer
Older
import unittest
import numpy as np
import pandas as pd
from aosstower.level_00.influxdb import Updater, construct_url, get_url_data
class TestDataCase:
def __init__(self, input_data, expected_avg, expected_url):
self.input_data = input_data
self.expected_avg = expected_avg
self.expected_url = expected_url
def create_data(size, data_interval=datetime.timedelta(seconds=5)):
return [
{
"wind_speed": i % 119,
"wind_dir": 0,
"box_pressure": i,
"paro_air_temp_period": i,
"paro_pressure_period": i,
"paro_air_temp": i,
"pressure": i,
"paro_cal_sig": i,
"box_air_temp": i,
"air_temp_2": i,
"air_temp_3": i,
"air_temp_4": i,
"rh_shield_freq": i,
"rel_hum": i,
"air_temp_6_3m": i,
"dewpoint": i,
"rtd_shied_freq": i,
"air_temp": i,
"solar_flux": i,
"precip": i,
"accum_precip": -99999,
"altimeter": i,
"timestamp": datetime.datetime(2019, 1, 1, 0, 3, 33) + data_interval * i,
}
for i in range(size)
]
def _isnan(x):
return isinstance(x, float) and np.isnan(x)
class TestInfluxdb(unittest.TestCase):
def setUp(self):
self.updater = Updater()
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
create_data(209),
[
{
"wind_speed": 17,
"wind_dir": 0,
"box_pressure": 17,
"paro_air_temp_period": 17,
"paro_pressure_period": 17,
"paro_air_temp": 17,
"pressure": 17,
"paro_cal_sig": 17,
"box_air_temp": 17,
"air_temp_2": 17,
"air_temp_3": 17,
"air_temp_4": 17,
"rh_shield_freq": 17,
"rel_hum": 17,
"air_temp_6_3m": 17,
"dewpoint": 17,
"rtd_shied_freq": 17,
"air_temp": 17,
"solar_flux": 17,
"precip": 17,
"accum_precip": np.nan,
"altimeter": 17,
"wind_east": 0,
"wind_north": 17,
"wind_speed_2m": np.nan,
"wind_dir_2m": np.nan,
"gust_1m": np.nan,
"gust_10m": np.nan,
"index": pd.Timestamp("2019-01-01 00:04:58"),
},
{
"wind_speed": 77,
"wind_dir": 0,
"box_pressure": 77,
"paro_air_temp_period": 77,
"paro_pressure_period": 77,
"paro_air_temp": 77,
"pressure": 77,
"paro_cal_sig": 77,
"box_air_temp": 77,
"air_temp_2": 77,
"air_temp_3": 77,
"air_temp_4": 77,
"rh_shield_freq": 77,
"rel_hum": 77,
"air_temp_6_3m": 77,
"dewpoint": 77,
"rtd_shied_freq": 77,
"air_temp": 77,
"solar_flux": 77,
"precip": 77,
"accum_precip": np.nan,
"altimeter": 77,
"wind_east": 0,
"wind_north": 77,
"wind_speed_2m": 65.5,
"wind_dir_2m": 0,
"gust_1m": 77,
"gust_10m": np.nan,
"index": pd.Timestamp("2019-01-01 00:09:58"),
},
{
"wind_speed": 18,
"wind_dir": 0,
"box_pressure": 137,
"paro_air_temp_period": 137,
"paro_pressure_period": 137,
"paro_air_temp": 137,
"pressure": 137,
"paro_cal_sig": 137,
"box_air_temp": 137,
"air_temp_2": 137,
"air_temp_3": 137,
"air_temp_4": 137,
"rh_shield_freq": 137,
"rel_hum": 137,
"air_temp_6_3m": 137,
"dewpoint": 137,
"rtd_shied_freq": 137,
"air_temp": 137,
"solar_flux": 137,
"precip": 137,
"accum_precip": np.nan,
"altimeter": 137,
"wind_east": 0,
"wind_north": 18,
"wind_speed_2m": 31.291666666666668,
"wind_dir_2m": 0,
"gust_1m": np.nan,
"gust_10m": np.nan,
"index": pd.Timestamp("2019-01-01 00:14:58"),
},
{
"wind_speed": 78,
"wind_dir": 0,
"box_pressure": 197,
"paro_air_temp_period": 197,
"paro_pressure_period": 197,
"paro_air_temp": 197,
"pressure": 197,
"paro_cal_sig": 197,
"box_air_temp": 197,
"air_temp_2": 197,
"air_temp_3": 197,
"air_temp_4": 197,
"rh_shield_freq": 197,
"rel_hum": 197,
"air_temp_6_3m": 197,
"dewpoint": 197,
"rtd_shied_freq": 197,
"air_temp": 197,
"solar_flux": 197,
"precip": 197,
"accum_precip": np.nan,
"altimeter": 197,
"wind_east": 0,
"wind_north": 78,
"wind_speed_2m": 66.5,
"wind_dir_2m": 0,
"gust_1m": 78,
"gust_10m": 118,
"index": pd.Timestamp("2019-01-01 00:19:58"),
},
],
[
"http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?"
"ID=&PASSWORD=&dateutc=2019-01-01+00%3A04%3A58&winddir=0.0&winddir_avg2m="
"&windspeedmph=38.02798&windspdmph_avg2m=&windgustmph=&windgustmph_10m="
"&humidity=17.0&tempf=62.6&baromin=0.5020096628001095&dewptf=62.6"
"&solarradiation=17.0&rainin=17.0&dailyrainin=&softwaretype=SSEC-RIG"
"&action=updateraw",
"http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?"
"ID=&PASSWORD=&dateutc=2019-01-01+00%3A09%3A58&winddir=0.0&winddir_avg2m=0.0"
"&windspeedmph=172.24438&windspdmph_avg2m=146.51957000000002&windgustmph=172.24438"
"&windgustmph_10m=&humidity=77.0&tempf=170.6&baromin=2.2738084726828487"
"&dewptf=170.6&solarradiation=77.0&rainin=77.0&dailyrainin=&softwaretype=SSEC-RIG"
"&action=updateraw",
"http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?"
"ID=&PASSWORD=&dateutc=2019-01-01+00%3A14%3A58&winddir=0.0&winddir_avg2m=0.0"
"&windspeedmph=40.264920000000004&windspdmph_avg2m=69.99758083333334&windgustmph="
"&windgustmph_10m=&humidity=137.0&tempf=278.6&baromin=4.045607282565588"
"&dewptf=278.6&solarradiation=137.0&rainin=137.0&dailyrainin=&softwaretype=SSEC-RIG"
"&action=updateraw",
"http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?"
"ID=&PASSWORD=&dateutc=2019-01-01+00%3A19%3A58&winddir=0.0&winddir_avg2m=0.0"
"&windspeedmph=174.48132&windspdmph_avg2m=148.75651000000002&windgustmph=174.48132"
"&windgustmph_10m=263.95892000000003&humidity=197.0&tempf=386.6"
"&baromin=5.817406092448327&dewptf=386.6&solarradiation=197.0&rainin=197.0"
"&dailyrainin=&softwaretype=SSEC-RIG&action=updateraw",
],
)
def test_updater(self):
output = []
avg = self.updater.rolling_average(record)
if avg is not None:
output.append({key: avg[key][-1] for key in avg})
output[-1]["index"] = avg.index[-1]
assert len(self.test_data.expected_avg) >= len(output)
assert len(self.test_data.expected_avg[len(output) - 1]) == len(output[-1])
for key in output[-1]:
if not (_isnan(output[-1][key]) and _isnan(self.test_data.expected_avg[len(output) - 1][key])):
assert self.test_data.expected_avg[len(output) - 1][key] == output[-1][key]
assert len(self.test_data.expected_avg) == len(output)
def test_construct_url(self):
output = []
avg = self.updater.rolling_average(record)
if avg is not None:
output.append(construct_url(get_url_data(avg, "", "")))
assert len(self.test_data.expected_url) >= len(output)
assert self.test_data.expected_url[len(output) - 1] == output[-1]
assert len(self.test_data.expected_url) == len(output)