Skip to content

Commit e5ef2a7

Browse files
committed
Use different methods instead of just one for tests
1 parent 5634d53 commit e5ef2a7

File tree

1 file changed

+99
-65
lines changed

1 file changed

+99
-65
lines changed

tests/test_modelchain.py

Lines changed: 99 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
"""
2+
The ``modelchain`` module contains functions and classes of the
3+
windpowerlib. This module makes it easy to get started with the windpowerlib
4+
and demonstrates standard ways to use the library.
5+
6+
"""
7+
8+
__copyright__ = "Copyright oemof developer group"
9+
__license__ = "GPLv3"
10+
111
import pandas as pd
212
import numpy as np
313
import pytest
@@ -11,14 +21,29 @@ class TestModelChain:
1121

1222
@classmethod
1323
def setup_class(self):
24+
"""Setup default values"""
1425
self.test_turbine = {'hub_height': 100,
1526
'turbine_type': 'E-126/4200',
1627
'power_curve': True}
1728

29+
temperature_2m = np.array([[267], [268]])
30+
temperature_10m = np.array([[267], [266]])
31+
pressure_0m = np.array([[101125], [101000]])
32+
wind_speed_8m = np.array([[4.0], [5.0]])
33+
wind_speed_10m = np.array([[5.0], [6.5]])
34+
roughness_length = np.array([[0.15], [0.15]])
35+
self.weather_df = pd.DataFrame(
36+
np.hstack((temperature_2m, temperature_10m, pressure_0m,
37+
wind_speed_8m, wind_speed_10m, roughness_length)),
38+
index=[0, 1],
39+
columns=[np.array(['temperature', 'temperature', 'pressure',
40+
'wind_speed', 'wind_speed',
41+
'roughness_length']),
42+
np.array([2, 10, 0, 8, 10, 0])])
43+
1844
def test_temperature_hub(self):
1945
# Test modelchain with temperature_model='linear_gradient'
20-
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
21-
temperature_model='linear_gradient')
46+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine))
2247
# Test modelchain with temperature_model='interpolation_extrapolation'
2348
test_mc_2 = mc.ModelChain(
2449
wt.WindTurbine(**self.test_turbine),
@@ -56,8 +81,7 @@ def test_temperature_hub(self):
5681

5782
def test_density_hub(self):
5883
# Test modelchain with density_model='barometric'
59-
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
60-
density_model='barometric')
84+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine))
6185
# Test modelchain with density_model='ideal_gas'
6286
test_mc_2 = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
6387
density_model='ideal_gas')
@@ -114,8 +138,7 @@ def test_density_hub(self):
114138

115139
def test_wind_speed_hub(self):
116140
# Test modelchain with wind_speed_model='logarithmic'
117-
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
118-
wind_speed_model='logarithmic')
141+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine))
119142
# Test modelchain with wind_speed_model='hellman'
120143
test_mc_2 = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
121144
wind_speed_model='hellman')
@@ -159,42 +182,25 @@ def test_wind_speed_hub(self):
159182
v_wind_exp = pd.Series(data=[5.0, 6.5], name=100)
160183
assert_series_equal(test_mc.wind_speed_hub(weather_df), v_wind_exp)
161184

162-
def test_run_model(self):
163-
164-
temperature_2m = np.array([[267], [268]])
165-
temperature_10m = np.array([[267], [266]])
166-
pressure_0m = np.array([[101125], [101000]])
167-
wind_speed_8m = np.array([[4.0], [5.0]])
168-
wind_speed_10m = np.array([[5.0], [6.5]])
169-
roughness_length = np.array([[0.15], [0.15]])
170-
weather_df = pd.DataFrame(np.hstack((temperature_2m,
171-
temperature_10m,
172-
pressure_0m,
173-
wind_speed_8m,
174-
wind_speed_10m,
175-
roughness_length)),
176-
index=[0, 1],
177-
columns=[np.array(['temperature',
178-
'temperature',
179-
'pressure',
180-
'wind_speed',
181-
'wind_speed',
182-
'roughness_length']),
183-
np.array([2, 10, 0, 8, 10, 0])])
185+
# ***** test_run_model *********
184186

187+
def test_with_default_parameter(self):
188+
"""Test with default parameters of modelchain (power curve)"""
185189
test_turbine = {'hub_height': 100,
186190
'rotor_diameter': 80,
187191
'turbine_type': 'E-126/4200'}
188-
189-
# Test with default parameters of modelchain (power curve)
190192
power_output_exp = pd.Series(data=[1637405.4840444783,
191193
3154438.3894902095],
192194
name='feedin_power_plant')
193195
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine))
194-
test_mc.run_model(weather_df)
196+
test_mc.run_model(self.weather_df)
195197
assert_series_equal(test_mc.power_output, power_output_exp)
196198

197-
# Test with density corrected power curve and hellman
199+
def test_with_density_corrected_power_curve_and_hellman(self):
200+
"""Test with density corrected power curve and hellman"""
201+
test_turbine = {'hub_height': 100,
202+
'rotor_diameter': 80,
203+
'turbine_type': 'E-126/4200'}
198204
test_modelchain = {'wind_speed_model': 'hellman',
199205
'power_output_model': 'power_curve',
200206
'density_correction': True}
@@ -203,10 +209,14 @@ def test_run_model(self):
203209
name='feedin_power_plant')
204210
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
205211
**test_modelchain)
206-
test_mc.run_model(weather_df)
212+
test_mc.run_model(self.weather_df)
207213
assert_series_equal(test_mc.power_output, power_output_exp)
208214

