Skip to content

Commit 6647a5a

Browse files
committed
Add new tests
1 parent 7897a01 commit 6647a5a

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

tests/test_data_handling.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,72 @@
33
SPDX-License-Identifier: MIT
44
"""
55

6+
import os
7+
from datetime import datetime
8+
from shutil import copyfile
9+
10+
import pandas as pd
11+
import pytest
12+
13+
from windpowerlib.data import check_imported_data, check_data_integretiy
14+
15+
16+
class TestDataCheck:
17+
@classmethod
18+
def setup_class(cls):
19+
cls.path = os.path.join(os.path.dirname(__file__), "oedb")
20+
cls.filename = os.path.join(cls.path, "{0}.csv")
21+
cls.df = pd.read_csv(cls.filename.format("turbine_data"), index_col=0)
22+
cls.time_stamp = datetime.now().strftime("%Y%m%d%H%M%S")
23+
cls.broken_fn = os.path.join(cls.path, "power_curves_broken.csv")
24+
cls.backup_fn = os.path.join(cls.path, "power_curves_backup.csv")
25+
cls.orig_path = os.path.join(os.path.dirname(__file__), os.pardir,
26+
"windpowerlib", "oedb")
27+
cls.orig_fn = os.path.join(cls.orig_path, "power_curves.csv")
28+
29+
@classmethod
30+
def teardown_class(cls):
31+
cls.path = os.path.join(os.path.dirname(__file__), "oedb")
32+
cls.backup_fn = os.path.join(cls.path, "power_curves_backup.csv")
33+
orig_path = os.path.join(os.path.dirname(__file__), os.pardir,
34+
"windpowerlib", "oedb")
35+
cls.orig_fn = os.path.join(orig_path, "power_curves.csv")
36+
copyfile(cls.backup_fn, cls.orig_fn)
37+
for f in os.listdir(cls.path):
38+
if "error" in f:
39+
os.remove(os.path.join(cls.path, f))
40+
41+
def test_normal_data_check(self):
42+
copyfile(
43+
self.filename.format("turbine_data"),
44+
self.filename.format("turbine_data_{0}".format(self.time_stamp)),
45+
)
46+
for curve_type in ["power_curve", "power_coefficient_curve"]:
47+
copyfile(
48+
self.filename.format("{}s".format(curve_type)),
49+
self.filename.format("{0}s_{1}".format(curve_type,
50+
self.time_stamp)),
51+
)
52+
check_imported_data(self.df, self.filename, self.time_stamp)
53+
54+
def test_data_check_logging_warnings(self, caplog):
55+
self.df.loc["GE158/4800", "has_power_curve"] = True
56+
self.df.loc["GE100/2750", "has_cp_curve"] = True
57+
check_data_integretiy(self.df, min_pc_length=26)
58+
assert "E48/800: power_curve is to short (25 values)" in caplog.text
59+
assert "GE158/4800: No power curve" in caplog.text
60+
assert "GE100/2750: No cp-curve but has_cp_curve" in caplog.text
61+
62+
def test_global_error(self):
63+
msg = "Must have equal len keys"
64+
with pytest.raises(ValueError, match=msg):
65+
self.df.loc["GE158/4800", "has_cp_curve"] = [5, 3]
66+
check_imported_data(self.df, self.filename, self.time_stamp)
67+
68+
def test_broken_pwr_curve(self):
69+
copyfile(self.orig_fn, self.backup_fn)
70+
copyfile(self.broken_fn, self.orig_fn)
71+
msg = "could not convert string to float"
72+
with pytest.raises(ValueError, match=msg):
73+
check_imported_data(self.df, self.filename, self.time_stamp)
674

7-
def test_integretiy_check():
8-
pass

windpowerlib/data.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
SPDX-License-Identifier: MIT
66
"""
77

8+
import logging
89
import os
910
import warnings
10-
import logging
1111
from datetime import datetime
1212
from shutil import copyfile
1313

@@ -184,7 +184,7 @@ def store_turbine_data_from_oedb(
184184
"""
185185
turbine_data = fetch_turbine_data_from_oedb(schema=schema, table=table)
186186
# standard file name for saving data
187-
filename = os.path.join(os.path.dirname(__file__), "oedb", "{}.csv")
187+
filename = os.path.join(os.path.dirname(__file__), "oedb", "{0}.csv")
188188

189189
time_stamp = datetime.now().strftime("%Y%m%d%H%M%S")
190190
# get all power (coefficient) curves and save to file
@@ -260,15 +260,10 @@ def store_turbine_data_from_oedb(
260260
def remove_tmp_file(filename, time_stamp):
261261
os.remove(filename.format("turbine_data_{0}".format(time_stamp)))
262262
for curve_type in ["power_curve", "power_coefficient_curve"]:
263-
copyfile(
264-
filename.format("{0}s_{1}".format(curve_type, time_stamp)),
265-
filename.format("{}s".format(curve_type)),
266-
)
267263
os.remove(filename.format("{0}s_{1}".format(curve_type, time_stamp)))
268264

269265

270266
def check_imported_data(data, filename, time_stamp):
271-
data.to_csv(filename.format("turbine_data"))
272267
try:
273268
data = check_data_integretiy(data)
274269
except Exception as e:
@@ -296,7 +291,7 @@ def check_imported_data(data, filename, time_stamp):
296291
return data
297292

298293

299-
def check_data_integretiy(data):
294+
def check_data_integretiy(data, min_pc_length=5):
300295
for dataset in data.iterrows():
301296
ttype = dataset[0]
302297
enercon_e126 = {"turbine_type": "{0}".format(ttype), "hub_height": 135}
@@ -316,8 +311,8 @@ def check_data_integretiy(data):
316311
logging.warning(
317312
"{0}: No cp-curve but has_cp_curve=True.".format(ttype)
318313
)
319-
if dataset[1].has_power_curve is True:
320-
if len(wt.power_curve) < 5:
314+
if wt.power_curve is not None:
315+
if len(wt.power_curve) < min_pc_length:
321316
logging.warning(
322317
"{0}: power_curve is to short ({1} values),".format(
323318
ttype, len(wt.power_curve)

0 commit comments

Comments
 (0)