/
test_latitude_longitude.py
84 lines (75 loc) · 2.45 KB
/
test_latitude_longitude.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
import pytest
from flexmeasures.data.schemas.assets import LatitudeField, LongitudeField
from flexmeasures.data.schemas.utils import ValidationError
@pytest.mark.parametrize(
("input", "exp_deserialization"),
[
(0, 0),
(0.1234567, 0.1234567),
(-90, -90),
(90, 90),
],
)
def test_latitude(input, exp_deserialization):
"""Testing straightforward cases"""
lf = LatitudeField()
deser = lf.deserialize(input, None, None)
assert deser == exp_deserialization
assert lf.serialize("duration", {"duration": deser}) == round(input, 7)
@pytest.mark.parametrize(
("input", "error_messages"),
[
("ninety", ["Not a valid number."]),
(90.01, ["Latitude 90.01 exceeds the maximum latitude of 90 degrees."]),
(0.12345678, ["Latitudes and longitudes are limited to 7 decimal places."]),
(
-90.00000001,
[
"Latitude -90.00000001 exceeds the minimum latitude of -90 degrees.",
"Latitudes and longitudes are limited to 7 decimal places.",
],
),
],
)
def test_latitude_field_invalid(input, error_messages):
lf = LatitudeField()
with pytest.raises(ValidationError) as ve:
lf.deserialize(input, None, None)
assert error_messages == ve.value.messages
@pytest.mark.parametrize(
("input", "exp_deserialization"),
[
(0, 0),
(0.1234567, 0.1234567),
(-180, -180),
(180, 180),
],
)
def test_longitude(input, exp_deserialization):
"""Testing straightforward cases"""
lf = LongitudeField()
deser = lf.deserialize(input, None, None)
assert deser == exp_deserialization
assert lf.serialize("duration", {"duration": deser}) == round(input, 7)
@pytest.mark.parametrize(
("input", "error_messages"),
[
("one-hundred-and-eighty", ["Not a valid number."]),
(
-180.01,
["Longitude -180.01 exceeds the minimum longitude of -180 degrees."],
),
(
-180.00000001,
[
"Longitude -180.00000001 exceeds the minimum longitude of -180 degrees.",
"Latitudes and longitudes are limited to 7 decimal places.",
],
),
],
)
def test_longitude_field_invalid(input, error_messages):
lf = LongitudeField()
with pytest.raises(ValidationError) as ve:
lf.deserialize(input, None, None)
assert error_messages == ve.value.messages