test_utils.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from openleadr import utils, objects
  2. from dataclasses import dataclass
  3. import pytest
  4. from datetime import datetime, timezone, timedelta
  5. from collections import deque
  6. @dataclass
  7. class dc:
  8. a: int = 2
  9. def test_hasmember():
  10. obj = {'a': 1}
  11. assert utils.hasmember(obj, 'a') == True
  12. assert utils.hasmember(obj, 'b') == False
  13. obj = dc()
  14. assert utils.hasmember(obj, 'a') == True
  15. assert utils.hasmember(obj, 'b') == False
  16. def test_getmember():
  17. obj = {'a': 1}
  18. assert utils.getmember(obj, 'a') == 1
  19. obj = dc()
  20. assert utils.getmember(obj, 'a') == 2
  21. def test_setmember():
  22. obj = {'a': 1}
  23. utils.setmember(obj, 'a', 10)
  24. assert utils.getmember(obj, 'a') == 10
  25. obj = dc()
  26. utils.setmember(obj, 'a', 10)
  27. assert utils.getmember(obj, 'a') == 10
  28. @pytest.mark.asyncio
  29. async def test_delayed_call_with_func():
  30. async def myfunc():
  31. pass
  32. await utils.delayed_call(myfunc, delay=0.1)
  33. @pytest.mark.asyncio
  34. async def test_delayed_call_with_coro():
  35. async def mycoro():
  36. pass
  37. await utils.delayed_call(mycoro(), delay=0.1)
  38. @pytest.mark.asyncio
  39. async def test_delayed_call_with_coro_func():
  40. async def mycoro():
  41. pass
  42. await utils.delayed_call(mycoro, delay=0.1)
  43. def test_determine_event_status_completed():
  44. active_period = {'dtstart': datetime.now(timezone.utc) - timedelta(seconds=10),
  45. 'duration': timedelta(seconds=5)}
  46. assert utils.determine_event_status(active_period) == 'completed'
  47. def test_determine_event_status_active():
  48. active_period = {'dtstart': datetime.now(timezone.utc) - timedelta(seconds=10),
  49. 'duration': timedelta(seconds=15)}
  50. assert utils.determine_event_status(active_period) == 'active'
  51. def test_determine_event_status_near():
  52. active_period = {'dtstart': datetime.now(timezone.utc) + timedelta(seconds=3),
  53. 'duration': timedelta(seconds=5),
  54. 'ramp_up_duration': timedelta(seconds=5)}
  55. assert utils.determine_event_status(active_period) == 'near'
  56. def test_determine_event_status_far():
  57. active_period = {'dtstart': datetime.now(timezone.utc) + timedelta(seconds=10),
  58. 'duration': timedelta(seconds=5)}
  59. assert utils.determine_event_status(active_period) == 'far'
  60. def test_determine_event_status_far_with_ramp_up():
  61. active_period = {'dtstart': datetime.now(timezone.utc) + timedelta(seconds=10),
  62. 'duration': timedelta(seconds=5),
  63. 'ramp_up_duration': timedelta(seconds=5)}
  64. assert utils.determine_event_status(active_period) == 'far'
  65. def test_get_active_period_from_intervals():
  66. now = datetime.now(timezone.utc)
  67. intervals=[{'dtstart': now,
  68. 'duration': timedelta(seconds=5)},
  69. {'dtstart': now + timedelta(seconds=5),
  70. 'duration': timedelta(seconds=5)}]
  71. assert utils.get_active_period_from_intervals(intervals) == {'dtstart': now,
  72. 'duration': timedelta(seconds=10)}
  73. intervals=[objects.Interval(dtstart=now,
  74. duration=timedelta(seconds=5),
  75. signal_payload=1),
  76. objects.Interval(dtstart=now + timedelta(seconds=5),
  77. duration=timedelta(seconds=5),
  78. signal_payload=2)]
  79. assert utils.get_active_period_from_intervals(intervals) == {'dtstart': now,
  80. 'duration': timedelta(seconds=10)}
  81. assert utils.get_active_period_from_intervals(intervals, False) == objects.ActivePeriod(dtstart=now,
  82. duration=timedelta(seconds=10))
  83. def test_cron_config():
  84. assert utils.cron_config(timedelta(seconds=5)) == {'second': '*/5', 'minute': '*', 'hour': '*'}
  85. assert utils.cron_config(timedelta(minutes=1)) == {'second': '0', 'minute': '*/1', 'hour': '*'}
  86. assert utils.cron_config(timedelta(minutes=5)) == {'second': '0', 'minute': '*/5', 'hour': '*'}
  87. assert utils.cron_config(timedelta(hours=1)) == {'second': '0', 'minute': '0', 'hour': '*/1'}
  88. assert utils.cron_config(timedelta(hours=2)) == {'second': '0', 'minute': '0', 'hour': '*/2'}
  89. assert utils.cron_config(timedelta(hours=25)) == {'second': '0', 'minute': '0', 'hour': '0'}
  90. assert utils.cron_config(timedelta(seconds=10), randomize_seconds=True) == {'second': '*/10',
  91. 'minute': '*',
  92. 'hour': '*',
  93. 'jitter': 1}
  94. def test_get_event_from_deque():
  95. d = deque()
  96. now = datetime.now(timezone.utc)
  97. event1 = objects.Event(event_descriptor=objects.EventDescriptor(event_id='event123',
  98. event_status='far',
  99. modification_number='1',
  100. market_context='http://marketcontext01'),
  101. event_signals=[objects.EventSignal(signal_name='simple',
  102. signal_type='level',
  103. signal_id=utils.generate_id(),
  104. intervals=[objects.Interval(dtstart=now,
  105. duration=timedelta(minutes=10),
  106. signal_payload=1)])],
  107. targets=[{'ven_id': 'ven123'}])
  108. msg_one = {'message': 'one'}
  109. msg_two = {'message': 'two'}
  110. msg_three = {'message': 'three'}
  111. event2 = objects.Event(event_descriptor=objects.EventDescriptor(event_id='event123',
  112. event_status='far',
  113. modification_number='1',
  114. market_context='http://marketcontext01'),
  115. event_signals=[objects.EventSignal(signal_name='simple',
  116. signal_type='level',
  117. signal_id=utils.generate_id(),
  118. intervals=[objects.Interval(dtstart=now,
  119. duration=timedelta(minutes=10),
  120. signal_payload=1)])],
  121. targets=[{'ven_id': 'ven123'}])
  122. d.append(event1)
  123. d.append(msg_one)
  124. d.append(msg_two)
  125. d.append(msg_three)
  126. d.append(event2)
  127. assert utils.get_next_event_from_deque(d) is event1
  128. assert utils.get_next_event_from_deque(d) is event2
  129. assert utils.get_next_event_from_deque(d) is None
  130. assert utils.get_next_event_from_deque(d) is None
  131. assert len(d) == 3
  132. assert d.popleft() is msg_one
  133. assert d.popleft() is msg_two
  134. assert d.popleft() is msg_three