test_SensorsToShowSchema.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import pytest
  2. from marshmallow import ValidationError
  3. from flexmeasures.data.schemas.generic_assets import SensorsToShowSchema
  4. def test_single_sensor_id():
  5. schema = SensorsToShowSchema()
  6. input_value = [42]
  7. expected_output = [{"title": None, "sensors": [42]}]
  8. assert schema.deserialize(input_value) == expected_output
  9. def test_list_of_sensor_ids():
  10. schema = SensorsToShowSchema()
  11. input_value = [42, 43]
  12. expected_output = [
  13. {"title": None, "sensors": [42]},
  14. {"title": None, "sensors": [43]},
  15. ]
  16. assert schema.deserialize(input_value) == expected_output
  17. def test_dict_with_title_and_single_sensor():
  18. schema = SensorsToShowSchema()
  19. input_value = [{"title": "Temperature", "sensor": 42}]
  20. expected_output = [{"title": "Temperature", "sensors": [42]}]
  21. assert schema.deserialize(input_value) == expected_output
  22. def test_dict_with_title_and_multiple_sensors():
  23. schema = SensorsToShowSchema()
  24. input_value = [{"title": "Pressure", "sensors": [42, 43]}]
  25. expected_output = [{"title": "Pressure", "sensors": [42, 43]}]
  26. assert schema.deserialize(input_value) == expected_output
  27. def test_invalid_sensor_string_input():
  28. schema = SensorsToShowSchema()
  29. with pytest.raises(
  30. ValidationError,
  31. match="Invalid item type in 'sensors_to_show'. Expected int, list, or dict.",
  32. ):
  33. schema.deserialize(["invalid_string"])
  34. def test_invalid_sensor_in_list():
  35. schema = SensorsToShowSchema()
  36. input_value = [{"title": "Test", "sensors": [42, "invalid"]}]
  37. with pytest.raises(
  38. ValidationError, match="'sensors' value must be a list of integers."
  39. ):
  40. schema.deserialize(input_value)
  41. def test_invalid_sensor_dict_without_sensors_key():
  42. schema = SensorsToShowSchema()
  43. input_value = [{"title": "Test", "something_else": 42}]
  44. with pytest.raises(
  45. ValidationError,
  46. match="Dictionary must contain either 'sensor' or 'sensors' key.",
  47. ):
  48. schema.deserialize(input_value)
  49. def test_mixed_valid_inputs():
  50. schema = SensorsToShowSchema()
  51. input_value = [
  52. {"title": "Test", "sensors": [1, 2]},
  53. {"title": None, "sensors": [3, 4]},
  54. 5,
  55. ]
  56. expected_output = [
  57. {"title": "Test", "sensors": [1, 2]},
  58. {"title": None, "sensors": [3, 4]},
  59. {"title": None, "sensors": [5]},
  60. ]
  61. assert schema.deserialize(input_value) == expected_output
  62. def test_string_json_input():
  63. schema = SensorsToShowSchema()
  64. input_value = (
  65. '[{"title": "Test", "sensors": [1, 2]}, {"title": "Test2", "sensors": [3]}]'
  66. )
  67. expected_output = [
  68. {"title": "Test", "sensors": [1, 2]},
  69. {"title": "Test2", "sensors": [3]},
  70. ]
  71. assert schema.deserialize(input_value) == expected_output
  72. # New test cases for misspelled or missing title and mixed sensor/sensors formats
  73. def test_dict_missing_title_key():
  74. schema = SensorsToShowSchema()
  75. input_value = [{"sensor": 42}]
  76. with pytest.raises(ValidationError, match="Dictionary must contain a 'title' key."):
  77. schema.deserialize(input_value)
  78. def test_dict_misspelled_title_key():
  79. schema = SensorsToShowSchema()
  80. input_value = [{"titel": "Temperature", "sensor": 42}] # Misspelled 'title'
  81. with pytest.raises(ValidationError, match="Dictionary must contain a 'title' key."):
  82. schema.deserialize(input_value)
  83. def test_dict_with_sensor_as_list():
  84. schema = SensorsToShowSchema()
  85. input_value = [{"title": "Temperature", "sensor": [42]}]
  86. with pytest.raises(ValidationError, match="'sensor' value must be an integer."):
  87. schema.deserialize(input_value)
  88. def test_dict_with_sensors_as_int():
  89. schema = SensorsToShowSchema()
  90. input_value = [
  91. {"title": "Temperature", "sensors": 42}
  92. ] # 'sensors' should be a list, not an int
  93. with pytest.raises(
  94. ValidationError, match="'sensors' value must be a list of integers."
  95. ):
  96. schema.deserialize(input_value)