/
test_sensor_data_schema.py
112 lines (101 loc) · 2.55 KB
/
test_sensor_data_schema.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
from datetime import timedelta
import pytest
from marshmallow import ValidationError
from flexmeasures.api.common.schemas.sensor_data import (
SingleValueField,
PostSensorDataSchema,
GetSensorDataSchema,
)
@pytest.mark.parametrize(
"deserialization_input, exp_deserialization_output",
[
(
"PT1H",
timedelta(hours=1),
),
(
"PT15M",
timedelta(minutes=15),
),
],
)
def test_resolution_field_deserialization(
deserialization_input,
exp_deserialization_output,
):
"""Check parsing the resolution field of the GetSensorDataSchema schema.
These particular ISO durations are expected to be parsed as python timedeltas.
"""
# todo: extend test cases with some nominal durations when timely-beliefs supports these
# see https://github.com/SeitaBV/timely-beliefs/issues/13
vf = GetSensorDataSchema._declared_fields["resolution"]
deser = vf.deserialize(deserialization_input)
assert deser == exp_deserialization_output
@pytest.mark.parametrize(
"deserialization_input, exp_deserialization_output",
[
(
1,
[1],
),
(
2.7,
[2.7],
),
(
[1],
[1],
),
(
[2.7],
[2.7],
),
],
)
def test_value_field_deserialization(
deserialization_input,
exp_deserialization_output,
):
"""Testing straightforward cases"""
vf = PostSensorDataSchema._declared_fields["values"]
deser = vf.deserialize(deserialization_input)
assert deser == exp_deserialization_output
@pytest.mark.parametrize(
"serialization_input, exp_serialization_output",
[
(
1,
[1],
),
(
2.7,
[2.7],
),
],
)
def test_value_field_serialization(
serialization_input,
exp_serialization_output,
):
"""Testing straightforward cases"""
vf = SingleValueField()
ser = vf.serialize("values", {"values": serialization_input})
assert ser == exp_serialization_output
@pytest.mark.parametrize(
"deserialization_input, error_msg",
[
(
["three", 4],
"Not a valid number",
),
(
"3, 4",
"Not a valid number",
),
],
)
def test_value_field_invalid(deserialization_input, error_msg):
sf = SingleValueField()
with pytest.raises(ValidationError) as ve:
sf.deserialize(deserialization_input)
assert error_msg in str(ve)