test_utils.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from openleadr import utils, objects
  2. from dataclasses import dataclass
  3. import pytest
  4. from datetime import datetime, timezone, timedelta
  5. @dataclass
  6. class dc:
  7. a: int = 2
  8. def test_hasmember():
  9. obj = {'a': 1}
  10. assert utils.hasmember(obj, 'a') == True
  11. assert utils.hasmember(obj, 'b') == False
  12. obj = dc()
  13. assert utils.hasmember(obj, 'a') == True
  14. assert utils.hasmember(obj, 'b') == False
  15. def test_getmember():
  16. obj = {'a': 1}
  17. assert utils.getmember(obj, 'a') == 1
  18. obj = dc()
  19. assert utils.getmember(obj, 'a') == 2
  20. def test_setmember():
  21. obj = {'a': 1}
  22. utils.setmember(obj, 'a', 10)
  23. assert utils.getmember(obj, 'a') == 10
  24. obj = dc()
  25. utils.setmember(obj, 'a', 10)
  26. assert utils.getmember(obj, 'a') == 10
  27. @pytest.mark.asyncio
  28. async def test_delayed_call_with_func():
  29. async def myfunc():
  30. pass
  31. await utils.delayed_call(myfunc, delay=0.1)
  32. @pytest.mark.asyncio
  33. async def test_delayed_call_with_coro():
  34. async def mycoro():
  35. pass
  36. await utils.delayed_call(mycoro(), delay=0.1)
  37. @pytest.mark.asyncio
  38. async def test_delayed_call_with_coro_func():
  39. async def mycoro():
  40. pass
  41. await utils.delayed_call(mycoro, delay=0.1)
  42. def test_determine_event_status_completed():
  43. active_period = {'dtstart': datetime.now(timezone.utc) - timedelta(seconds=10),
  44. 'duration': timedelta(seconds=5)}
  45. assert utils.determine_event_status(active_period) == 'completed'
  46. def test_determine_event_status_active():
  47. active_period = {'dtstart': datetime.now(timezone.utc) - timedelta(seconds=10),
  48. 'duration': timedelta(seconds=15)}
  49. assert utils.determine_event_status(active_period) == 'active'
  50. def test_determine_event_status_near():
  51. active_period = {'dtstart': datetime.now(timezone.utc) + timedelta(seconds=3),
  52. 'duration': timedelta(seconds=5),
  53. 'ramp_up_duration': timedelta(seconds=5)}
  54. assert utils.determine_event_status(active_period) == 'near'
  55. def test_determine_event_status_far():
  56. active_period = {'dtstart': datetime.now(timezone.utc) + timedelta(seconds=10),
  57. 'duration': timedelta(seconds=5)}
  58. assert utils.determine_event_status(active_period) == 'far'
  59. def test_determine_event_status_far_with_ramp_up():
  60. active_period = {'dtstart': datetime.now(timezone.utc) + timedelta(seconds=10),
  61. 'duration': timedelta(seconds=5),
  62. 'ramp_up_duration': timedelta(seconds=5)}
  63. assert utils.determine_event_status(active_period) == 'far'
  64. def test_get_active_period_from_intervals():
  65. now = datetime.now(timezone.utc)
  66. intervals=[{'dtstart': now,
  67. 'duration': timedelta(seconds=5)},
  68. {'dtstart': now + timedelta(seconds=5),
  69. 'duration': timedelta(seconds=5)}]
  70. assert utils.get_active_period_from_intervals(intervals) == {'dtstart': now,
  71. 'duration': timedelta(seconds=10)}
  72. intervals=[objects.Interval(dtstart=now,
  73. duration=timedelta(seconds=5),
  74. signal_payload=1),
  75. objects.Interval(dtstart=now + timedelta(seconds=5),
  76. duration=timedelta(seconds=5),
  77. signal_payload=2)]
  78. assert utils.get_active_period_from_intervals(intervals) == {'dtstart': now,
  79. 'duration': timedelta(seconds=10)}
  80. assert utils.get_active_period_from_intervals(intervals, False) == objects.ActivePeriod(dtstart=now,
  81. duration=timedelta(seconds=10))
  82. def test_cron_config():
  83. assert utils.cron_config(timedelta(seconds=5)) == {'second': '*/5', 'minute': '*', 'hour': '*'}
  84. assert utils.cron_config(timedelta(minutes=1)) == {'second': '0', 'minute': '*/1', 'hour': '*'}
  85. assert utils.cron_config(timedelta(minutes=5)) == {'second': '0', 'minute': '*/5', 'hour': '*'}
  86. assert utils.cron_config(timedelta(hours=1)) == {'second': '0', 'minute': '0', 'hour': '*/1'}
  87. assert utils.cron_config(timedelta(hours=2)) == {'second': '0', 'minute': '0', 'hour': '*/2'}
  88. assert utils.cron_config(timedelta(hours=25)) == {'second': '0', 'minute': '0', 'hour': '0'}
  89. assert utils.cron_config(timedelta(seconds=10), randomize_seconds=True) == {'second': '*/10',
  90. 'minute': '*',
  91. 'hour': '*',
  92. 'jitter': 1}