test_profit_reporter.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pytest
  2. from datetime import datetime, timedelta
  3. from pytz import timezone
  4. from sqlalchemy import select
  5. from flexmeasures.data.models.reporting.profit import ProfitOrLossReporter
  6. from flexmeasures.data.models.time_series import Sensor
  7. from flexmeasures.tests.utils import get_test_sensor
  8. @pytest.mark.parametrize(
  9. "use_power_sensor, loss_is_positive",
  10. [(False, False), (True, False), (False, True), (True, True)],
  11. )
  12. def test_profit_reporter(app, db, profit_report, use_power_sensor, loss_is_positive):
  13. (
  14. profit_sensor_hourly,
  15. profit_sensor_daily,
  16. power_sensor,
  17. energy_sensor,
  18. ) = profit_report
  19. output_sensor = energy_sensor
  20. if use_power_sensor:
  21. output_sensor = power_sensor
  22. epex_da = get_test_sensor(db)
  23. epex_da_production = db.session.execute(
  24. select(Sensor).filter(Sensor.name == "epex_da_production")
  25. ).scalar_one_or_none()
  26. profit_reporter = ProfitOrLossReporter(
  27. consumption_price_sensor=epex_da,
  28. production_price_sensor=epex_da_production,
  29. loss_is_positive=loss_is_positive,
  30. )
  31. sign = 1.0
  32. if loss_is_positive:
  33. sign = -1.0
  34. tz = timezone("Europe/Amsterdam")
  35. result = profit_reporter.compute(
  36. start=tz.localize(datetime(2015, 1, 3)),
  37. end=tz.localize(datetime(2015, 1, 4)),
  38. input=[dict(sensor=output_sensor)],
  39. output=[
  40. dict(sensor=profit_sensor_hourly),
  41. dict(sensor=profit_sensor_daily),
  42. ],
  43. )
  44. result_hourly = result[0]["data"]
  45. result_daily = result[1]["data"]
  46. assert result_hourly.event_resolution == timedelta(hours=1)
  47. # period of negative prices
  48. # in the period from 00:00 to 03:00
  49. # the device produces 100kWh hourly at a -50 EUR/MWh price
  50. assert (result_hourly[0:4] == -5 * sign).event_value.all()
  51. # in the period from 04:00 to 08:00
  52. # the device consumes 100kWh hourly at a 10 EUR/MWh price
  53. assert (result_hourly[4:8] == 1 * sign).event_value.all()
  54. # period of positive prices
  55. # in the period from 08:00 to 12:00
  56. # the device produces 100kWh hourly at a 60 EUR/MWh price
  57. assert (result_hourly[8:12] == 6 * sign).event_value.all()
  58. # in the period from 12:00 to 16:00
  59. # the device produces 100kWh hourly at a 100 EUR/MWh price
  60. assert (result_hourly[12:16] == -10 * sign).event_value.all()
  61. assert result_daily.event_value.iloc[0] == result_hourly.sum().iloc[0]