forked from equinor/tagreader-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.py
More file actions
225 lines (195 loc) · 6.67 KB
/
test_cache.py
File metadata and controls
225 lines (195 loc) · 6.67 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
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
218
219
220
221
222
223
224
225
import pytest
import os
import pandas as pd
from importlib.util import find_spec
from tagreader.utils import ReaderType
from tagreader.cache import SmartCache, safe_tagname
if find_spec("tables") is None:
pytest.skip(
"Cache requires package 'tables'",
allow_module_level=True
)
os.environ["NUMEXPR_MAX_THREADS"] = "8"
@pytest.fixture()
def data():
length = 10
df_total = pd.DataFrame(
{"tag1": range(0, length)},
index=pd.date_range(
start="2018-01-18 05:00:00", freq="60s", periods=length, name="time"
),
)
yield df_total
@pytest.fixture()
def cache(request):
cache = SmartCache("testcache.h5")
yield cache
cache.remove()
def test_safe_tagname():
assert safe_tagname("ASGB.tt-___56_ _%_/_") == "ASGB_tt___56____"
def test_key_path(cache):
pass
def test_cache_single_store_and_fetch(cache, data):
cache.store(data, readtype=ReaderType.INT)
df_read = cache.fetch("tag1", ReaderType.INT, 60)
pd.testing.assert_frame_equal(data, df_read)
def test_cache_multiple_store_single_fetch(cache, data):
df1 = data[0:3]
df2 = data[2:10]
cache.store(df1, readtype=ReaderType.INT)
cache.store(df2, readtype=ReaderType.INT)
df_read = cache.fetch("tag1", ReaderType.INT, 60)
pd.testing.assert_frame_equal(df_read, data)
def test_interval_reads(cache, data):
cache.store(data, readtype=ReaderType.INT)
start_time_oob = pd.to_datetime("2018-01-18 04:55:00")
start_time = pd.to_datetime("2018-01-18 05:05:00")
stop_time = pd.to_datetime("2018-01-18 05:08:00")
stop_time_oob = pd.to_datetime("2018-01-18 06:00:00")
df_read = cache.fetch("tag1", ReaderType.INT, ts=60, start_time=start_time)
pd.testing.assert_frame_equal(data[start_time:], df_read)
df_read = cache.fetch("tag1", ReaderType.INT, ts=60, stop_time=stop_time)
pd.testing.assert_frame_equal(data[:stop_time], df_read)
df_read = cache.fetch("tag1", ReaderType.INT, ts=60, start_time=start_time_oob)
pd.testing.assert_frame_equal(data, df_read)
df_read = cache.fetch("tag1", ReaderType.INT, ts=60, stop_time=stop_time_oob)
pd.testing.assert_frame_equal(data, df_read)
df_read = cache.fetch(
"tag1", ReaderType.INT, ts=60, start_time=start_time, stop_time=stop_time
)
pd.testing.assert_frame_equal(data[start_time:stop_time], df_read)
def test_match_tag(cache):
assert (
cache._match_tag("INT/s60/tag1", readtype=ReaderType.INT, ts=60, tagname="tag1")
is True
)
assert (
cache._match_tag(
"INT/s86401/tag1", readtype=ReaderType.INT, ts=86401, tagname="tag1"
)
is True
)
assert (
cache._match_tag("INT/s60/tag1", readtype=ReaderType.RAW, ts=60, tagname="tag1")
is False
)
assert (
cache._match_tag("INT/s60/tag1", readtype=ReaderType.INT, ts=10, tagname="tag1")
is False
)
assert (
cache._match_tag("INT/s60/tag1", readtype=ReaderType.INT, ts=60, tagname="tag2")
is False
)
assert cache._match_tag("INT/s60/tag1", ts=60, tagname="tag1") is True
assert (
cache._match_tag(
"INT/s60/tag1", readtype=ReaderType.INTERPOLATE, tagname="tag1"
)
is True
)
assert cache._match_tag("INT/s60/tag1", readtype=ReaderType.INT, ts=60) is True
assert (
cache._match_tag(
"INT/s60/tag1",
readtype=[ReaderType.INT, ReaderType.RAW],
ts=[60, 10],
tagname=["tag1", "tag2"],
)
is True
)
assert (
cache._match_tag(
"INT/s60/tag1",
readtype=[ReaderType.AVERAGE, ReaderType.RAW],
ts=[60, 10],
tagname=["tag1", "tag2"],
)
is False
)
assert (
cache._match_tag(
"INT/s60/tag1",
readtype=[ReaderType.INT, ReaderType.RAW],
ts=[120, 10],
tagname=["tag1", "tag2"],
)
is False
)
assert (
cache._match_tag(
"INT/s60/tag1",
readtype=[ReaderType.INT, ReaderType.RAW],
ts=[60, 10],
tagname=["tag3", "tag2"],
)
is False
)
def test_delete_tag(cache, data):
cache.store(data, readtype=ReaderType.INT)
cache.store(data, readtype=ReaderType.RAW)
with cache._get_hdfstore() as f:
assert "INT/s60/tag1" in f
assert "RAW/tag1" in f
cache.delete_key("tag1", ReaderType.INT, 60)
cache.delete_key("tag1")
with cache._get_hdfstore() as f:
assert "INT/s60/tag1" not in f
assert "RAW/tag1" not in f
def test_store_empty_df(cache, data):
# Empty dataframes should not be stored (note: df full of NaN is not empty!)
cache.store(data, readtype=ReaderType.INT)
df = pd.DataFrame({"tag1": []})
cache.store(
df, readtype=ReaderType.INT, ts=60
) # Specify ts to ensure correct key /if/ stored
df_read = cache.fetch("tag1", ReaderType.INT, 60)
pd.testing.assert_frame_equal(data, df_read)
def test_store_metadata(cache):
cache.store_tag_metadata("tag1", {"unit": "%", "desc": "Some description"})
cache.store_tag_metadata("tag1", {"max": 60})
r = cache.fetch_tag_metadata("tag1", "unit")
assert "%" == r["unit"]
r = cache.fetch_tag_metadata("tag1", ["unit", "max", "noworky"])
assert "%" == r["unit"]
assert 60 == r["max"]
assert "noworky" not in r
def test_to_DST_skips_time(cache):
index = pd.date_range(
start="2018-03-25 01:50:00",
end="2018-03-25 03:30:00",
tz="Europe/Oslo",
freq="600s",
name="time",
)
index.freq = None
df = pd.DataFrame({"tag1": range(0, len(index))}, index=index)
assert (
df.loc["2018-03-25 01:50:00":"2018-03-25 03:10:00"].size == (2 + 1 * 6 + 1) - 6
)
cache.store(df, readtype=ReaderType.INT)
df_read = cache.fetch("tag1", ReaderType.INT, 600)
pd.testing.assert_frame_equal(df_read, df)
def test_from_DST_folds_time(cache):
index = pd.date_range(
start="2017-10-29 00:30:00",
end="2017-10-29 04:30:00",
tz="Europe/Oslo",
freq="600s",
name="time",
)
index.freq = None
df = pd.DataFrame({"tag1": range(0, len(index))}, index=index)
assert len(df) == (4 + 1) * 6 + 1
# Time exists inside fold:
assert (
df["tag1"].loc["2017-10-29 01:10:00+02:00":"2017-10-29 01:50:00+02:00"].size
== 5
)
# Time inside fold is always included:
assert (
df.loc["2017-10-29 01:50:00":"2017-10-29 03:10:00"].size == 2 + (1 + 1) * 6 + 1
)
cache.store(df, readtype=ReaderType.INT)
df_read = cache.fetch("tag1", ReaderType.INT, 600)
pd.testing.assert_frame_equal(df_read, df)