test_latitude_longitude.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import pytest
  2. from marshmallow.exceptions import ValidationError
  3. from flexmeasures.data.schemas.locations import LatitudeField, LongitudeField
  4. @pytest.mark.parametrize(
  5. ("input", "exp_deserialization"),
  6. [
  7. (0, 0),
  8. (0.1234567, 0.1234567),
  9. (-90, -90),
  10. (90, 90),
  11. ],
  12. )
  13. def test_latitude(input, exp_deserialization):
  14. """Testing straightforward cases"""
  15. lf = LatitudeField()
  16. deser = lf.deserialize(input, None, None)
  17. assert deser == exp_deserialization
  18. assert lf.serialize("duration", {"duration": deser}) == round(input, 7)
  19. @pytest.mark.parametrize(
  20. ("input", "error_messages"),
  21. [
  22. ("ninety", ["Not a valid number."]),
  23. (90.01, ["Latitude 90.01 exceeds the maximum latitude of 90 degrees."]),
  24. (0.12345678, ["Latitudes and longitudes are limited to 7 decimal places."]),
  25. (
  26. -90.00000001,
  27. [
  28. "Latitude -90.00000001 exceeds the minimum latitude of -90 degrees.",
  29. "Latitudes and longitudes are limited to 7 decimal places.",
  30. ],
  31. ),
  32. ],
  33. )
  34. def test_latitude_field_invalid(input, error_messages):
  35. lf = LatitudeField()
  36. with pytest.raises(ValidationError) as ve:
  37. lf.deserialize(input, None, None)
  38. assert error_messages == ve.value.messages
  39. @pytest.mark.parametrize(
  40. ("input", "exp_deserialization"),
  41. [
  42. (0, 0),
  43. (0.1234567, 0.1234567),
  44. (-180, -180),
  45. (180, 180),
  46. ],
  47. )
  48. def test_longitude(input, exp_deserialization):
  49. """Testing straightforward cases"""
  50. lf = LongitudeField()
  51. deser = lf.deserialize(input, None, None)
  52. assert deser == exp_deserialization
  53. assert lf.serialize("duration", {"duration": deser}) == round(input, 7)
  54. @pytest.mark.parametrize(
  55. ("input", "error_messages"),
  56. [
  57. ("one-hundred-and-eighty", ["Not a valid number."]),
  58. (
  59. -180.01,
  60. ["Longitude -180.01 exceeds the minimum longitude of -180 degrees."],
  61. ),
  62. (
  63. -180.00000001,
  64. [
  65. "Longitude -180.00000001 exceeds the minimum longitude of -180 degrees.",
  66. "Latitudes and longitudes are limited to 7 decimal places.",
  67. ],
  68. ),
  69. ],
  70. )
  71. def test_longitude_field_invalid(input, error_messages):
  72. lf = LongitudeField()
  73. with pytest.raises(ValidationError) as ve:
  74. lf.deserialize(input, None, None)
  75. assert error_messages == ve.value.messages