209-
# Test with power coefficient curve and hellman
215+
def test_with_power_coefficient_curve_and_hellman(self):
216+
"""Test with power coefficient curve and hellman"""
217+
test_turbine = {'hub_height': 100,
218+
'rotor_diameter': 80,
219+
'turbine_type': 'E-126/4200'}
210220
power_output_exp = pd.Series(data=[534137.5112701517,
211221
1103611.1736067757],
212222
name='feedin_power_plant')
@@ -215,58 +225,82 @@ def test_run_model(self):
215225
'density_correction': False}
216226
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
217227
**test_modelchain)
218-
test_mc.run_model(weather_df)
228+
test_mc.run_model(self.weather_df)
219229
assert_series_equal(test_mc.power_output, power_output_exp)
220230

221-
# Raise ValueErrors due to wrong spelling of parameters
231+
def test_wrong_spelling_power_output_model(self):
232+
"""Raise ValueErrors due to wrong spelling of power_output_model"""
222233
with pytest.raises(ValueError):
223-
test_modelchain['power_output_model'] = 'wrong_spelling'
224-
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
234+
test_modelchain = {'wind_speed_model': 'hellman',
235+
'power_output_model': 'wrong_spelling',
236+
'density_correction': False}
237+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
225238
**test_modelchain)
226-
test_mc.run_model(weather_df)
239+
test_mc.run_model(self.weather_df)
240+
241+
def test_wrong_spelling_density_model(self):
242+
"""Raise ValueErrors due to wrong spelling of density_model"""
227243
with pytest.raises(ValueError):
228-
test_modelchain['density_model'] = 'wrong_spelling'
229-
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
244+
test_modelchain = {'wind_speed_model': 'hellman',
245+
'power_output_model': 'power_coefficient_curve',
246+
'density_correction': False,
247+
'density_model': 'wrong_spelling'}
248+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
230249
**test_modelchain)
231-
test_mc.run_model(weather_df)
250+
test_mc.run_model(self.weather_df)
251+
252+
def test_wrong_spelling_temperature_model(self):
253+
"""Raise ValueErrors due to wrong spelling of temperature_model"""
232254
with pytest.raises(ValueError):
233-
test_modelchain['temperature_model'] = 'wrong_spelling'
234-
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
255+
test_modelchain = {'wind_speed_model': 'hellman',
256+
'power_output_model': 'power_coefficient_curve',
257+
'density_correction': False,
258+
'temperature_model': 'wrong_spelling'}
259+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
235260
**test_modelchain)
236-
test_mc.run_model(weather_df)
261+
test_mc.run_model(self.weather_df)
262+
263+
def test_wrong_spelling_wind_speed_model(self):
264+
"""Raise ValueErrors due to wrong spelling of wind_speed_model"""
237265
with pytest.raises(ValueError):
238-
test_modelchain['wind_speed_model'] = 'wrong_spelling'
239-
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
266+
test_modelchain = {'wind_speed_model': 'wrong_spelling',
267+
'power_output_model': 'power_coefficient_curve',
268+
'density_correction': False}
269+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
240270
**test_modelchain)
241-
test_mc.run_model(weather_df)
271+
test_mc.run_model(self.weather_df)
242272

243-
# Raise TypeErrors due to wrong type of `density_correction`
273+
def test_wrong_density_correction_type(self):
274+
"""Raise TypeErrors due to wrong type of `density_correction`"""
244275
with pytest.raises(TypeError):
245276
test_modelchain = {'power_output_model': 'power_curve',
246277
'density_correction': 'wrong_type'}
247-
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
278+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
248279
**test_modelchain)
249-
test_mc.run_model(weather_df)
280+
test_mc.run_model(self.weather_df)
250281

251-
# Raise TypeErrors due to missing cp- or p-values
252-
# with pytest.raises(TypeError):
282+
def test_missing_cp_values(self):
283+
"""Raise TypeErrors due to missing cp-values"""
253284
with pytest.raises(TypeError):
254-
test_turbine = {'hub_height': 100,
255-
'rotor_diameter': 80,
256-
'turbine_type': 'E-126/4200',
257-
'power_coefficient_curve': 'True'}
285+
self.test_turbine = {'hub_height': 100,
286+
'rotor_diameter': 80,
287+
'turbine_type': 'E-126/4200',
288+
'power_coefficient_curve': False}
258289
test_modelchain = {'power_output_model': 'power_coefficient_curve',
259290
'density_correction': True}
260-
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
291+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
261292
**test_modelchain)
262-
test_mc.run_model(weather_df)
293+
test_mc.run_model(self.weather_df)
294+
295+
def test_missing_p_values(self):
296+
"""Raise TypeErrors due to missing p-values"""
263297
with pytest.raises(TypeError):
264-
test_turbine = {'hub_height': 100,
265-
'rotor_diameter': 80,
266-
'turbine_type': 'E-126/4200',
267-
'power_curve': 'True'}
298+
self.test_turbine = {'hub_height': 100,
299+
'rotor_diameter': 80,
300+
'turbine_type': 'E-126/4200',
301+
'power_curve': False}
268302
test_modelchain = {'power_output_model': 'power_curve',
269303
'density_corr': True}
270-
test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine),
304+
test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine),
271305
**test_modelchain)
272-
test_mc.run_model(weather_df)
306+
test_mc.run_model(self.weather_df)

0 commit comments

Comments
 (0)