|
3 | 3 | SPDX-License-Identifier: MIT |
4 | 4 | """ |
5 | 5 |
|
| 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) |
6 | 74 |
|
7 | | -def test_integretiy_check(): |
8 | | - pass |
|
0 commit comments