/
test_unit_utils.py
161 lines (141 loc) · 4.42 KB
/
test_unit_utils.py
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
from datetime import timedelta
import pint.errors
import pytest
import pandas as pd
from flexmeasures.utils.unit_utils import (
convert_units,
determine_flow_unit,
determine_stock_unit,
determine_unit_conversion_multiplier,
units_are_convertible,
is_energy_unit,
is_power_unit,
ur,
)
@pytest.mark.parametrize(
"from_unit, to_unit, expected_multiplier, expected_values",
[
("m/s", "km/h", 3.6, None),
("m³/h", "l/h", 1000, None),
("m³", "m³/h", 4, None),
("MW", "kW", 1000, None),
("kWh", "kW", 4, None),
("°C", "K", None, [273.15, 283.15, 284.15]),
],
)
def test_convert_unit(
from_unit,
to_unit,
expected_multiplier,
expected_values,
):
"""Check some common unit conversions.
Note that for the above expectations:
- conversion from kWh to kW, and from m³ to m³/h, both depend on the event resolution set below
- conversion from °C to K depends on the data values set below
"""
data = pd.Series([0, 10.0, 11.0])
converted_data: pd.Series = convert_units(
data=data,
from_unit=from_unit,
to_unit=to_unit,
event_resolution=timedelta(minutes=15),
)
if expected_multiplier is not None:
expected_data = data * expected_multiplier
else:
expected_data = pd.Series(expected_values)
pd.testing.assert_series_equal(converted_data, expected_data)
@pytest.mark.parametrize(
"unit, time_unit, expected_unit",
[
("m³", None, "m³/h"),
("kWh", None, "kW"),
("km", "h", "km/h"),
("m", "s", "km/h"),
],
)
def test_determine_flow_unit(
unit,
time_unit,
expected_unit,
):
if time_unit is None:
assert determine_flow_unit(unit) == expected_unit
else:
assert determine_flow_unit(unit, time_unit) == expected_unit
@pytest.mark.parametrize(
"unit, time_unit, expected_unit",
[
("m³/h", None, "m³"),
("kW", None, "kWh"),
("m/s", "s", "m"),
("m/s", "h", "km"),
],
)
def test_determine_stock_unit(
unit,
time_unit,
expected_unit,
):
if time_unit is None:
assert determine_stock_unit(unit) == expected_unit
else:
assert determine_stock_unit(unit, time_unit) == expected_unit
def test_determine_unit_conversion_multiplier():
assert determine_unit_conversion_multiplier("kW", "W") == 1000
assert determine_unit_conversion_multiplier("J/s", "W") == 1
assert determine_unit_conversion_multiplier("Wh", "W", timedelta(minutes=10)) == 6
assert determine_unit_conversion_multiplier("kWh", "MJ") == 3.6
with pytest.raises(pint.errors.OffsetUnitCalculusError):
# Not a conversion that can be specified as a multiplication
determine_unit_conversion_multiplier("°C", "K")
def test_h_denotes_hour_and_not_planck_constant():
assert ur.Quantity("h").dimensionality == ur.Quantity("hour").dimensionality
assert (
ur.Quantity("hbar").dimensionality
== ur.Quantity("planck_constant").dimensionality
)
def test_units_are_convertible():
assert units_are_convertible("kW", "W") # units just have different prefixes
assert units_are_convertible(
"J/s", "W"
) # units can be converted using some multiplier
assert units_are_convertible(
"Wh", "W"
) # units that represent a stock delta can, knowing the duration, be converted to a flow
assert units_are_convertible("toe", "W") # tonne of oil equivalent
assert units_are_convertible("°C", "K") # offset unit to absolute unit
assert not units_are_convertible("°C", "W")
assert not units_are_convertible("EUR/MWh", "W")
assert not units_are_convertible("not-a-unit", "W")
@pytest.mark.parametrize(
"unit, power_unit",
[
("EUR/MWh", False),
("KRW/kWh", False),
("kWh", False),
("kW", True),
("watt", True),
("°C", False),
("", False),
("not-a-unit", False),
],
)
def test_is_power_unit(unit: str, power_unit: bool):
assert is_power_unit(unit) is power_unit
@pytest.mark.parametrize(
"unit, energy_unit",
[
("EUR/MWh", False),
("KRW/kWh", False),
("kWh", True),
("kW", False),
("watthour", True),
("°C", False),
("", False),
("not-a-unit", False),
],
)
def test_is_energy_unit(unit: str, energy_unit: bool):
assert is_energy_unit(unit) is energy_unit